-
-
Save ujnak/19e55eec03d480f80cbef326f83bad57 to your computer and use it in GitHub Desktop.
JavaによるRSAの実装
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Base64; | |
import java.security.KeyFactory; | |
import java.security.PublicKey; | |
import java.security.PrivateKey; | |
import java.security.spec.X509EncodedKeySpec; | |
import java.security.spec.PKCS8EncodedKeySpec; | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.OAEPParameterSpec; | |
import java.security.spec.MGF1ParameterSpec; | |
import javax.crypto.spec.PSource; | |
/* | |
* Utility Class for Encrypt/Decrypt with RSA | |
* | |
* How to load Java into schema APEXDEV | |
* loadjava -force -verbose -resolve -u apexdev/****@localhost/xepdb1 RSAUtl.java | |
* | |
* Wrapper Function: | |
-- | |
create or replace function rsautl_encrypt | |
( | |
text varchar2, | |
key varchar2 | |
alg varchar2 // Valid only SHA-256 | |
) return varchar2 | |
as | |
language java name 'RSAUtl.encrypt(java.lang.String, java.lang.String, java.lang.String) return java.lang.String'; | |
/ | |
create or replace function rsautl_decrypt | |
( | |
text varchar2, | |
key varchar2, | |
alg varchar2 // Valid only SHA-256 | |
) return varchar2 | |
as | |
language java name 'RSAUtl.decrypt(java.lang.String, java.lang.String, java.lang.String) return java.lang.String'; | |
/ | |
-- | |
*/ | |
public class RSAUtl { | |
/* | |
* Encryption | |
*/ | |
public static String encrypt(String text, String key, String alg) | |
throws Exception | |
{ | |
MGF1ParameterSpec mgf = new MGF1ParameterSpec(alg); | |
OAEPParameterSpec spec = new OAEPParameterSpec(alg,"MGF1",mgf,PSource.PSpecified.DEFAULT); | |
byte[] data = text.getBytes("UTF-8"); // plain text into bytes | |
byte[] pkdata = Base64.getDecoder().decode(key); // PEM text key into DER | |
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); | |
PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(pkdata)); | |
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding"); | |
cipher.init(Cipher.ENCRYPT_MODE, publicKey, spec); | |
return Base64.getEncoder().encodeToString(cipher.doFinal(data)); | |
} | |
/* | |
* Decryption - PKCS#8 | |
*/ | |
public static String decrypt(String text, String key, String alg) | |
throws Exception | |
{ | |
MGF1ParameterSpec mgf = new MGF1ParameterSpec(alg); | |
OAEPParameterSpec spec = new OAEPParameterSpec(alg,"MGF1",mgf,PSource.PSpecified.DEFAULT); | |
byte[] data = Base64.getDecoder().decode(text); // base64 cipher text into bytes | |
byte[] pkdata = Base64.getDecoder().decode(key); // PEM text key into DER | |
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); | |
PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(pkdata)); | |
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding"); | |
cipher.init(Cipher.DECRYPT_MODE, privateKey, spec); | |
return new String(cipher.doFinal(data),"UTF-8"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment