BlackBerry 上使用 Bouncy Castle 进行 RSA 填充

发布于 2024-10-21 09:50:48 字数 1585 浏览 2 评论 0原文

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

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

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

I'm using Bouncy Castle to encrypt strings to send them to my java web service where they are decrypted, when the message reaches the server I get a BadPaddingException, anybody know how to properly add the padding to an RSA Cipher with Bouncy Castle on J2ME?

This is the encryption code on the client:

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

And this is how I decrypt it server side:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒数 2024-10-28 09:50:48

不直接使用 RSAEngine,而是使用 PKCS1Encoding 类并构造它

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

Instead of using RSAEngine directly use the PKCS1Encoding class and construct it with

PKCS1Encoding engine = new PKCS1Encoding(new RSAEngine());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文