javax.crypto.badpaddingexception:使用RSA和BASE64编码解密时解密错误
我有以下程序来加密和解密数据。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论