RSA加密和解密。将加密转换为解密时出错 [英] RSA Encryption And Decryption . Getting Error When I Convert Encryption To Decryption

查看:113
本文介绍了RSA加密和解密。将加密转换为解密时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用RSA算法进行加密和解密。我转换为字符串值加密它正常工作。当我将加密转换为解密时,我得到错误。下面我通过我的代码。请给我任何帮助

  public   final  字符串 modulusString =   2rRVVVFJRbH / wAPDtnwZwu + nxU + AZ6uXxh / SW + AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw / FfcM9DsKs / rYR83tBLiIAfgdnVjF27tZID + HJMFTiI30mALjr7 + TFP + 2lIACXA1RIKTk7S9pDmX8 =; 
public final String publicExponentString = AQAB;

加密代码:

public String EncryptionValue( String EncryptionValue) throws Base64DecodingException,NoSuchAlgorithmException,InvalidKeySpecException,NoSuchPaddingException,InvalidKeyException, IllegalBlockSizeException,UnsupportedEncodingException,BadPaddingException
{

byte [] modulebytes = Base64.decode(modulusString);
byte [] exponentbytes = Base64.decode(publicExponentString);

BigInteger module = new BigInteger( 1 ,modulebytes);
BigInteger publicexponent = new BigInteger( 1 ,exponentbytes);

RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(module,publicexponent);
KeyFactory fact = KeyFactory.getInstance( RSA);
PublicKey pubKey = fact.generatePublic(rsaPubKey);

Cipher cipher = Cipher.getInstance( RSA / ECB / PKCS1Padding );
cipher.init(Cipher.ENCRYPT_MODE,pubKey);

byte [] plainBytes = EncryptionValue.getBytes( UTF-8\" );
byte [] cipherData = cipher.doFinal(plainBytes);
String encryptedString = Base64.encode(cipherData);

return encryptedString;
}


解密代码:


public 字符串 DecryptionValue( String DecryptionValue) throws Base64DecodingException,NoSuchAlgorithmException,InvalidKeySpecException,NoSuchPaddingException,InvalidKeyException,UnsupportedEncodingException,IllegalBlockSizeException,BadPaddingException
{
byte [] modulebytes = Base64.decode( modulusString);
byte [] exponentbytes = Base64.decode(publicExponentString);

BigInteger modulus = new BigInteger( 1 ,modulebytes);
BigInteger exponent = new BigInteger( 1 ,exponentbytes);

RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulus,exponent);
KeyFactory fact = KeyFactory.getInstance( RSA);
PrivateKey privKey = fact.generatePrivate(rsaPrivKey);

Cipher cipher = Cipher.getInstance( RSA / ECB / PKCS1Padding );
cipher.init(Cipher.DECRYPT_MODE,privKey);

byte [] base64String = Base64.decode(DecryptionValue);
byte [] plainBytes = new String (base64String).getBytes( UTF-8);
plainBytes = cipher.update(plainBytes);
byte [] values = cipher.doFinal(plainBytes);

return new String (values, UTF-8);
}





我的错误:



线程中的异常  main javax.crypto。 BadPaddingException:解密错误
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java: 380
at sun.security.rsa。 RSAPadding.unpad(RSAPadding.java: 291
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java: 363
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java: 389
at javax.crypto.Cipher.doFinal(Cipher.java: 2121
at cryptocodefinal.CryptoCodeFinal.DecryptionValue(CryptoCodeFinal.java: 79
at cryptocodefinal.CryptoCodeFinal.main(CryptoCodeFinal.java: 148

解决方案

我想你会发现被解密的字符串的长度是错误的。



你需要填充数据作为必需的块大小,256字节IIRC,否则你会得到一个糟糕的填充错误。加密数据是 buyte [] ,然后将其转换为 String 。尝试不进行转换,你应该能够使事情有效。


hi,

i used RSA Algorithm to encryption and decryption . i convert to string value to encryption it's working properly. when i convert encryption to decryption that time i get error . below i past my code. please any one help to me

 public final String modulusString = "2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=";
   public final String publicExponentString = "AQAB";
  
Encryption Code :

 public String EncryptionValue(String EncryptionValue) throws Base64DecodingException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, UnsupportedEncodingException, BadPaddingException
    {

        byte[] modulebytes = Base64.decode(modulusString);
        byte[] exponentbytes = Base64.decode(publicExponentString);
       
        BigInteger module = new BigInteger(1,modulebytes);
        BigInteger publicexponent = new BigInteger(1,exponentbytes);
        
            RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(module, publicexponent);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PublicKey pubKey = fact.generatePublic(rsaPubKey);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);

        byte[] plainBytes = EncryptionValue.getBytes("UTF-8");
        byte[] cipherData = cipher.doFinal( plainBytes );
        String encryptedString = Base64.encode(cipherData);
        
        return encryptedString;
    }


Decryption Code :


  public String DecryptionValue(String DecryptionValue) throws Base64DecodingException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException
    {
        byte[] modulebytes = Base64.decode(modulusString);
        byte[] exponentbytes = Base64.decode(publicExponentString);
        
        BigInteger modulus = new BigInteger(1, modulebytes );
        BigInteger exponent = new BigInteger(1, exponentbytes);

        RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulus, exponent);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PrivateKey privKey = fact.generatePrivate(rsaPrivKey);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privKey);

        byte[] base64String = Base64.decode(DecryptionValue);
        byte[] plainBytes = new String(base64String).getBytes("UTF-8");
        plainBytes = cipher.update(plainBytes);
        byte[] values = cipher.doFinal(plainBytes);

        return new String(values, "UTF-8");
    }



My Error :

Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
	at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
	at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
	at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
	at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
	at javax.crypto.Cipher.doFinal(Cipher.java:2121)
	at cryptocodefinal.CryptoCodeFinal.DecryptionValue(CryptoCodeFinal.java:79)
	at cryptocodefinal.CryptoCodeFinal.main(CryptoCodeFinal.java:148)

解决方案

I think you'll find that the length of the string being decrypted is wrong.

You need to pad out the data to be the required block size, 256 bytes IIRC, or else you get a bad padding error. The encrypted data is buyte[] and you convert it to String. Try not doing the conversion and you should be able to make things work.


这篇关于RSA加密和解密。将加密转换为解密时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆