【发布时间】:2019-12-18 08:21:11
【问题描述】:
加密字符串时,会在字符串末尾生成'\n'。
这就是我进行加密的方式
public static String encrypt(String plainText) throws Exception {
byte[] tdesKeyData = Consts.getSecretKey().getBytes();
byte[] myIV = Consts.getInitializationVector().getBytes();
SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "DES");
IvParameterSpec ivspec = new IvParameterSpec(myIV);
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
byte[] plainTextBytes = plainText.getBytes("UTF-8");
byte[] buf = cipher.doFinal(plainTextBytes);
byte[] base64Bytes = Base64.encode(buf, Base64.DEFAULT);
String base64EncryptedString = new String(base64Bytes);
return base64EncryptedString;
}
请有人指导我,我在这里做错了什么?提前致谢。
【问题讨论】:
-
一旦你解决了你的错误,只有一个建议:不要使用 DES,它现在已经过时了。使用 AES 之类的东西。
-
如果
Base64.DEFAULT被选为Base64#encode中的第二个参数,则结果包含换行符。使用Base64.NO_WRAP,结果没有换行符。 -
是的 刚刚在文档中看到developer.android.com/reference/android/util/Base64.html 并修复了@Topaco 是的,没错。
标签: java android encryption des