This repository was archived by the owner on Apr 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
How to convert this piece of Java code into RSA #222
Comments
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. |
@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 codeimport 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);
}
} |
I solved this problem with 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
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
use rsa, get this error, but java can do this
java
this is result
The text was updated successfully, but these errors were encountered: