永发信息网

java环境下实现idea算法的加密解密

答案:2  悬赏:30  手机版
解决时间 2021-04-06 04:03
java环境下实现idea算法的加密解密
最佳答案


package frelationmainten;


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;
}

public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
try {
return byte2hex(IdeaEncrypt(bytekey,strMing.getBytes(),true) );
}
catch(Exception e){
e.printStackTrace();
}
finally {
byteMing = null;
byteMi = null;
}
return strMi;
}

public String getDesString(String strMi) {
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
String tmp= new String(IdeaEncrypt(bytekey,hex2byte(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 < x) ? 1 : 0);
}

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);
}

for (int j = 8; j < 52; j++) {
if ((j & 0x7) < 6) {
key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 6] >> 7)) &
0xffff;
} else if ((j & 0x7) == 6) {
key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 14] >> 7)) &
0xffff;
} else {
key[j] = (((key[j - 15] & 0x7f) << 9) | (key[j - 14] >> 7)) &
0xffff;
}
}

return key;
}

private int fun_a(int a) {
if (a < 2) {
return a;
}

int b = 1;
int c = 0x10001 / a;

for (int i = 0x10001 % a; i != 1;) {
int d = a / i;
a %= i;
b = (b + (c * d)) & 0xffff;

if (a == 1) {
return b;
}
d = i / a;
i %= a;
c = (c + (b * d)) & 0xffff;
}

return (1 - c) & 0xffff;
}

private int fun_b(int b) {
return (0 - b) & 0xffff;
}

private int[] uncrypt_subkey(int[] key) {
int dec = 52;
int asc = 0;
int[] unkey = new int[52];
int aa = fun_a(key[asc++]);
int bb = fun_b(key[asc++]);
int cc = fun_b(key[asc++]);
int dd = fun_a(key[asc++]);
unkey[--dec] = dd;
unkey[--dec] = cc;
unkey[--dec] = bb;
unkey[--dec] = aa;

for (int k1 = 1; k1 < 8; k1++) {
aa = key[asc++];
bb = key[asc++];
unkey[--dec] = bb;
unkey[--dec] = aa;
aa = fun_a(key[asc++]);
bb = fun_b(key[asc++]);
cc = fun_b(key[asc++]);
dd = fun_a(key[asc++]);
unkey[--dec] = dd;
unkey[--dec] = bb;
unkey[--dec] = cc;
unkey[--dec] = aa;
}

aa = key[asc++];
bb = key[asc++];
unkey[--dec] = bb;
unkey[--dec] = aa;
aa = fun_a(key[asc++]);
bb = fun_b(key[asc++]);
cc = fun_b(key[asc++]);
dd = fun_a(key[asc]);
unkey[--dec] = dd;
unkey[--dec] = cc;
unkey[--dec] = bb;
unkey[--dec] = aa;

return unkey;
}

private int[] get_subkey(boolean flag, byte[] bytekey) {
if (flag) {
return encrypt_subkey(bytekey);
} else {
return uncrypt_subkey(encrypt_subkey(bytekey));
}
}

private byte[] ByteDataFormat(byte[] data, int unit) {
int len = data.length;
int padlen = unit - (len % unit);
int newlen = len + padlen;
byte[] newdata = new byte[newlen];
System.arraycopy(data, 0, newdata, 0, len);

for (int i = len; i < newlen; i++)
newdata[i] = (byte) padlen;

return newdata;
}

public byte[] IdeaEncrypt(byte[] idea_key, byte[] idea_data, boolean flag) {
byte[] format_key = ByteDataFormat(idea_key, 16);
byte[] format_data = ByteDataFormat(idea_data, 8);

int datalen = format_data.length;
int unitcount = datalen / 8;
byte[] result_data = new byte[datalen];

for (int i = 0; i < unitcount; i++) {
byte[] tmpkey = new byte[16];
byte[] tmpdata = new byte[8];
System.arraycopy(format_key, 0, tmpkey, 0, 16);
System.arraycopy(format_data, i * 8, tmpdata, 0, 8);

byte[] tmpresult = Encrypt(tmpkey, tmpdata, flag);
System.arraycopy(tmpresult, 0, result_data, i * 8, 8);
}

return result_data;
}


public static String byte2hex(byte[] b) { //一个字节的数,
// 转成16进制字符串
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
//整数转成十六进制表示
stmp = (java.lang.Integer.toHexString(b[n] & 0xFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase(); //转成大写
}

public static byte[] hex2byte(byte[] b) {
if((b.length%2)!=0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length/2];
for (int n = 0; n < b.length; n+=2) {
String item = new String(b,n,2);
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节
b2[n/2] = (byte)Integer.parseInt(item,16);
}

return b2;
}

public static void main(String[] args) {

IDEA idea = new IDEA();

idea.getKey("aadd");//生成密匙

String strEnc = idea.getEncString("1234567890");//加密字符串,返回String的密文
System.out.println(strEnc);

String strDes = idea.getDesString(strEnc);//把String 类型的密文解密
System.out.println(strDes);

// 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] + " ");
// }

}
}参考资料:http://www.sfcode.cn/soft/019873433.htm
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
第三个字是花的诗句,第三个字是风的七言古诗
一个鸡蛋悬浮于盐水中,如果向盐水中慢慢地倒
全面战争 有没有人知道这是哪个mod
套路段子,一大段感人的故事然后最后来一句“
问道79体木
高中毕业生登记表丢了,怎么补办
在单位工作3年,没有犯错,底工资被降,合法
我最感激名言,真诚感激别人的名言
单选题Havingintroducedthenewequipment
形容一个人很坏的诗词,有没有形容一个人很落
我的墙有点渗水,怎样补漏
清实录的图书信息
a second time 和 the second time 有什么区
不同品牌之间电力猫和路由器的链接
2*RVV*0.5表示的是几根线
推荐资讯
桑塔纳经典哪里还有新车卖的
DDR4内存、GDDR6显存、SSD固态硬盘分别什么时
H3C 5024P 交换机能不能绑定IP地址?
1982农历8月22是阳历哪一天
iphone5s怎么设置qq消息提示您收到一条信息
准高三的励志语录,高一学生对高三学生说的祝
如何经营超市
我想爷爷了怎么办?
微信怎么查对方删了我,怎么让别人主动加我微
这音箱怎用?
高山滑雪2007中文版 在哪下载?
以优势为话题的400字作文
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?