Java RSA加密/解密问题
我需要的代码,可以加密/解密数据使用RSA的BouncyCastleJavaAPI。我实现了它,但出现了以下异常:Java RSA加密/解密问题,java,rsa,bouncycastle,jce,Java,Rsa,Bouncycastle,Jce,我需要的代码,可以加密/解密数据使用RSA的BouncyCastleJavaAPI。我实现了它,但出现了以下异常: javax.crypto.BadPaddingException: unknown block type in following code. 代码如下: public class rsa { private PrivateKey rsaPrivate; private PublicKey rsapublic; private Cipher cipher=null; priva
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")));
}
}
*输出**:测试1加密正常:
uXpqwit/bNH9GUpCPoL+7PJVHOYFQT95ZHCHROBNTFYKHV9CNQUESE2DF1OTDP45JZFG9UOHGSSNMP2BP9Lm6+xwxprQi7t2n3mjoLOTj+e+2rc2/HKLHOHIPEMO6O7SWEQH+05PIhP9YnPOM97VKGfu/oXFj1284pC9s0smM=线程“main”javax.crypto.BadPaddingException中的异常:org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(未知源)处的未知块类型位于com.crypto.rsa.decryption(rsa.java:85)处的javax.crypto.Cipher.doFinal(DashoA13*)。解密(rsa.java:85)>>>>>>>>>>>>>>>>>>>>>>>在方法解密Cipher.doFinal(encryptedMsg.getBytes()中出错
最终获得了输出
public class RSAUTIL
{
}
输出:123加密数据来自哪里?它真的是用PKCS#1 v1.5填充加密的纯RSA吗?请不要一次发布两个问题。我已经编辑了你的帖子,删除了第二个问题。请注意,您的第二个问题对SO无效-我们不进行代码审查。试试看。@Robert:我需要加密纯文本的代码,返回Base64编码的加密文本,并将其解密为纯文本。我认为我的加密工作正常,但解密不起作用。请不要在评论中发布stacktraces。编辑你的任务并添加到那里。最后我得到了正确的输出,
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()))));
}