Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. private void calculateSessionKeySupport(PrivateKey y, PublicKey gToTheX) {
  2. try {
  3. // Find g^xy
  4. KeyAgreement serverKeyAgree = KeyAgreement.getInstance("DiffieHellman");
  5. serverKeyAgree.init(y);
  6. serverKeyAgree.doPhase(gToTheX, true);
  7. byte[] secretDH = serverKeyAgree.generateSecret();
  8. if (debug) System.out.println("g^xy: "+byteArrayToHexString(secretDH));
  9. //Use first 16 bytes of g^xy to make an AES key
  10. byte[] aesSecret = new byte[16];
  11. System.arraycopy(secretDH,0,aesSecret,0,16);
  12. Key aesSessionKey = new SecretKeySpec(aesSecret, "AES");
  13. if (debug) System.out.println("Session key: "+byteArrayToHexString(aesSessionKey.getEncoded()));
  14. // Set up Cipher Objects
  15. decAESsessionSupport = Cipher.getInstance("AES");
  16. decAESsessionSupport.init(Cipher.DECRYPT_MODE, aesSessionKey);
  17. encAESsessionSupport = Cipher.getInstance("AES");
  18. encAESsessionSupport.init(Cipher.ENCRYPT_MODE, aesSessionKey);
  19. } catch (NoSuchAlgorithmException e ) {
  20. System.out.println(e);
  21. } catch (InvalidKeyException e) {
  22. System.out.println(e);
  23. } catch (NoSuchPaddingException e) {
  24. e.printStackTrace();
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement