javax.crypto.badpaddingexception:使用RSA和BASE64编码解密时解密错误

发布于 2025-02-07 21:08:49 字数 2014 浏览 1 评论 0原文

我有以下程序来加密和解密数据。

import org.apache.commons.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.inject.Inject;
import java.security.*;


public class EncryptionUtil {

    static final String SECRET_ALGO = "RSA";
    static final String CIPHER_TOKENIZER_TRANSFORMER = "RSA/ECB/PKCS1Padding";
    static final int KEY_SIZE = 1024;

    KeyPairGenerator keyPairGen;
    KeyPair pair;
    Cipher cipher;

    @Inject
    public EncryptionUtil(){
        try{
            keyPairGen = KeyPairGenerator.getInstance(SECRET_ALGO);
            keyPairGen.initialize(KEY_SIZE);
            pair = keyPairGen.generateKeyPair();
            cipher = Cipher.getInstance(CIPHER_TOKENIZER_TRANSFORMER);
        }catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
            throw new IllegalStateException("Cannot Build Encryption Utils with given params");
        }
    }

    public String decrypt(String encrypted) {
        try {
            cipher.init(Cipher.DECRYPT_MODE, pair.getPrivate());
            return new String(cipher.doFinal(new Base64().decode(encrypted.getBytes())));
        } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException e) {
            throw new FiveXXServiceException(e.getMessage(), ErrorCode.DECRYPTION_EXCEPTION);
        }
    }

    public String encrypt(String text) {
        try {
            cipher.init(Cipher.ENCRYPT_MODE, pair.getPublic());
            return new String(new Base64().encode(cipher.doFinal(text.getBytes())));
        } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException e) {
            throw new FiveXXServiceException(e.getMessage(), ErrorCode.ENCRYPTION_EXCEPTION);
        }
    }

}

在这里,我在解密我尝试过的不同语法的数据时得到以下例外,

javax.crypto.BadPaddingException: Decryption error

但没有任何效果。 知道我在这里做错了什么吗?

I have following program for encrypting and decrypting data.

import org.apache.commons.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.inject.Inject;
import java.security.*;


public class EncryptionUtil {

    static final String SECRET_ALGO = "RSA";
    static final String CIPHER_TOKENIZER_TRANSFORMER = "RSA/ECB/PKCS1Padding";
    static final int KEY_SIZE = 1024;

    KeyPairGenerator keyPairGen;
    KeyPair pair;
    Cipher cipher;

    @Inject
    public EncryptionUtil(){
        try{
            keyPairGen = KeyPairGenerator.getInstance(SECRET_ALGO);
            keyPairGen.initialize(KEY_SIZE);
            pair = keyPairGen.generateKeyPair();
            cipher = Cipher.getInstance(CIPHER_TOKENIZER_TRANSFORMER);
        }catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
            throw new IllegalStateException("Cannot Build Encryption Utils with given params");
        }
    }

    public String decrypt(String encrypted) {
        try {
            cipher.init(Cipher.DECRYPT_MODE, pair.getPrivate());
            return new String(cipher.doFinal(new Base64().decode(encrypted.getBytes())));
        } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException e) {
            throw new FiveXXServiceException(e.getMessage(), ErrorCode.DECRYPTION_EXCEPTION);
        }
    }

    public String encrypt(String text) {
        try {
            cipher.init(Cipher.ENCRYPT_MODE, pair.getPublic());
            return new String(new Base64().encode(cipher.doFinal(text.getBytes())));
        } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException e) {
            throw new FiveXXServiceException(e.getMessage(), ErrorCode.ENCRYPTION_EXCEPTION);
        }
    }

}

Here I am getting the following exception while decrypting the data

javax.crypto.BadPaddingException: Decryption error

couple of different syntax i tried but nothing worked..
any idea what i am doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文