List of usage examples for javax.crypto KeyAgreement getInstance
public static final KeyAgreement getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:MainClass.java
public static void main(String[] args) throws Exception { String s = "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "2F78C7"; BigInteger base = BigInteger.valueOf(2); BigInteger modulus = new BigInteger(s, 16); DHParameterSpec skipParameterSpec = new DHParameterSpec(modulus, base); KeyPairGenerator kpg1 = KeyPairGenerator.getInstance("DH"); kpg1.initialize(skipParameterSpec);//w w w . j a v a2 s . com KeyPair kp1 = kpg1.generateKeyPair(); KeyAgreement ka1 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey1 = (DHPrivateKey) kp1.getPrivate(); DHPublicKey publicKey1 = (DHPublicKey) kp1.getPublic(); ka1.init(privateKey1); System.out.println("1 is using " + publicKey1.getY() + " for its public key"); KeyPairGenerator kpg2 = KeyPairGenerator.getInstance("DH"); kpg2.initialize(skipParameterSpec); KeyPair kp2 = kpg2.generateKeyPair(); KeyAgreement ka2 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey2 = (DHPrivateKey) kp2.getPrivate(); DHPublicKey publicKey2 = (DHPublicKey) kp2.getPublic(); ka2.init(privateKey2); System.out.println("2 is using " + publicKey2.getY() + " for its public key"); ka1.doPhase(publicKey2, true); byte[] sharedKey1 = ka1.generateSecret(); System.out.println("1 is using " + new BigInteger(1, sharedKey1) + " for its shared key"); ka2.doPhase(publicKey1, true); byte[] sharedKey2 = ka2.generateSecret(); System.out.println("2 is using " + new BigInteger(1, sharedKey2) + " for its shared key"); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String s = "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "2F78C7"; BigInteger base = BigInteger.valueOf(2); BigInteger modulous = new BigInteger(s, 16); DHParameterSpec skipParameterSpec = new DHParameterSpec(modulous, base); KeyPairGenerator kpg1 = KeyPairGenerator.getInstance("DH"); kpg1.initialize(skipParameterSpec);//from w w w. j a va 2s. c om KeyPair kp1 = kpg1.generateKeyPair(); KeyAgreement ka1 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey1 = (DHPrivateKey) kp1.getPrivate(); DHPublicKey publicKey1 = (DHPublicKey) kp1.getPublic(); ka1.init(privateKey1); System.out.println("1 is using " + publicKey1.getY() + " for its public key"); KeyPairGenerator kpg2 = KeyPairGenerator.getInstance("DH"); kpg2.initialize(skipParameterSpec); KeyPair kp2 = kpg2.generateKeyPair(); KeyAgreement ka2 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey2 = (DHPrivateKey) kp2.getPrivate(); DHPublicKey publicKey2 = (DHPublicKey) kp2.getPublic(); ka2.init(privateKey2); System.out.println("2 is using " + publicKey2.getY() + "for its public key"); // Use the KeyAgreement object of 1 to generate its shared key ka1.doPhase(publicKey2, true); SecretKey sharedKey1 = ka1.generateSecret("DES"); System.out.println("1 is using " + new String(sharedKey1.getEncoded()) + " as its DES session key"); // Use the KeyAgreement object of 2 to generate its shared key ka2.doPhase(publicKey1, true); SecretKey sharedKey2 = ka2.generateSecret("DES"); System.out.println("2 is using " + new String(sharedKey2.getEncoded()) + "as its DES session key"); }
From source file:com.zf.decipher.DataEn.java
private static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey); PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey); PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm()); keyAgree.init(priKey);//from www . jav a 2s . co m keyAgree.doPhase(pubKey, true); return keyAgree.generateSecret(AES).getEncoded(); }
From source file:com.sshtools.j2ssh.transport.kex.DhGroup1Sha1.java
/** * * * @throws IOException/*from w w w.j a va 2 s . c om*/ * @throws AlgorithmNotSupportedException */ protected void onInit() throws IOException { try { dhKeyPairGen = KeyPairGenerator.getInstance("DH"); dhKeyAgreement = KeyAgreement.getInstance("DH"); } catch (NoSuchAlgorithmException ex) { throw new AlgorithmNotSupportedException(ex.getMessage()); } }
From source file:hudson.cli.Connection.java
public KeyAgreement diffieHellman(boolean side, int keySize) throws IOException, GeneralSecurityException { KeyPair keyPair;/* ww w . ja v a 2s .c o m*/ PublicKey otherHalf; if (side) { AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance("DH"); paramGen.init(keySize); KeyPairGenerator dh = KeyPairGenerator.getInstance("DH"); dh.initialize(paramGen.generateParameters().getParameterSpec(DHParameterSpec.class)); keyPair = dh.generateKeyPair(); // send a half and get a half writeKey(keyPair.getPublic()); otherHalf = KeyFactory.getInstance("DH").generatePublic(readKey()); } else { otherHalf = KeyFactory.getInstance("DH").generatePublic(readKey()); KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DH"); keyPairGen.initialize(((DHPublicKey) otherHalf).getParams()); keyPair = keyPairGen.generateKeyPair(); // send a half and get a half writeKey(keyPair.getPublic()); } KeyAgreement ka = KeyAgreement.getInstance("DH"); ka.init(keyPair.getPrivate()); ka.doPhase(otherHalf, true); return ka; }
From source file:SecureConnection.java
private byte[] getPublicKeyStep2(DHParameterSpec dhParamSpec) throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DH"); keyPairGen.initialize(dhParamSpec);/* w ww. j a va2 s . c o m*/ KeyPair keyPair = keyPairGen.generateKeyPair(); this.keyAgree = KeyAgreement.getInstance("DH"); this.keyAgree.init(keyPair.getPrivate()); return keyPair.getPublic().getEncoded(); }
From source file:org.apache.abdera2.common.security.DHBase.java
public Key generateSecret() throws NoSuchAlgorithmException, InvalidKeyException { KeyAgreement ka = KeyAgreement.getInstance("DH"); ka.init(keyPair.getPrivate());/*from w w w. ja va 2s .c o m*/ ka.doPhase(publicKey, true); return ka.generateSecret("DESede"); }
From source file:org.apache.abdera2.common.security.DHBase.java
public Key generateSecret(String alg) throws NoSuchAlgorithmException, InvalidKeyException { KeyAgreement ka = KeyAgreement.getInstance("DH"); ka.init(keyPair.getPrivate());//from w w w . j av a 2s . c o m ka.doPhase(publicKey, true); return ka.generateSecret(alg); }
From source file:org.apache.geode.internal.cache.tier.sockets.HandShake.java
private Cipher getEncryptCipher(String dhSKAlgo, PublicKey publicKey) throws Exception { try {/* ww w . ja va 2s. c o m*/ if (_encrypt == null) { KeyAgreement ka = KeyAgreement.getInstance("DH"); ka.init(dhPrivateKey); ka.doPhase(publicKey, true); Cipher encrypt; int keysize = getKeySize(dhSKAlgo); int blocksize = getBlockSize(dhSKAlgo); if (keysize == -1 || blocksize == -1) { SecretKey sKey = ka.generateSecret(dhSKAlgo); encrypt = Cipher.getInstance(dhSKAlgo); encrypt.init(Cipher.ENCRYPT_MODE, sKey); } else { String dhAlgoStr = getDhAlgoStr(dhSKAlgo); byte[] sKeyBytes = ka.generateSecret(); SecretKeySpec sks = new SecretKeySpec(sKeyBytes, 0, keysize, dhAlgoStr); IvParameterSpec ivps = new IvParameterSpec(sKeyBytes, keysize, blocksize); encrypt = Cipher.getInstance(dhAlgoStr + "/CBC/PKCS5Padding"); encrypt.init(Cipher.ENCRYPT_MODE, sks, ivps); } _encrypt = encrypt; } } catch (Exception ex) { throw ex; } return _encrypt; }
From source file:org.apache.geode.internal.cache.tier.sockets.HandShake.java
private Cipher getDecryptCipher(String dhSKAlgo, PublicKey publicKey) throws Exception { if (_decrypt == null) { try {/*from w w w . j a v a2s. c o m*/ KeyAgreement ka = KeyAgreement.getInstance("DH"); ka.init(dhPrivateKey); ka.doPhase(publicKey, true); Cipher decrypt; int keysize = getKeySize(dhSKAlgo); int blocksize = getBlockSize(dhSKAlgo); if (keysize == -1 || blocksize == -1) { SecretKey sKey = ka.generateSecret(dhSKAlgo); decrypt = Cipher.getInstance(dhSKAlgo); decrypt.init(Cipher.DECRYPT_MODE, sKey); } else { String algoStr = getDhAlgoStr(dhSKAlgo); byte[] sKeyBytes = ka.generateSecret(); SecretKeySpec sks = new SecretKeySpec(sKeyBytes, 0, keysize, algoStr); IvParameterSpec ivps = new IvParameterSpec(sKeyBytes, keysize, blocksize); decrypt = Cipher.getInstance(algoStr + "/CBC/PKCS5Padding"); decrypt.init(Cipher.DECRYPT_MODE, sks, ivps); } _decrypt = decrypt; } catch (Exception ex) { throw ex; } } return _decrypt; }