Blowfish加密算法之Java实现

本文介绍了Blowfish加密算法,并提供了Java实现的详细代码,包括算法原理、计算步骤以及加密解密方法。通过示例展示了如何使用密钥进行加密和解密操作,适合对加密算法感兴趣的读者深入研究。
摘要由CSDN通过智能技术生成

     好久没有写博客了,最近比较忙,更比较烦。工作忙的一塌糊涂,感情也没有着落,怎一个杯具了得呀~!


     前些天日方客户要求对用户信息文件加密,需要采用blowfish算法。其实网上blowfish代码也挺多的,不过给大家分享下,用的时候也方便。

    

     一、Blowfish算法是什么呢?     

         Blowfish是一个64位分组及可变密钥长度的分组密码算法,算法由两部分组成:密钥扩展和数据加密。密钥扩展把长度可达到448位的密钥转变成总共4168字节的几个子密钥。
  数据加密由一个简单函数迭代16轮,每一轮由密钥相关的置换,密钥相关和数据相关的代替组成。所有的运算都是32位字的加法和异或,仅有的另一个运算是每轮的四个查表。
  Blowfish使用了大量的子密钥,这些密钥必须在加密及解密之前进行预计算。


二、Blowfish算法的计算步骤?

  1.初始化P数组,然后是4个S盒用固定的串.这些串由π的十六进制组成.

  2.用密钥的第一个32位与P1异或,用密钥的第二个32位与P2异或,依此类推,直到密钥的所有位(直到P18).周期性地循环密钥的所有位直到整个P数组与密钥异或完为止.

  3.利用Blowfish算法加密全零串,其密钥为在第1和第2步中描述的子密钥.

  4.用第3步的输出取代P1和P2.

  5.利用Blowfish算法加密第3步的输出,其密钥为修改过的子密钥.

  6.用第5步的输出取代P3和P4.

  7.重重上述操作,直到P数组的所有元素及4个S盒全部被连续变化的Blowfish的输出所取代.

    主要代码:

  import java.security.MessageDigest;
import java.util.Random;
public class BlowfishUtil
{

    private BlowfishCBC m_bfish;
    private static Random m_rndGen = new Random();
    private static final char HEXTAB[] = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'a', 'b', 'c', 'd', 'e', 'f'
    };
   
    private static class BlowfishCBC extends BlowfishECB
    {

        public long getCBCIV()
        {
            return m_lCBCIV;
        }

        public void getCBCIV(byte dest[])
        {
            BlowfishUtil.longToByteArray(m_lCBCIV, dest, 0);
        }

        public void setCBCIV(long lNewCBCIV)
        {
            m_lCBCIV = lNewCBCIV;
        }

        public void setCBCIV(byte newCBCIV[])
        {
            m_lCBCIV = BlowfishUtil.byteArrayToLong(newCBCIV, 0);
        }

        public void cleanUp()
        {
            m_lCBCIV = 0L;
            super.cleanUp();
        }

        private long encryptBlockCBC(long lPlainblock)
        {
            lPlainblock ^= m_lCBCIV;
            lPlainblock = super.encryptBlock(lPlainblock);
            return m_lCBCIV = lPlainblock;
        }

        private long decryptBlockCBC(long lCipherblock)
        {
            long lTemp = lCipherblock;
            lCipherblock = super.decryptBlock(lCipherblock);
            lCipherblock ^= m_lCBCIV;
            m_lCBCIV = lTemp;
            return lCipherblock;
        }

        public void encrypt(byte inbuffer[], byte outbuffer[])
        {
            int nLen = inbuffer.length;
            for(int nI = 0; nI < nLen; nI += 8)
            {
                long lTemp = BlowfishUtil.byteArrayToLong(inbuffer, nI);
                lTemp = encryptBlockCBC(lTemp);
                BlowfishUtil.longToByteArray(lTemp, outbuffer, nI);
            }

        }

        public void encrypt(byte buffer[])
        {
            int nLen = buffer.length;
            for(int nI = 0; nI < nLen; nI += 8)
            {
                long lTemp = BlowfishUtil.byteArrayToLong(buffer, nI);
                lTemp = encryptBlockCBC(lTemp);
                BlowfishUtil.longToByteArray(lTemp, buffer, nI);
            }

        }

        public void encrypt(int inbuffer[], int outbuffer[])
        {
            int nLen = inbuffer.length;
            for(int nI = 0; nI < nLen; nI += 2)
            {
                long lTemp = BlowfishUtil.intArrayToLong(inbuffer, nI);
                lTemp = encryptBlockCBC(lTemp);
                BlowfishUtil.longToIntArray(lTemp, outbuffer, nI);
            }

        }

        public void encrypt(int buffer[])
        {
            int nLen = buffer.length;
            for(int nI = 0; nI < nLen; nI += 2)
            {
                long lTemp = BlowfishUtil.intArrayToLong(buffer, nI);
                lTemp = encryptBlockCBC(lTemp);
                BlowfishUtil.longToIntArray(lTemp, buffer, nI);
            }

        }

        public void encrypt(long inbuffer[], long outbuffer[])
        {
            int nLen = inbuffer.length;
            for(int nI = 0; nI < nLen; nI++)
                outbuffer[nI] = encryptBlockCBC(inbuffer[nI]);

        }

        public void encrypt(long buffer[])
        {
            int nLen = buffer.length;
            for(int nI = 0; nI < nLen; nI++)
                buffer[nI] = encryptBlockCBC(buffer[nI]);

        }

        public void decrypt(byte inbuffer[], byte outbuffer[])
        {
            int nLen = inbuffer.length;
            for(int nI = 0; nI < nLen; nI += 8)
            {
                long lTemp = BlowfishUtil.byteArrayToLong(inbuffer, nI);
                lTemp = decryptBlockCBC(lTemp);
                BlowfishUtil.longToByteArray(lTemp, outbuffer, nI);
            }

        }

        public void decrypt(byte buffer[])
        {
            int nLen = buffer.length;
            for(int nI = 0; nI < nLen; nI += 8)
            {
                long lTemp = BlowfishUtil.byteArrayToLong(buffer, nI);
                lTemp = decryptBlockCBC(lTemp);
                BlowfishUtil.longToByteArray(lTemp, buffer, nI);
            }

        }

        public void decrypt(int inbuffer[], int outbuffer[])
        {
            int nLen = inbuffer.length;
            for(int nI = 0; nI < nLen; nI += 2)
            {
                long lTemp = BlowfishUtil.intArrayToLong(inbuffer, nI);
                lTemp = decryptBlockCBC(lTemp);
                BlowfishUtil.longToIntArray(lTemp, outbuffer, nI);
            }

        }

        public void decrypt(int buffer[])
        {
            int nLen = buffer.length;
            for(int nI = 0; nI < nLen; nI += 2)
            {
                long lTemp = BlowfishUtil.intArrayToLong(buffer, nI);
                lTemp = decryptBlockCBC(lTemp);
         

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
该文件汇总了标题所述的加密方法的操作源码,并考虑到加密数据的文件存放问题,避免了byte[]转string类型时的数据失真的bug;下面是演示部分代码和演示结果: public static void main(String[] args) { try {//过程演示,普通的加解密操作 Eryptogram etg = new Eryptogram(); etg.debug = true; byte[] key = etg.getSecretKey(); String aa = "0123456789中文测试EnglishTest!%$#@!~~"; byte[] en = etg.encryptData(aa.getBytes(), key); /*********************************************************/ /*为了将加密数据写入文件但不出现错误,目前的方法是转为int类型进行存储*/ /*********************************************************/ int[] intTemp = new int[en.length/4]; for (int i = 0; i < intTemp.length; i++) { byte[] byteTemp = new byte[4]; byteTemp[0] = en[i*4+0]; byteTemp[1] = en[i*4+1]; byteTemp[2] = en[i*4+2]; byteTemp[3] = en[i*4+3]; intTemp[i] = etg.byteToint(byteTemp); // System.out.println("intTemp["+i+"]="+intTemp[i]); } /*********************************************************/ /*待提取文件内容后,将int转化为byte即可进行解密 */ /*********************************************************/ byte[] decodeTemp = new byte[intTemp.length*4]; for (int i = 0; i < intTemp.length; i++) { byte[] byteTemp = etg.intTobyte(intTemp[i]); decodeTemp[i*4+0] = byteTemp[0]; decodeTemp[i*4+1] = byteTemp[1]; decodeTemp[i*4+2] = byteTemp[2]; decodeTemp[i*4+3] = byteTemp[3]; } byte[] de = etg.decryptData(decodeTemp, key); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } try {//MD5非对称加密 Eryptogram etg = new Eryptogram(); System.out.println("对‘01234567’进行MD5加密后的结果:"+etg.encrypt("01234567")); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } } 结果: 生成密钥:6B:58:23:40:43:0B:F4:9D 加密前的字符串:0123456789中文测试EnglishTest!%$#@!~~ 加密前的二进串:30:31:32:33:34:35:36:37:38:39:D6:D0:CE:C4:B2:E2:CA:D4:45:6E:67:6C:69:73:68:54:65:73:74:21:25:24:23:40:21:7E:7E 加密后的二进串:72:83:1D:0A:BF:F0:EB:81:DE:B2:DD:58:CA:EA:4F:7B:8C:9F:98:13:01:99:F0:F4:48:6B:1E:8F:17:D2:3E:DF:9B:C1:90:39:D5:91:6D:F8 解密前的信息:72:83:1D:0A:BF:F0:EB:81:DE:B2:DD:58:CA:EA:4F:7B:8C:9F:98:13:01:99:F0:F4:48:6B:1E:8F:17:D2:3E:DF:9B:C1:90:39:D5:91:6D:F8 解密后的二进串:30:31:32:33:34:35:36:37:38:39:D6:D0:CE:C4:B2:E2:CA:D4:45:6E:67:6C:69:73:68:54:65:73:74:21:25:24:23:40:21:7E:7E 解密后的字符串:0123456789中文测试EnglishTest!%$#@!~~ 对‘01234567’进行MD5加密后的结果:2e9ec317e197819358fbc43afca7d837
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值