【发布时间】:2017-07-10 05:16:29
【问题描述】:
如果我尝试使用密钥 qwertykey 加密“123456”,我会通过在线工具 https://www.tools4noobs.com/online_tools/encrypt/ 得到 UVEXg9fgBxo= 作为响应。
但如果我使用 android 代码,我会得到 2XQNkfXlN6E= 作为加密值。
谁能给我解释一下这是怎么做到的?
我的代码是:
public String encrypt(String plainTextPassword){
String encrypted = "";
try{
DESKeySpec keySpec = new DESKeySpec("qwertykey".getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
byte[] cleartext = plainTextPassword.getBytes();
Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
cipher.init(Cipher.ENCRYPT_MODE, key);
encrypted = Base64.encodeToString(cipher.doFinal(cleartext),Base64.DEFAULT);
}catch (Exception e){
}
return encrypted;
}
【问题讨论】:
-
在
catch块中打印异常... -
确保使用与在线工具相同的参数(模式、iv)(顺便说一句有点可疑)。
-
你是用海绵城堡罐子加解密吗??
-
DES 不再安全。代码中的关键用法也不是最好的方法。检查 this 库以获取 AES 算法的安全实现。