我需要可以通过 BouncyCastle Java API 使用 RSA 加密/解密数据的代码。我实现了它,但得到了以下异常:
javax.crypto.BadPaddingException: unknown block type in following code.
这是代码:
public class rsa {
private PrivateKey rsaPrivate;
private PublicKey rsapublic;
private Cipher cipher=null;
private final String ALGORITHM = "RSA";
private final String PROVIDER = "BC";
public rsa() throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException
{
this.init();
}
public void init() throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException
{
Security.addProvider(new BouncyCastleProvider());
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM,PROVIDER);
keyGen.initialize(1024);
KeyPair keyPair = keyGen.generateKeyPair();
this.setRsaPrivate(keyPair.getPrivate()) ;
this.setRsapublic(keyPair.getPublic());
}
********* Getter**(){} AND **Setter**(){} methods are removed **********
public String encryption(String Message) throws InvalidKeyException,IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchProviderException, NoSuchPaddingException,
UnsupportedEncodingException
{
cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding",PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE,this.getRsapublic());
byte[] encryptedMsg = cipher.doFinal(Message.getBytes());
return new String(encryptedMsg);
}
public String decryption(String encryptedMsg) throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, UnsupportedEncodingException,
NoSuchAlgorithmException, NoSuchProviderException,
NoSuchPaddingException
{
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding",PROVIDER);
cipher.init(Cipher.DECRYPT_MODE,this.getRsaPrivate());
byte[] dectyptedText = cipher.doFinal(encryptedMsg.getBytes());
return new String(dectyptedText);
}
public static void main(String args[]) throws Exception
{
rsa r=new rsa();
System.out.println("Test1 encrypt normal: "+Base64.encodeBase64String(r.encryption("123456").getBytes()));
System.out.println("Test2 decrypt normal: "+r.decryption(r.encryption("123456")));
}
}
* OUTPUT ** : Test1 加密正常:
uXpqwit/bNH9GUpCPoL+7pjVhOYfQT95ZHCHRoBntfYuKhV9cnQUeSe2df1oTdp45JZFG9uohGssnMP2BP9Lm6+xwxprQi7t2n3mjoLOTj+e+2rc2/hKlwhoHIpEmO6O7SWeQh+05PIhP9YnPOM97VKGfu/oXFj1284pC9s0smM= Exception in thread "main" javax.crypto.BadPaddingException: unknown block type at org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(未知来源)在 javax.crypto.Cipher.doFinal(DashoA13*..) 在 com.crypto.rsa.decryption(rsa.java:85) >>>>>>>>>>>>>>>>>> >>>在方法解密cipher.doFinal(encryptedMsg.getBytes())中出现错误
终于有输出了
public class RSAUTIL
{
String ALGORITHM_USED = "RSA";
String PROVIDER = "BC";
private KeyPair key;
public RSAUTIL() throws NoSuchAlgorithmException
{
this.init();
this.generateKey();
}
public void init()
{
Security.addProvider(new BouncyCastleProvider());
}
public KeyPair generateKey() throws NoSuchAlgorithmException
{
KeyPairGenerator keyGen = null;
try {
keyGen = KeyPairGenerator.getInstance(ALGORITHM_USED, PROVIDER);
}catch (NoSuchProviderException e){e.printStackTrace();}
keyGen.initialize(1024);
key = keyGen.generateKeyPair();
return key;
}
public PublicKey getpublickey()
{
return key.getPublic();
}
public PrivateKey getprivatekey()
{
return key.getPrivate();
}
public byte[] encrypt(byte[] text, PublicKey key) throws Exception
{
byte[] cipherText = null;
try
{
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding",PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(text);
}catch (Exception e){throw e;}
return cipherText;
}
public String encrypt(String text, PublicKey key) throws Exception
{
String encryptedText;
try
{ byte[] cipherText = encrypt(text.getBytes(),key);
encryptedText = encodeToBASE64(cipherText);
}catch (Exception e){throw e;}return encryptedText;
}
public byte[] decrypt(byte[] text, PrivateKey key) throws Exception
{
byte[] dectyptedText = null;
try
{
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding",PROVIDER);
cipher.init(Cipher.DECRYPT_MODE,key);
dectyptedText = cipher.doFinal(text);
}catch (Exception e){throw e;}
return dectyptedText;
}
public String decrypt(String text, PrivateKey key) throws Exception
{
String result;
try
{ byte[] dectyptedText = decrypt(decodeToBASE64(text),key);
result = new String(dectyptedText);
}catch (Exception e){throw e;}
return result;
}
public String getKeyAsString(Key key)
{
byte[] keyBytes = key.getEncoded();
BASE64Encoder b64 = new BASE64Encoder();
return b64.encode(keyBytes);
}
public PrivateKey getPrivateKeyFromString(String key) throws Exception
{
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_USED);
BASE64Decoder b64 = new BASE64Decoder();
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(b64.decodeBuffer(key));
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
return privateKey;
}
public PublicKey getPublicKeyFromString(String key) throws Exception
{
BASE64Decoder b64 = new BASE64Decoder();
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_USED);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(b64.decodeBuffer(key));
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
return publicKey;
}
private String encodeToBASE64(byte[] bytes)
{
BASE64Encoder b64 = new BASE64Encoder();
return b64.encode(bytes);
}
private byte[] decodeToBASE64(String text) throws IOException
{
BASE64Decoder b64 = new BASE64Decoder();
return b64.decodeBuffer(text);
}
public static void main(String[] args) throws Exception {
RSAUTIL rsa= new RSAUTIL();
System.out.println(rsa.decrypt(rsa.encrypt("123",
rsa.getPublicKeyFromString(rsa.getKeyAsString(rsa.getpublickey()))),
rsa.getPrivateKeyFromString(rsa.getKeyAsString(rsa.getprivatekey()))));
}
}
输出:123