「Java」秘密鍵/公開鍵で暗復号する
秘密鍵/公開鍵を使って暗復号するコード書いてみました。その時のメモです。
■ CipherTest.java
import java.io.FileInputStream; import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; import java.security.cert.X509Certificate; import javax.crypto.Cipher; public class CipherTest { private static PrivateKey privateKey; private static PublicKey publicKey; public static void main(String[] args) throws Exception { getKeyPair(); String s = "ABCDE"; byte[] encryptData = encrypt(s); byte[] decryptData = decrypt(encryptData); System.out.print("encryptData : "); printEncryptData(encryptData); System.out.println("decryptData : " + new String(decryptData)); } // 暗号 private static byte[] encrypt(String data) throws Exception { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(data.getBytes()); } // 復号 private static byte[] decrypt(byte[] data) throws Exception { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, publicKey); return cipher.doFinal(data); } // encryptData の表示用メソッド public static void printEncryptData(byte[] b) { for (int i = 0; i < b.length; i++) { String h = Integer.toHexString(b[i] & 0xff); System.out.print(h + " "); // System.out.format("%02x ", b[i]); } System.out.println(); } private static void getKeyPair() throws Exception { ・・・・・ }
※ getKeyPair() は、以下のエントリーで書いたキーストアから秘密鍵/公開鍵を取得する処理です。
・「Java」キーストアから秘密鍵/公開鍵を取得する - プログラム日記
http://a4dosanddos.hatenablog.com/entry/2014/02/08/144856
■ 実行結果
encryptData : 63 1f ba ae 91 cc 56 4b 75 c3 65 2e 14 73 d5 ad aa 89 14 c9 49 f1 5d 62 c9 be 8b 88 7f 0 ea fa 74 b1 17 83 40 c de 37 43 68 cd ce ab bd d2 d9 af 2a 86 b6 6 31 97 2d a6 8c f5 17 e 25 45 95 2e 1f c2 99 c5 b5 f4 6b 40 a5 63 c0 ad bc e1 1 cf 9d ad 7a 6e 82 51 be d3 ce e3 b7 d5 61 25 77 b1 52 51 dd 50 40 3b 57 42 98 e8 39 77 ba b1 1a a4 49 f3 2b a5 39 40 31 99 1d 19 17 71 12 c7 fc b8 5f 71 70 da e6 d3 51 3a c0 e 63 21 de 19 ef 93 70 c7 25 a4 26 8c 4 ee 35 69 d 4 a5 77 16 5b be c5 e5 e9 5c e6 12 bb 97 e0 b6 53 8e 46 dd 45 36 99 bf 31 86 f8 79 bd f5 e9 c8 f2 b6 28 7 77 72 b8 8e 43 9e 54 94 1d 13 38 0 49 cf b7 52 cc e8 af 18 d0 b4 18 b5 eb 72 e1 2a 3e e0 d7 d5 88 b0 7c d1 83 5 60 55 bb 68 43 47 f5 94 a9 28 85 71 2d 24 d0 26 6f 3 ff ed bf 8f b5 7 c8 c8 decryptData : ABCDE
ちゃんと暗復号できておりますかね。
上記は、キーストアから取得した鍵で暗復号してますが、以下の感じでコード内で鍵を生成してもいいですね。
private static void getKeyPair2() throws Exception { KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA"); kg.initialize(2048); KeyPair keyPair = kg.generateKeyPair(); KeyFactory factoty = KeyFactory.getInstance("RSA"); RSAPublicKeySpec publicKeySpec = factoty.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class); RSAPrivateKeySpec privateKeySpec = factoty.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class); publicKey = factoty.generatePublic(publicKeySpec); privateKey = factoty.generatePrivate(privateKeySpec); }
java.security パッケージの API ドキュメントは以下です。
・java.security (Java Platform SE 7 )
http://docs.oracle.com/javase/jp/7/api/java/security/package-summary.html
以上です。