相关疑难解决方法(0)

AES加密Java无效密钥长度

我正在尝试创建AES加密方法,但出于某种原因,我一直在努力

java.security.InvalidKeyException: Key length not 128/192/256 bits

这是代码:

public static SecretKey getSecretKey(char[] password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException{
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    // NOTE: last argument is the key length, and it is 256
    KeySpec spec = new PBEKeySpec(password, salt, 1024, 256);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
    return(secret);
}


public static byte[] encrypt(char[] password, byte[] salt, String text) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
    SecretKey secret = getSecretKey(password, salt);

    Cipher cipher = …
Run Code Online (Sandbox Code Playgroud)

java encryption aes

9
推荐指数
2
解决办法
4万
查看次数

在python中加密并使用AES-CFB在Java中解密

我知道一个与此非常类似的问题(如何在Python中加密并在Java中解密?)但我有一个不同的问题.

我的问题是,我无法正确解密Java.尽管使用了正确的密钥和IV,我仍然在解密后获得垃圾字符.我在Java中没有任何编译/运行时错误或异常,因此我相信我正在使用正确的参数进行解密.

Python加密代码 -

from Crypto.Cipher import AES
import base64
key = '0123456789012345'
iv = 'RandomInitVector'
raw = 'samplePlainText'
cipher = AES.new(key,AES.MODE_CFB,iv)
encrypted = base64.b64encode(iv + cipher.encrypt(raw))
Run Code Online (Sandbox Code Playgroud)

Java解密代码 -

private static String KEY = "0123456789012345";
public static String decrypt(String encrypted_encoded_string) throws NoSuchAlgorithmException, NoSuchPaddingException,
    InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

      String plain_text = "";
      try{
          byte[] encrypted_decoded_bytes = Base64.getDecoder().decode(encrypted_encoded_string);
          String encrypted_decoded_string = new String(encrypted_decoded_bytes);
          String iv_string = encrypted_decoded_string.substring(0,16); //IV is retrieved correctly.

          IvParameterSpec iv = new IvParameterSpec(iv_string.getBytes());
          SecretKeySpec skeySpec = new …
Run Code Online (Sandbox Code Playgroud)

python java encryption pycrypto cfb-mode

4
推荐指数
1
解决办法
4046
查看次数

标签 统计

encryption ×2

java ×2

aes ×1

cfb-mode ×1

pycrypto ×1

python ×1