I trying implement algorithm AES 128 in Android but it doesn't work, the problem is import javax.xml.bind.DatatypeConverter;
DatatypeConverter.parseHexBinary(key)
and
DatatypeConverter.printBase64Binary(finalData)
Does an alternative exist?
My method:
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;
}
I see others answers, but I can't implement a solution.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…