关于java的IDEA加密算法

上http://www.bouncycastle.org/ 下载那个bouncy castle crypto API, 把
它的jar包加进来以后, 用以下一段很短的代码就可以实现IDEA加密了, good luck!

import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.engines.IDEAEngine;
import org.bouncycastle.util.encoders.Base64;
/**
* User: wooce
* Date: 2003-9-10
* Time: 10:36:05
*/
public class IDEAEncrypt {
private BufferedBlockCipher cipher;
private String key;

public IDEAEncrypt(String key) {
cipher = new BufferedBlockCipher(new IDEAEngine());
this.key = key;
}

public byte[] encrypt(String str) {
CipherParameters param = new KeyParameter(key.getBytes());
cipher.init(true, param);

int len = str.length();
if (str.length() % 8 > 0)
len -= str.length() % 8;
byte[] cleartext = new byte[str.length()];
System.arraycopy(str.getBytes(), 0, cleartext, 0, str.length());
byte[] out = new byte[cleartext.length];

if (len > 0)
cipher.processBytes(cleartext, 0, cleartext.length, out, 0);
if (str.length() % 8 > 0) {
int j = 0;
for (int i = str.length() - 1; j < str.length() % 8; i--) {
j++;
out[i] = (byte) (cleartext[i] ^ 197);
}
}
byte[] finalOut = Base64.encode(out);
return finalOut;
}
}

你可能感兴趣的:(java,算法,J#,idea)