資源簡介
java對idea算法的實現,// String key = "0000000000000000";
// String data = "11111111馮";
// byte[] bytekey = key.getBytes();
// byte[] bytedata = data.getBytes();
//
// IDEA idea = new IDEA();
// byte[] encryptdata = idea.IdeaEncrypt(bytekey, bytedata, true);
// byte[] decryptdata = idea.IdeaEncrypt(bytekey, encryptdata, false);
//
// System.out.println("--------------------------------");
//
// for (int i = 0; i < bytedata.length; i++) {
// System.out.print(" " + bytedata[i] + " ");
// }
//
// System.out.println("");
//
// for (int i = 0; i < encryptdata.length; i++) {
// System.out.print(" " + encryptdata[i] + " ");
// }
//
// System.out.println("");
//
// for (int i = 0; i < decryptdata.length; i++) {
// System.out.print(" " + decryptdata[i] + " ");
代碼片段和文件信息
public?class?IDEA?{
private?byte[]?bytekey;
public?byte[]?getKey(String?key){
int?len1?=key.length();
if?(len1>=16)?{
key=key.substring(0?16);
}?else?{
for?(int?i=0;i<16-len1;i++){
key=key.concat(“0“);
}
}
bytekey=key.getBytes();
return?bytekey;
}
/**?
*?加密String明文輸入String密文輸出?
*?@param?strMing?
*?@return?
*/?
public?String?getEncString(String?strMing)?{?
byte[]?byteMi?=?null;?
byte[]?byteMing?=?null;?
String?strMi?=?““;?
try?{?
return?byte2hex(IdeaEncrypt(bytekeystrMing.getBytes()true)?);
}?
catch(Exception?e){?
e.printStackTrace();?
}?
finally?{?
byteMing?=?null;?
byteMi?=?null;?
}?
return?strMi;?
}?
/**?
*?解密?以String密文輸入String明文輸出?
*?@param?strMi?
*?@return?
*/?
public?String?getDesString(String?strMi)?{?
byte[]?byteMing?=?null;?
byte[]?byteMi?=?null;?
String?strMing?=?““;?
try?{?
String?tmp=?new?String(IdeaEncrypt(bytekeyhex2byte(strMi.getBytes())false?));?
int?len1=tmp.length();
return?tmp.substring(0?len1-6);
}?
catch(Exception?e)?{?
e.printStackTrace();?
}?
finally?{?
byteMing?=?null;?
byteMi?=?null;?
}?
return?strMing;?
}?
private?byte[]?Encrypt(byte[]?bytekey?byte[]?inputBytes?boolean?flag)?{
byte[]?encryptCode?=?new?byte[8];
//?分解子密鑰
int[]?key?=?get_subkey(flag?bytekey);
//?進行加密操作
encrypt(key?inputBytes?encryptCode);
//?返回加密數據
return?encryptCode;
}
private?int?bytesToInt(byte[]?inBytes?int?startPos)?{
return?((inBytes[startPos]?<8)?&?0xff00)?+
(inBytes[startPos?+?1]?&?0xff);
}
private?void?intToBytes(int?inputInt?byte[]?outBytes?int?startPos)?{
outBytes[startPos]?=?(byte)?(inputInt?>>>?8);
outBytes[startPos?+?1]?=?(byte)?inputInt;
}
private?int?x_multiply_y(int?x?int?y)?{
if?(x?==?0)?{
x?=?0x10001?-?y;
}?else?if?(y?==?0)?{
x?=?0x10001?-?x;
}?else?{
int?tmp?=?x?*?y;
y?=?tmp?&?0xffff;
x?=?tmp?>>>?16;
x?=?(y?-?x)?+?((y?}
return?x?&?0xffff;
}
private?void?encrypt(int[]?key?byte[]?inbytes?byte[]?outbytes)?{
int?k?=?0;
int?a?=?bytesToInt(inbytes?0);
int?b?=?bytesToInt(inbytes?2);
int?c?=?bytesToInt(inbytes?4);
int?d?=?bytesToInt(inbytes?6);
for?(int?i?=?0;?i?8;?i++)?{
a?=?x_multiply_y(a?key[k++]);
b?+=?key[k++];
b?&=?0xffff;
c?+=?key[k++];
c?&=?0xffff;
d?=?x_multiply_y(d?key[k++]);
int?tmp1?=?b;
int?tmp2?=?c;
c?^=?a;
b?^=?d;
c?=?x_multiply_y(c?key[k++]);
b?+=?c;
b?&=?0xffff;
b?=?x_multiply_y(b?key[k++]);
c?+=?b;
c?&=?0xffff;
a?^=?b;
d?^=?c;
b?^=?tmp2;
c?^=?tmp1;
}
intToBytes(x_multiply_y(a?key[k++])?outbytes?0);
intToBytes(c?+?key[k++]?outbytes?2);
intToBytes(b?+?key[k++]?outbytes?4);
intToBytes(x_multiply_y(d?key[k])?outbytes?6);
}
private?int[]?encrypt_subkey(byte[]?byteKey)?{
int[]?key?=?new?int[52];
if?(byteKey.length?16)?{
byte[]?tmpkey?=?new?byte[16];
System.arraycopy(byteKey?0?tmpkey
tmpkey.length?-?byteKey.length?byteKey.length);
byteKey?=?tmpkey;
}
for?(int?i?=?0;?i?8;?i++)?{
key[i]?=?bytesToInt(byteKey?i?*?2);
}
f
評論
共有 條評論