我试图在Android中实现AES 128算法,但它不起作用,问题出在import javax.xml.bind.DatatypeConverter;
DatatypeConverter.parseHexBinary(key)
和
DatatypeConverter.printBase64Binary(finalData)
是否存在替代方法?
我的方法:
private static final String ALGORIT = "AES";
public static String encryptHackro(String plaintext, String key)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, IOException, DecoderException {
byte[] raw = DatatypeConverter.parseHexBinary(key);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance(ALGORITMO);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] cipherText = cipher.doFinal(plaintext.getBytes(""));
byte[] iv = cipher.getIV();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(iv);
outputStream.write(cipherText);
byte[] finalData = outputStream.toByteArray();
String encodedFinalData = DatatypeConverter.printBase64Binary(finalData);
return encodedFinalData;
}
我看了别人的答案,但我无法实现一个解决方案。