如何使用 rsa 私钥解密数据

发布于 2024-07-25 03:09:14 字数 1008 浏览 1 评论 0原文

我正在使用JAVA 我的朋友使用 SYMBIAN

我和我的朋友有相同的 rsa 模数。 如果我使用公钥加密数据,那么我的朋友就能够解密该数据。 但如果我的朋友用公钥加密数据,那么我就无法解密数据。 我收到错误“数据必须从零开始”

public static byte[] encrypt(byte[] encrptdByte) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    byte[] encryptionByte = null;
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    encryptionByte = cipher.doFinal(encrptdByte);
    return encryptionByte;
}

public static void decrypt(byte[] encrptdByte) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
    byte[] encryptionByte = null;
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, publicKey);
    encryptionByte = cipher.doFinal(encrptdByte);

    System.out.println("Recovered String     :::  " + new String(encryptionByte));
}

谢谢 苏尼尔

I am using JAVA
My friend uses SYMBIAN

I and my friend have same rsa modulus. If I encrypt the data using public key then my friend is able to decrypt the same. But if my friend encrypt the data with public key then I am not able to decrypt the data. I got an error as "Data must start with zero "

public static byte[] encrypt(byte[] encrptdByte) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    byte[] encryptionByte = null;
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    encryptionByte = cipher.doFinal(encrptdByte);
    return encryptionByte;
}

public static void decrypt(byte[] encrptdByte) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
    byte[] encryptionByte = null;
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, publicKey);
    encryptionByte = cipher.doFinal(encrptdByte);

    System.out.println("Recovered String     :::  " + new String(encryptionByte));
}

Thanks
Sunil

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

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

发布评论

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

评论(2

泛滥成性 2024-08-01 03:09:14

decrypt 函数使用 publicKey - 它来自哪里? 请注意,使用公钥加密的数据必须使用相应的私钥解密,而不是使用相同的公钥。 RSA 等非对称加密具有密钥对的概念,其中密钥对中的每个密钥都可以解密使用另一个密钥加密的数据,而 AES 等对称加密则使用相同的密钥用于加密和解密。

The decrypt function uses publicKey - where does it come from? Note that data encrypted with a public key must be decrypted with the corresponding private key and not with the same public key. Asymmetric encryption such as RSA have the notion of key pairs where each key in the pair can decrypt data encrypted with the other key, in contrast to symmetric encryption such as AES where the same key works for both encryption and decryption.

远昼 2024-08-01 03:09:14

补充一下上一篇文章,使用非对称加密大规模加密/解密数据是不切实际的(因为它比对称加密慢得多)。 非对称加密(如 RSA)最实际的用途是对用于加密数据的对称密钥(对于 AES 或类似算法)进行加密,并对消息摘要的安全散列(SHA-256 等)进行签名。

加密的消息通常密封在“信封”中,其中包含加密的消息以及用于加密的密钥。 当然,密钥是用接收者的公钥加密的,从而确保只有私钥的持有者才能检索密钥。

最后,消息的发送者可以选择计算消息的安全散列并使用发送者的私钥对其进行加密。 接收者解密加密的哈希值(使用发送者的公钥)并与计算出的哈希值进行比较以验证发送者的身份。

To add the previous post, it's impractical to encrypt / decrypt data on a large scale using assymetric encryption (because it's significantly slower than symmetric encryption). The most practical use of assymmetric encryption (like RSA) is to encrypt the symmetric keys (for AES or similar algorithm) that were used to encrypt the data and also to sign a secure hash of the message digest (SHA-256 etc).

The encrypted message is typically sealed in an "envelope" that contains the encrypted message as well as the keys used for encryption. The keys are of course encrypted with the recipients public key, thereby ensuring that only the holder the private key can retrieve the keys.

Finally, the sender of the message may optionally compute a secure hash of the message and encrypt it with the sender's private key. The recipient decrypts the encrypted hash (using the sender's public key) and compares with the computed hash to verify the identity of the sender.

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