欢迎来到天天文库
浏览记录
ID:50811869
大小:53.00 KB
页数:6页
时间:2020-03-14
《IDEA加密算法源码(java版).doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、publicclassIDEA{privatebyte[]Encrypt(byte[]bytekey,byte[]inputBytes,booleanflag){//每一轮加密函数byte[]encryptCode=newbyte[8];int[]key=get_subkey(flag,bytekey);//分解子密钥encrypt(key,inputBytes,encryptCode);//进行加密操作returnencryptCode;//返回加密数据}privateintbytesToInt(byte[]inBytes,
2、intstartPos){//二进制数组转换为字节return((inBytes[startPos]<<8)&0xff00)+(inBytes[startPos+1]&0xff);}privatevoidintToBytes(intinputInt,byte[]outBytes,intstartPos){//字节转换为二进制数组outBytes[startPos]=(byte)(inputInt>>>8);outBytes[startPos+1]=(byte)inputInt;}privateintx_multiply_y(i
3、ntx,inty){//乘法运算if(x==0){x=0x10001-y;}elseif(y==0){x=0x10001-x;}else{inttmp=x*y;y=tmp&0xffff;x=tmp>>>16;x=(y-x)+((y4、tb=bytesToInt(inbytes,2);intc=bytesToInt(inbytes,4);intd=bytesToInt(inbytes,6);for(inti=0;i<8;i++){//八轮循环开始a=x_multiply_y(a,key[k++]);//步骤(1)b+=key[k++];//(2)b&=0xffff;c+=key[k++];//(3)c&=0xffff;d=x_multiply_y(d,key[k++]);//(4)inttmp1=b;inttmp2=c;c^=a;//(5)b^=d;//(65、)c=x_multiply_y(c,key[k++]);//(7)b+=c;//(8)b&=0xffff;b=x_multiply_y(b,key[k++]);//(9)c+=b;//(10)c&=0xffff;a^=b;//(11)d^=c;//(12)b^=tmp2;//(13)c^=tmp1;//(14)}intToBytes(x_multiply_y(a,key[k++]),outbytes,0);//将一轮循环后的子块转化为二进制数组下一轮使用intToBytes(c+key[k++],outbytes,2);int6、ToBytes(b+key[k++],outbytes,4);intToBytes(x_multiply_y(d,key[k]),outbytes,6);}privateint[]encrypt_subkey(byte[]byteKey){//加密时子密钥产生过程int[]key=newint[52];if(byteKey.length<16){byte[]tmpkey=newbyte[16];System.arraycopy(byteKey,0,tmpkey,tmpkey.length-byteKey.length,byte7、Key.length);byteKey=tmpkey;}for(inti=0;i<8;i++){key[i]=bytesToInt(byteKey,i*2);}for(intj=8;j<52;j++){if((j&0x7)<6){key[j]=(((key[j-7]&0x7f)<<9)8、(key[j-6]>>7))&0xffff;}elseif((j&0x7)==6){key[j]=(((key[j-7]&0x7f)<<9)9、(key[j-14]>>7))&0xffff;}else{key[j]=(((key[j-15]&0x10、7f)<<9)11、(key[j-14]>>7))&0xffff;}}returnkey;}privateintfun_a(inta){//解密子密钥中求逆算法if(a<2){returna;}intb=1;intc=0x10001/a;for(inti=0x10001%a;i!
4、tb=bytesToInt(inbytes,2);intc=bytesToInt(inbytes,4);intd=bytesToInt(inbytes,6);for(inti=0;i<8;i++){//八轮循环开始a=x_multiply_y(a,key[k++]);//步骤(1)b+=key[k++];//(2)b&=0xffff;c+=key[k++];//(3)c&=0xffff;d=x_multiply_y(d,key[k++]);//(4)inttmp1=b;inttmp2=c;c^=a;//(5)b^=d;//(6
5、)c=x_multiply_y(c,key[k++]);//(7)b+=c;//(8)b&=0xffff;b=x_multiply_y(b,key[k++]);//(9)c+=b;//(10)c&=0xffff;a^=b;//(11)d^=c;//(12)b^=tmp2;//(13)c^=tmp1;//(14)}intToBytes(x_multiply_y(a,key[k++]),outbytes,0);//将一轮循环后的子块转化为二进制数组下一轮使用intToBytes(c+key[k++],outbytes,2);int
6、ToBytes(b+key[k++],outbytes,4);intToBytes(x_multiply_y(d,key[k]),outbytes,6);}privateint[]encrypt_subkey(byte[]byteKey){//加密时子密钥产生过程int[]key=newint[52];if(byteKey.length<16){byte[]tmpkey=newbyte[16];System.arraycopy(byteKey,0,tmpkey,tmpkey.length-byteKey.length,byte
7、Key.length);byteKey=tmpkey;}for(inti=0;i<8;i++){key[i]=bytesToInt(byteKey,i*2);}for(intj=8;j<52;j++){if((j&0x7)<6){key[j]=(((key[j-7]&0x7f)<<9)
8、(key[j-6]>>7))&0xffff;}elseif((j&0x7)==6){key[j]=(((key[j-7]&0x7f)<<9)
9、(key[j-14]>>7))&0xffff;}else{key[j]=(((key[j-15]&0x
10、7f)<<9)
11、(key[j-14]>>7))&0xffff;}}returnkey;}privateintfun_a(inta){//解密子密钥中求逆算法if(a<2){returna;}intb=1;intc=0x10001/a;for(inti=0x10001%a;i!
此文档下载收益归作者所有