关于这个主题有很多问题,同样的解决方案,但这对我不起作用.我有一个加密的简单测试.加密/解密本身是有效的(只要我使用字节数组本身而不是字符串处理此测试).问题是不希望将它作为字节数组处理,而是作为String处理,但是当我将字节数组编码为字符串并返回时,生成的字节数组与原始字节数组不同,因此解密不再起作用.我在相应的字符串方法中尝试了以下参数:UTF-8,UTF8,UTF-16,UTF8.他们都没有工作.生成的字节数组与原始数组不同.任何想法为什么会这样?
加密:
public class NewEncrypter
{
private String algorithm = "DESede";
private Key key = null;
private Cipher cipher = null;
public NewEncrypter() throws NoSuchAlgorithmException, NoSuchPaddingException
{
key = KeyGenerator.getInstance(algorithm).generateKey();
cipher = Cipher.getInstance(algorithm);
}
public byte[] encrypt(String input) throws Exception
{
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] inputBytes = input.getBytes("UTF-16");
return cipher.doFinal(inputBytes);
}
public String decrypt(byte[] encryptionBytes) throws Exception
{
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
String recovered = new String(recoveredBytes, "UTF-16");
return recovered;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试的测试:
public class NewEncrypterTest
{
@Test
public …Run Code Online (Sandbox Code Playgroud) 我已经有了java加密代码。现在我想使用我的服务器上的 API。即使在尝试了各种教程和示例代码之后,我也无法成功解密哈希值。
我知道固定盐和静脉注射根本不推荐。但为了简单起见并为了理解问题,我将盐和IV保留为“00000000000000000000000000000000”;
Java 加密后的哈希 = "XjxCg0KK0ZDWa4XMFhykIw=="; 使用的私钥=“Mayur12354673645”
有人可以帮我使用 dart 解密上面的字符串吗?
JAVA代码
public String encrypt(String salt, String iv, String passphrase,
String plaintext) {
try {
SecretKey key = generateKey(salt, passphrase);
byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, iv, plaintext
.getBytes("UTF-8"));
return base64(encrypted);
} catch (UnsupportedEncodingException e) {
throw fail(e);
}
}
public String decrypt(String salt, String iv, String passphrase,
String ciphertext) {
try {
SecretKey key = generateKey(salt, passphrase);
byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, iv,
base64(ciphertext));
return new String(decrypted, "UTF-8");
} catch …Run Code Online (Sandbox Code Playgroud)