|
| 1 | +package jp.jbxl; |
| 2 | + |
| 3 | +import java.security.*; |
| 4 | +import java.security.spec.*; |
| 5 | +import javax.crypto.*; |
| 6 | +import javax.crypto.spec.*; |
| 7 | +import javax.crypto.interfaces.*; |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * Diffie-Hellman鍵交換法 クライアント |
| 12 | + * @author Fumi.Iseki |
| 13 | + * @version 1.0 |
| 14 | + */ |
| 15 | +public class DHCrypt |
| 16 | +{ |
| 17 | + |
| 18 | + private KeyPairGenerator myKeyPairGen; |
| 19 | + private KeyFactory myKeyFac; |
| 20 | + private KeyPair myKeyPair; |
| 21 | + private PublicKey serverPubKey; |
| 22 | + private KeyAgreement myKeyAgree; |
| 23 | + private byte[] mySharedSecret = null; |
| 24 | + |
| 25 | + /** |
| 26 | + * このオブジェクトの Subject Public Key Info (DER形式) <br> |
| 27 | + * dhClient() 実行後でないと,有効な値を得られない. |
| 28 | + */ |
| 29 | + public byte[] myPubKey = null; |
| 30 | + |
| 31 | + /** |
| 32 | + * Base64でエンコードされた myPubKey <br> |
| 33 | + * dhClient() 実行後でないと,有効な値を得られない. |
| 34 | + */ |
| 35 | + public String myPubKeyEnc = null; |
| 36 | + |
| 37 | + |
| 38 | + /** |
| 39 | + * コンストラクタ.状態を初期化する. |
| 40 | + */ |
| 41 | + public DHCrypt() |
| 42 | + { |
| 43 | + try { |
| 44 | + myKeyFac = KeyFactory.getInstance("DiffieHellman"); |
| 45 | + myKeyPairGen = KeyPairGenerator.getInstance("DiffieHellman"); |
| 46 | + myKeyAgree = KeyAgreement.getInstance("DiffieHellman"); |
| 47 | + } |
| 48 | + catch (Exception e) { |
| 49 | + e.printStackTrace(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * サーバの SPKI(DER形式)から自分の SPKI(DER形式)を生成して返す.また,共通鍵 mySharedSecret も計算する. |
| 57 | + * |
| 58 | + * @param serverKeyEnc サーバの Subject Public Key Info (DER) |
| 59 | + * @return このオブジェクトの Subject Public Key Info (DER) |
| 60 | + */ |
| 61 | + public byte[] dhClient(byte[] serverKeyEnc) |
| 62 | + { |
| 63 | + myPubKey = null; |
| 64 | + myPubKeyEnc = null; |
| 65 | + mySharedSecret = null; |
| 66 | + |
| 67 | + try { |
| 68 | + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(serverKeyEnc); |
| 69 | + serverPubKey = myKeyFac.generatePublic(x509KeySpec); |
| 70 | + DHParameterSpec dhParamSpec = ((DHPublicKey)serverPubKey).getParams(); |
| 71 | + |
| 72 | + myKeyPairGen.initialize(dhParamSpec); |
| 73 | + myKeyPair = myKeyPairGen.generateKeyPair(); |
| 74 | + myKeyAgree.init(myKeyPair.getPrivate()); |
| 75 | + myKeyAgree.doPhase(serverPubKey, true); |
| 76 | + |
| 77 | + byte[] tmpSharedSecret = new byte[256]; // for 2048 bit key |
| 78 | + int myLen = myKeyAgree.generateSecret(tmpSharedSecret, 0); |
| 79 | + mySharedSecret = new byte[myLen]; |
| 80 | + for (int i=0; i<myLen; i++) mySharedSecret[i] = tmpSharedSecret[i]; |
| 81 | + tmpSharedSecret = null; |
| 82 | + |
| 83 | + myPubKey = myKeyPair.getPublic().getEncoded(); |
| 84 | + myPubKeyEnc = Base64.encode(myPubKey); |
| 85 | + //System.err.println("SCRT = "+Tools.byteArray_toHex(mySharedSecret)); |
| 86 | + } |
| 87 | + catch (Exception e) { |
| 88 | + e.printStackTrace(); |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + return myPubKey; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + /** |
| 98 | + * DES(ECB) での暗号化.テスト用.<br> |
| 99 | + * 要動作確認 |
| 100 | + * |
| 101 | + * @param data 暗号化するバイト列. |
| 102 | + * @return 暗号化されたバイト列. |
| 103 | + */ |
| 104 | + public byte[] dhCrypt(byte[] data) |
| 105 | + { |
| 106 | + byte[] cipher = null; |
| 107 | + |
| 108 | + try { |
| 109 | + myKeyAgree.doPhase(serverPubKey, true); |
| 110 | + SecretKey myDesKey = myKeyAgree.generateSecret("DES"); |
| 111 | + |
| 112 | + Cipher myCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); |
| 113 | + myCipher.init(Cipher.ENCRYPT_MODE, myDesKey); |
| 114 | + cipher = myCipher.doFinal(data); |
| 115 | + } |
| 116 | + catch (Exception e) { |
| 117 | + e.printStackTrace(); |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + return cipher; |
| 122 | + } |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | + |
0 commit comments