openssl genrsa -out private_key.pem
openssl rsa -pubout -in private_key.pem -out public_key.pem
openssl rsa -inform pem -outform der –pubin -in public_key.pem -out public_key.der
openssl rsa -inform pem -outform der -in public_key.pem -pubin -out public_key.der
(-pubin オプションの指定場所が Linux/Windows で異なるため注意要)
(-pubin オプションの指定場所が Linux/Windows で異なるため注意要)
openssl pkcs8 -topk8 -in private_key.pem -inform pem –nocrypt -out private_key.der -outform der
openssl pkcs8 -topk8 -in private_key.pem -inform pem -out private_key.der -outform der -nocrypt
(-nocrypt オプションの指定場所が Linux/Windows で異なるため注意要. パスワードを尋ねられたら -nocypt オプションは無効状態)
1.Java公開鍵暗号化
//path -> 公開鍵のパス
File keyFile = new File(path);
byte[] encodedKey = new byte[(int)keyFile.length()];
FileInputStream in = new FileInputStream(keyFile);
in.read(encodedKey);
in.close();
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey pubKey = kf.generatePublic(publicKeySpec);
Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherText = rsa.doFinal(plainTest);
//path -> 秘密鍵のパス
File keyFile = new File(path);
File keyFile = new File(path);
byte[] encodedKey = new byte[(int)keyFile.length()];
new FileInputStream(keyFile).read(encodedKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedKey);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.DECRYPT_MODE, privateKey);
byte[] plainText = rsa.doFinal(cipherText);
0 件のコメント:
コメントを投稿