2011-03-10 81 views
3

我使用Bouncy城​​堡來加密字符串,將它們發送到我的Java Web服務,他們在那裏解密,當消息到達服務器時,我得到一個BadPaddingException,任何人都知道如何正確地添加在J2ME上用Bouncy Castle填充RSA密碼?RSA填充與充氣城堡在BlackBerry

這是客戶端上的加密代碼:

public byte[] Encrypt(byte[] data) 
    { 
    RSAKeyParameters publicKey = new RSAKeyParameters(false, new BigInteger(_publicKeyModulus), new BigInteger(_publicKeyExponent)); 
    RSAEngine engine = new RSAEngine(); 
    engine.init(true, publicKey); 

    byte[] output = engine.processBlock(data, 0, data.length); 

    return output; 
    } 

這就是我如何解密服務器端:

public byte[] Decrypt(byte[] data) 
    { 
     try { 
      Cipher cipher = Cipher.getInstance("RSA"); 
      cipher.init(Cipher.DECRYPT_MODE, privateKey); 
      byte[] cipherData = cipher.doFinal(data); 
      return cipherData; 
     } catch (NoSuchAlgorithmException ex) { 
      Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (NoSuchPaddingException ex) { 
      Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); 
     } catch(IllegalBlockSizeException ex) { 
      Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); 
     } catch(InvalidKeyException ex) { 
      Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); 
     } catch(BadPaddingException ex) { 
      Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     return null; 
    } 
+0

發佈整個堆棧跟蹤。 – 2011-03-11 12:19:14

回答

7

而不是使用RSAEngine直接使用PKCS1Encoding類以及構建它

PKCS1Encoding engine = new PKCS1Encoding(new RSAEngine()); 
+0

非常感謝Greg :) – 8vius 2011-03-11 15:06:07