发布于 2009-08-30 12:28:03
查看Java Simplified Encryption (Jasypt)。
Jasypt是一个java库,它允许开发人员以最少的努力向他/她的项目添加基本的加密功能,并且不需要对密码学如何工作有深入的了解。
基于
的JCE
发布于 2012-02-12 19:54:22
我使用这个简单的One-Time-Pad算法:
import org.apache.commons.codec.binary.Base64;
public class Cipher {
private static final String KEY = "some-secret-key-of-your-choice";
public String encrypt(final String text) {
return Base64.encodeBase64String(this.xor(text.getBytes()));
}
public String decrypt(final String hash) {
try {
return new String(this.xor(Base64.decodeBase64(hash.getBytes())), "UTF-8");
} catch (java.io.UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
}
}
private byte[] xor(final byte[] input) {
final byte[] output = new byte[input.length];
final byte[] secret = this.KEY.getBytes();
int spos = 0;
for (int pos = 0; pos < input.length; ++pos) {
output[pos] = (byte) (input[pos] ^ secret[spos]);
spos += 1;
if (spos >= secret.length) {
spos = 0;
}
}
return output;
}
不要忘记将commons-codec
添加到类路径中。
https://stackoverflow.com/questions/1354803
复制相似问题