gpt4 book ai didi

java - SlowAES加密和java解密

转载 作者:行者123 更新时间:2023-11-30 05:12:06 25 4
gpt4 key购买 nike

我尝试实现 AES .NET 中讨论的相同步骤

但没有成功,我似乎无法让java和slowAes一起玩...附件是我的代码我很抱歉我无法添加更多这是我第一次尝试处理加密将不胜感激任何帮助

private static final String ALGORITHM = "AES";
private static final byte[] keyValue = getKeyBytes("12345678901234567890123456789012");

private static final byte[] INIT_VECTOR = new byte[16];
private static IvParameterSpec ivSpec = new IvParameterSpec(INIT_VECTOR);

public static void main(String[] args) throws Exception {
String encoded = encrypt("watson?");
System.out.println(encoded);
}

private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGORITHM);
// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
// key = keyFactory.generateSecret(new DESKeySpec(keyValue));
return key;
}

private static byte[] getKeyBytes(String key) {
byte[] hash = DigestUtils.sha(key); // key.getBytes()
byte[] saltedHash = new byte[16];
System.arraycopy(hash, 0, saltedHash, 0, 16);
return saltedHash;
}

public static String encrypt(String valueToEnc) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, key,ivSpec);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = new BASE64Encoder().encode(encValue);
return encryptedValue;
}

public static String decrypt(String encryptedValue) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}

返回的字节不同提前致谢。

最佳答案

您使用 CBC 模式下的 AES 和 PKC5Padding 加密数据,但仅使用普通 AES 解密。在解密方法中,您需要使用“AES/CBC/PKCS5Padding”创建密码,就像在加密方法中一样。

关于java - SlowAES加密和java解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3032832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com