当我试图使用AES欧洲央行加密密钥的MD5散列时,然后解密它,就会得到不同的结果:
先于:>·ly†lrœœGQ2 2 a欧元
After:0¦tB?W
用于加密和解密的代码是:
public class AES {
private String a= "AES/ECB/NoPadding";
private byte[] key;
Cipher c;
public AES(byte [] key) throws NoSuchAlgorithmException, NoSuchPaddingException{
this.key = key;
c = Cipher.getInstance(a);
}
public String encrypt(byte[] Data) throws Exception{
Key k = new SecretKeySpec(key, "AES");
c.init(Cipher.ENCRYPT_MODE, k);
byte[] encoded = c.doFinal(Data);
String encrypted= new String(encoded);
return encrypted;
}
public String decrypt(byte[] v) throws Exception{
Key k = new SecretKeySpec(key, "AES");
if(v.length%16!=0)
return null;
c.init(Cipher.DECRYPT_MODE, k);
byte[] decv = c.doFinal(v);
String decrypted = new String(decv);
return decrypted;
}
}发布于 2016-05-25 17:44:27
这不管用:
String encrypted= new String(encoded);encoded中的密码文本字节是伪随机的胡言乱语.它们不太可能形成有效的文本编码,不管您的平台默认是什么。
如果需要文本表示,请使用base-64编码:
String encrypted = Base64.getEncoder().encodeToString(encoded);这是一个常见的错误,虽然它表现在不同的方式,所以“问题”是多种多样的,很难识别为重复的。Here's another answer提供更多细节。
https://stackoverflow.com/questions/37442548
复制相似问题