This repository was archived by the owner on Apr 20, 2025. It is now read-only.
This repository was archived by the owner on Apr 20, 2025. It is now read-only.
How to convert this piece of Java code into RSA #222
Closed
Description
use rsa, get this error, but java can do this
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
Metadata
Metadata
Assignees
Labels
No labels