简体   繁体   English

尝试加密字符串时为什么会出现BadPaddingException?

[英]Why do I get BadPaddingException when trying to encrypt a string?

I create an Encryption class to encryt/decrypt binary data in my Android project, by this link . 通过此链接 ,我创建了一个Encryption类来Encryption /解密Android项目中的二进制数据。

package com.my.package;

import java.security.SecureRandom;

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

// TODO Incomplete class
public class Encryption {

    private static final byte[] salt = { (byte) 0xA4, (byte) 0x0B, (byte) 0xC8,
            (byte) 0x34, (byte) 0xD6, (byte) 0x95, (byte) 0xF3, (byte) 0x13 };

    private static int BLOCKS = 128;

    private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(clear);
        return encrypted;
    }

    private static byte[] decrypt(byte[] raw, byte[] encrypted)
            throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] decrypted = cipher.doFinal(encrypted);
        return decrypted;
    }

    private static byte[] getKey() throws Exception {
        byte[] keyStart = "this is a key".getBytes();
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        sr.setSeed(keyStart);
        kgen.init(128, sr); // 192 and 256 bits may not be available
        SecretKey skey = kgen.generateKey();
        byte[] key = skey.getEncoded();
        return key;
    }

    public static void test() {
        String test = "My Name Is Dragon Warrior";

        byte[] e = null;
        try {
            e = encrypt(getKey(), test.getBytes());
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        byte[] d = null;
        try {
            d = decrypt(getKey(), e);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println(new String(d));
    }
}

Then I run the code in main activity: 然后在主要活动中运行代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...
    Encryption.test();
    // ...
}

Then I get a BadPaddingException when the following code is being executed in test() : 然后在test()执行以下代码时,我得到BadPaddingException

try {
    d = decrypt(getKey(), e);
} catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

The funny thing is that I created a Java project other than Android project. 有趣的是,我创建了一个Java项目而不是Android项目。 And the code is running just fine without any exception. 而且代码运行正常,没有任何异常。

What's wrong with my code? 我的代码有什么问题?

"SHA1PRNG" is not a key derivation function, and the implementation may differ across providers. “ SHA1PRNG” 不是关键的派生功能,并且在提供程序之间的实现可能有所不同。 Please use a correct KDF such as PBKDF2 for passwords. 请使用正确的KDF(例如PBKDF2)作为密码。 "This is a key" is not a key, it is a string. “这是键” 不是键,是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javax.crypto.BadPaddingException,因为我尝试将十六进制字符串转换为字节数组。 我为什么得到它? - javax.crypto.BadPaddingException as I try to convert hex string to byte array. Why do I get it? 为什么从RSA解密中得到BadPaddingException? - Why do I get a BadPaddingException from RSA decryption? 解密文本文件中存储为位数组的数据时,为什么会出现“BadPaddingException:消息大于模数”? - Why do I get "BadPaddingException: Message is larger than modulus" when decrypting data stored as a bit array from text file? 为什么我在解密时收到“BadPaddingException”? - Why am I getting 'BadPaddingException' when decrypting? 当我做“”+1我得到一个字符串 - 为什么 - When I do “”+1 I get a String - Why 为什么在尝试在Java中反转字符串时会出现类型不匹配的问题? - Why do I get a type mismatch when trying to reverse a string in Java? 尝试创建新控制台时为什么会出现异常? - Why do I get an exception when trying to create a new Console? 为什么在尝试输入参数时出现错误? - Why do I get an error when trying to enter a parameter? 尝试在表上绘制图形时为什么会出现NullPointerException? - Why do I get a NullPointerException when trying to draw graphics on a table? 尝试DFS此图时为什么会出现StackOverFlowError? - Why do I get StackOverFlowError when trying to DFS this graph?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM