1 21 22 package net.sourceforge.jcetaglib.lib; 23 24 import org.bouncycastle.jce.provider.BouncyCastleProvider; 25 26 import javax.crypto.KeyAgreement; 27 import javax.crypto.spec.DHParameterSpec; 28 import javax.crypto.spec.SecretKeySpec; 29 import java.math.BigInteger ; 30 import java.security.*; 31 import java.security.spec.InvalidKeySpecException ; 32 import java.security.spec.X509EncodedKeySpec ; 33 34 42 public class DHKeyAgreement { 43 53 private static final byte SKIP_1024_MODULUS_BYTES[] = { 54 (byte) 0xF4, (byte) 0x88, (byte) 0xFD, (byte) 0x58, 55 (byte) 0x4E, (byte) 0x49, (byte) 0xDB, (byte) 0xCD, 56 (byte) 0x20, (byte) 0xB4, (byte) 0x9D, (byte) 0xE4, 57 (byte) 0x91, (byte) 0x07, (byte) 0x36, (byte) 0x6B, 58 (byte) 0x33, (byte) 0x6C, (byte) 0x38, (byte) 0x0D, 59 (byte) 0x45, (byte) 0x1D, (byte) 0x0F, (byte) 0x7C, 60 (byte) 0x88, (byte) 0xB3, (byte) 0x1C, (byte) 0x7C, 61 (byte) 0x5B, (byte) 0x2D, (byte) 0x8E, (byte) 0xF6, 62 (byte) 0xF3, (byte) 0xC9, (byte) 0x23, (byte) 0xC0, 63 (byte) 0x43, (byte) 0xF0, (byte) 0xA5, (byte) 0x5B, 64 (byte) 0x18, (byte) 0x8D, (byte) 0x8E, (byte) 0xBB, 65 (byte) 0x55, (byte) 0x8C, (byte) 0xB8, (byte) 0x5D, 66 (byte) 0x38, (byte) 0xD3, (byte) 0x34, (byte) 0xFD, 67 (byte) 0x7C, (byte) 0x17, (byte) 0x57, (byte) 0x43, 68 (byte) 0xA3, (byte) 0x1D, (byte) 0x18, (byte) 0x6C, 69 (byte) 0xDE, (byte) 0x33, (byte) 0x21, (byte) 0x2C, 70 (byte) 0xB5, (byte) 0x2A, (byte) 0xFF, (byte) 0x3C, 71 (byte) 0xE1, (byte) 0xB1, (byte) 0x29, (byte) 0x40, 72 (byte) 0x18, (byte) 0x11, (byte) 0x8D, (byte) 0x7C, 73 (byte) 0x84, (byte) 0xA7, (byte) 0x0A, (byte) 0x72, 74 (byte) 0xD6, (byte) 0x86, (byte) 0xC4, (byte) 0x03, 75 (byte) 0x19, (byte) 0xC8, (byte) 0x07, (byte) 0x29, 76 (byte) 0x7A, (byte) 0xCA, (byte) 0x95, (byte) 0x0C, 77 (byte) 0xD9, (byte) 0x96, (byte) 0x9F, (byte) 0xAB, 78 (byte) 0xD0, (byte) 0x0A, (byte) 0x50, (byte) 0x9B, 79 (byte) 0x02, (byte) 0x46, (byte) 0xD3, (byte) 0x08, 80 (byte) 0x3D, (byte) 0x66, (byte) 0xA4, (byte) 0x5D, 81 (byte) 0x41, (byte) 0x9F, (byte) 0x9C, (byte) 0x7C, 82 (byte) 0xBD, (byte) 0x89, (byte) 0x4B, (byte) 0x22, 83 (byte) 0x19, (byte) 0x26, (byte) 0xBA, (byte) 0xAB, 84 (byte) 0xA2, (byte) 0x5E, (byte) 0xC3, (byte) 0x55, 85 (byte) 0xE9, (byte) 0x2F, (byte) 0x78, (byte) 0xC7 86 }; 87 88 91 private static final BigInteger MODULUS = new BigInteger 92 (1, SKIP_1024_MODULUS_BYTES); 93 94 97 private static final BigInteger BASE = BigInteger.valueOf(2); 98 99 102 private static final DHParameterSpec PARAMETER_SPEC = 103 new DHParameterSpec(MODULUS, BASE); 104 105 113 public static KeyPair generateDHKeyPair() 114 throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { 115 116 Security.addProvider(new BouncyCastleProvider()); 117 118 KeyPairGenerator g = KeyPairGenerator.getInstance("DH", "BC"); 119 g.initialize(PARAMETER_SPEC); 120 121 return g.generateKeyPair(); 122 } 123 124 135 public static Key generateSessionKey(PublicKey pubkey 136 , PrivateKey privkey 137 , String algorithm 138 , int length) throws NoSuchAlgorithmException, InvalidKeyException { 139 140 Security.addProvider(new BouncyCastleProvider()); 141 142 KeyAgreement ka = KeyAgreement.getInstance("DH"); 143 ka.init(privkey); 144 ka.doPhase(pubkey, true); 145 146 byte[] sessionKeyBytes = ka.generateSecret(); 147 148 byte[] newBytes = new byte[length / 8]; 150 System.arraycopy(sessionKeyBytes, 0, newBytes, 0, length / 8); 151 152 Key sessionKey = new SecretKeySpec(newBytes, algorithm); 153 154 Clean.blank(sessionKeyBytes); 155 Clean.blank(newBytes); 156 157 return sessionKey; 158 } 159 160 166 public static byte[] publicKeyToBytes(PublicKey pub) { 167 return pub.getEncoded(); 168 } 169 170 178 public static PublicKey bytesToPublicKey(byte[] keyBytes) 179 throws NoSuchAlgorithmException, InvalidKeySpecException { 180 KeyFactory kf = KeyFactory.getInstance("DH"); 181 X509EncodedKeySpec x509Spec = new X509EncodedKeySpec (keyBytes); 182 return kf.generatePublic(x509Spec); 183 } 184 } 185 | Popular Tags |