sybrenstuvel / python-rsa

Python-RSA is a pure-Python RSA implementation.
https://stuvel.eu/rsa
Other
489 stars 114 forks source link

How to convert this piece of Java code into RSA #222

Closed achieve-dream1221 closed 1 year ago

achieve-dream1221 commented 1 year ago

use rsa, get this error, but java can do this

image

java

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RSADecryptionExample {
    public static void main(String[] args) throws Exception {
        String publicKeyPEM = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvvn357aKesK5kf/sASluITOcbBNU/r/Hyzjtg/xEr3Cc09h2k+/1noqG5YhSkUXwsAf7okdirwPfQNEMB1yaYaiaCsdkcQF8ER6bGkHiDGAq9F32Iyojj0ci4zh9LnspOmqazt9FYXujAD12BbiEVv16C1MgH6tDteRCbZUpLqwIDAQAB";
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyPEM);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        String encryptedData = "oqqioKd5VCldXY43ggx3wJ3E1IxBB8gCVY+3Q++EkGWTZ++XF5BrqUj+BgTjWtgfgKiQvOc8KpOcWXjKuowhjyVWuSU4dy6DZeKAc78Ni970ydkCI2nzNV4T+pDg25iyC5s7POet54vqCR/mgIf+Rg/Yww4uVysnAOhMkRbjw3M=";
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}

this is result

image

sybrenstuvel commented 1 year ago

Please do not attach screenshots of code. It makes it impossible for anyone who wants to help to copy-paste the code and give it a try.

achieve-dream1221 commented 1 year ago

@sybrenstuvel this is public key to decrypt

python code:

from rsa import PublicKey
import rsa
import base64

public_key = PublicKey.load_pkcs1_openssl_der(base64.b64decode(
    "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvvn357aKesK5kf/sASluITOcbBNU/r/Hyzjtg/xEr3Cc09h2k+/1noqG5YhSkUXwsAf7okdirwPfQNEMB1yaYaiaCsdkcQF8ER6bGkHiDGAq9F32Iyojj0ci4zh9LnspOmqazt9FYXujAD12BbiEVv16C1MgH6tDteRCbZUpLqwIDAQAB"))
cipher_text = "oqqioKd5VCldXY43ggx3wJ3E1IxBB8gCVY+3Q++EkGWTZ++XF5BrqUj+BgTjWtgfgKiQvOc8KpOcWXjKuowhjyVWuSU4dy6DZeKAc78Ni970ydkCI2nzNV4T+pDg25iyC5s7POet54vqCR/mgIf+Rg/Yww4uVysnAOhMkRbjw3M="
result = rsa.decrypt(base64.b64decode(cipher_text), public_key)
print(result)

Java code

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RSADecryptionExample {
    public static void main(String[] args) throws Exception {
        String publicKeyPEM = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvvn357aKesK5kf/sASluITOcbBNU/r/Hyzjtg/xEr3Cc09h2k+/1noqG5YhSkUXwsAf7okdirwPfQNEMB1yaYaiaCsdkcQF8ER6bGkHiDGAq9F32Iyojj0ci4zh9LnspOmqazt9FYXujAD12BbiEVv16C1MgH6tDteRCbZUpLqwIDAQAB";
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyPEM);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        String encryptedData = "oqqioKd5VCldXY43ggx3wJ3E1IxBB8gCVY+3Q++EkGWTZ++XF5BrqUj+BgTjWtgfgKiQvOc8KpOcWXjKuowhjyVWuSU4dy6DZeKAc78Ni970ydkCI2nzNV4T+pDg25iyC5s7POet54vqCR/mgIf+Rg/Yww4uVysnAOhMkRbjw3M=";
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}
achieve-dream1221 commented 1 year ago

I solved this problem withM2Crypto library

import base64
import json

from M2Crypto import RSA, BIO

key = """
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvvn357aKesK5kf/sASluITOcb
BNU/r/Hyzjtg/xEr3Cc09h2k+/1noqG5YhSkUXwsAf7okdirwPfQNEMB1yaYaiaC
sdkcQF8ER6bGkHiDGAq9F32Iyojj0ci4zh9LnspOmqazt9FYXujAD12BbiEVv16C
1MgH6tDteRCbZUpLqwIDAQAB
-----END PUBLIC KEY-----
"""
cipher_text = ("oqqioKd5VCldXY43ggx3wJ3E1IxBB8gCVY+3Q++EkGWTZ++XF5BrqUj"
               "+BgTjWtgfgKiQvOc8KpOcWXjKuowhjyVWuSU4dy6DZeKAc78Ni970ydkCI2nzNV4T+pDg25iyC5s7POet54vqCR/mgIf+Rg"
               "/Yww4uVysnAOhMkRbjw3M=")
public_key = RSA.load_pub_key_bio(BIO.MemoryBuffer(key.encode()))
result = public_key.public_decrypt(base64.b64decode(cipher_text), RSA.pkcs1_padding)
key_iv = json.loads(result)
print(key_iv)

Output

{'key': 'wJB76d2hYiMgWoLN', 'iv': 'AE6hdXGgnvHctzyJ'}