【发布时间】:2014-10-24 13:49:27
【问题描述】:
我写了一个类来用 AES 加密一个字符串:
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import javax.xml.bind.DatatypeConverter;
import org.bouncycastle.util.encoders.Hex;
public class CipherAES {
public static void brutToHexa(byte[] t) {
byte[] tab = Hex.encode(t);
System.out.print("secret key : ");
for (int i = 0; i < tab.length; i++) {
System.out.print((char) tab[i] + "");
}
System.out.println();
}
public static byte[] encrypter(final String message, SecretKey cle)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, cle);
byte[] donnees = message.getBytes();
return cipher.doFinal(donnees);
}
public static String decrypter(final byte[] donnees, SecretKey cle)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, cle);
return new String(cipher.doFinal(donnees));
}
public static void main(String[] args) {
final String message = "Java is the best";
KeyGenerator keyGen;
try {
keyGen = KeyGenerator.getInstance("AES");
SecretKey cle = keyGen.generateKey();
brutToHexa(cle.getEncoded());
byte[] enc = encrypter(message, cle);
System.out.print("encrypted text : ");
System.out.println(DatatypeConverter.printBase64Binary(enc));
String dec = decrypter(enc, cle);
System.out.println("decrypted text : " + dec);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException |
IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
}
}
我以 HEX 格式显示密钥和使用 Base64 编码加密的文本。 要运行,我得到:
secret key : dfaa3b49adbc546d4437107b6a666cb1
encrypted text : iwEjj0Gahfzgq4BWrdY9odNX9PqvHgppz9YZ3mddQq8=
decrypted text : Java is the best
这里是类解密:
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
public class DecryptCipherAES {
public static void main(String[] args) {
try {
byte[] key = ("dfaa3b49adbc546d4437107b6a666cb1").getBytes();
SecretKey secretKey = new SecretKeySpec(key, "AES");
String base64String = "iwEjj0Gahfzgq4BWrdY9odNX9PqvHgppz9YZ3mddQq8=";
byte[] enc = org.apache.commons.codec.binary.Base64.decodeBase64(base64String.getBytes());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
String dec = new String(cipher.doFinal(enc));
System.out.println("texte decrypte : " + dec);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
}
}
运行时,我得到这个异常: javax.crypto.BadPaddingException:给定最终块未正确填充
【问题讨论】:
-
我不认为解密密钥是正确的,因为你已经显示转换为十六进制,可能是错误的想法,这正是我跳出来的原因
-
我已经检查过了,当我转换十六进制时,我得到相同的字节键。或者提交我可以检查密钥的完整性?
-
我运行你的代码,将
cle.getEncoded()值传递给DecryptCipherAES(我合并了代码),它工作正常。您需要手动将密钥String“解开”回byte[],我认为您不能简单地依赖使用String#getBytes