【问题标题】:RSA Padding with Bouncy Castle on BlackBerry在 BlackBerry 上使用 Bouncy Castle 进行 RSA 填充
【发布时间】:2011-07-12 20:55:57
【问题描述】:

我正在使用 Bouncy Castle 加密字符串以将它们发送到我的 java Web 服务,在那里它们被解密,当消息到达服务器时,我得到一个 BadPaddingException,任何人都知道如何正确地将填充添加到 RSA 密码在 J2ME 上使用 Bouncy Castle?

这是客户端上的加密代码:

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;
    }

【问题讨论】:

    标签: blackberry encryption java-me rsa bouncycastle


    【解决方案1】:

    不要使用RSAEngine,而是直接使用PKCS1Encoding类并用

    构造它
    PKCS1Encoding engine = new PKCS1Encoding(new RSAEngine());
    

    【讨论】:

      猜你喜欢
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      相关资源
      最近更新 更多