gpt4 book ai didi

java - 长字符串的 AES 解密期间出现 BadPaddingException

转载 作者:行者123 更新时间:2023-12-01 20:16:01 24 4
gpt4 key购买 nike

public class AESEncryptionDecryption {

public static void main(String[] args) throws Exception {
String message = "FUN dfdf fgfgf dfffgf";
String randomNo = generateRandom(16); //16digit random number is generated.
//ENCRYPTION
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//byte[] ivBytes = Arrays.copyOfRange(randomNo.getBytes("UTF-8"), 0, 16);
IvParameterSpec iv = new IvParameterSpec(randomNo.getBytes("UTF-8"));
SecretKeySpec keyspec = new SecretKeySpec(randomNo.getBytes("UTF-8"), "AES");
cipher.init(Cipher.ENCRYPT_MODE, keyspec, iv);
cipher.update(message.getBytes("UTF-8"));
byte[] cipherText = cipher.doFinal();

//DECRYPTION
byte[] data = Base64.decodeBase64(base64);
cipher.init(Cipher.DECRYPT_MODE, keyspec, iv);
cipher.update(cipherText);
byte[] decryptedMessage = cipher.doFinal();
System.out.println("Decrypted Message :: " + new String(decryptedMessage,"UTF-8"));
}


}

我仅收到较长长度字符串的异常,对于较小长度的字符串,其工作正常。

最佳答案

import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class PasswordEncrypt {
public static String ALGORITHM = "AES";
private static String AES_CBS_PADDING = "AES/CBC/PKCS5Padding";
private static int AES_128 = 128;


private static byte[] encryptDecrypt(final int mode, final byte[] key, final byte[] IV, final byte[] message)
throws Exception {
final Cipher cipher = Cipher.getInstance(AES_CBS_PADDING);
final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
final IvParameterSpec ivSpec = new IvParameterSpec(IV);
cipher.init(mode, keySpec, ivSpec);
return cipher.doFinal(message);
}

public static Map<String, SecretKey> keyGenerator() throws NoSuchAlgorithmException{
Map<String, SecretKey> map = new HashMap<String, SecretKey>();
KeyGenerator keyGenerator = KeyGenerator.getInstance(PasswordEncrypt.ALGORITHM);
keyGenerator.init(AES_128);
SecretKey key = keyGenerator.generateKey();
map.put("key", key);
SecretKey IV = keyGenerator.generateKey();
map.put("iv", IV);
return map;

}


public static String encrypt(String message) throws Exception{
Map<String , SecretKey> map = keyGenerator();
SecretKey key = map.get("key");
SecretKey IV = map.get("iv");
byte[] cipherText = encryptDecrypt(Cipher.ENCRYPT_MODE, key.getEncoded(), IV.getEncoded(), message.getBytes());
String encrypted_message = Base64.getEncoder().encodeToString(cipherText);
String encodedKey = Base64.getEncoder().encodeToString(map.get("key").getEncoded());
String encodedIV = Base64.getEncoder().encodeToString(map.get("iv").getEncoded());

return encrypted_message+"javax"+encodedIV+"javax"+encodedKey;



}

public static String decrypt(String encryptedMessage) throws Exception{
String[] result = encryptedMessage.split("javax");
byte[] decodedIV = Base64.getDecoder().decode(result[1]);
byte[] decodedKey = Base64.getDecoder().decode(result[2]);
byte[] cipher_text = Base64.getDecoder().decode(result[0]);
SecretKey IV = new SecretKeySpec(decodedIV, 0, decodedIV.length, "AES");
SecretKey key = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
byte[] decryptedString = encryptDecrypt(Cipher.DECRYPT_MODE, key.getEncoded(), IV.getEncoded(), cipher_text);
String decryptedMessage = new String(decryptedString);
return decryptedMessage;

}


public static void main(String[] args) throws Exception {
PasswordEncrypt cu = new PasswordEncrypt();
String encryptedmessage = cu.encrypt("fds fasdf asdf asdf adsf asdrtwe trstsr sdffg sdf sgdfg fsd gfsd gfds gsdf gsdf");
System.out.println(encryptedmessage);
String decryptedMessage = cu.decrypt(encryptedmessage);
System.out.println(decryptedMessage);
}

输入:fds fasdf asdf asdf adsf asdrtwe trstsr sdffg sdf sgdfg fsd gfsd gfds gsdf gsdf 操作:h8Auwmfp98BPkIfjrMFt0myxwi/PSy+/g9Js3EOv31WC12mxg3KaimoTfpFO8Sc9e73kOhYpP0eoxigXf4dAXgqCVwqzT6tqUqm+6aDJKto=javaxeW6Fm9uf7kSI2OXPnyPr1g==javaxnhPcL3vz PWr2BfcmTKSsLQ==

关于java - 长字符串的 AES 解密期间出现 BadPaddingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58953861/

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