Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package lab9;
  7. import java.security.Key;
  8. import java.security.KeyPair;
  9. import java.security.KeyPairGenerator;
  10. import java.security.Security;
  11. import java.security.SecureRandom;
  12. import javax.crypto.Cipher;
  13. import java.security.InvalidKeyException;
  14. import java.security.NoSuchAlgorithmException;
  15. import javax.crypto.BadPaddingException;
  16. import javax.crypto.IllegalBlockSizeException;
  17. import javax.crypto.NoSuchPaddingException;
  18. import javax.crypto.ShortBufferException;
  19. import javax.crypto.spec.IvParameterSpec;
  20. import javax.crypto.spec.SecretKeySpec;
  21. import javax.rmi.CORBA.Util;
  22. /**
  23. *
  24. * @author student
  25. */
  26. public class Lab9 {
  27. public static void main(String[] args) throws Exception {
  28. byte[] keyBytes = new byte[16];
  29. byte[] keyBytes2 = new byte[16];
  30. // declare secure PRNG
  31. IvParameterSpec ob = new IvParameterSpec(keyBytes2);
  32. SecureRandom myPRNG = new SecureRandom();
  33. // seed the key
  34. myPRNG.nextBytes(keyBytes);
  35. // build the key from random key bytes
  36. SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
  37. // instantiate AES object for ECB with no padding
  38. Cipher myAES = Cipher.getInstance("AES/ECB/NoPadding");
  39. // initialize AES objecy to encrypt mode
  40. myAES.init(Cipher.ENCRYPT_MODE, myKey,ob);
  41. // initialize plaintext
  42. byte[] plaintext = new byte[16];
  43. //initialize ciphertext
  44. byte[] ciphertext = new byte[16];
  45. // update cipher with the plaintext
  46. int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext, 0);
  47. // process remaining blocks of plaintext
  48. cLength += myAES.doFinal(ciphertext, cLength);
  49. // print plaintext and ciphertext
  50. System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
  51. System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
  52. // initialize AES for decryption
  53. myAES.init(Cipher.DECRYPT_MODE, myKey,ob);
  54. // initialize a new array of bytes to place the decryption
  55. byte[] dec_plaintext = new byte[16];
  56. cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext, 0);
  57. // process remaining blocks of ciphertext
  58. cLength += myAES.doFinal(dec_plaintext, cLength);
  59. // print the new plaintext (hopefully identical to the initial one)
  60. System.out.println("decrypted: " + javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
  61. // get a Cipher instance for RSA with PKCS1 padding
  62. Cipher myRSA = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  63. // get an instance for the Key Generator
  64. KeyPairGenerator myRSAKeyGen = KeyPairGenerator.getInstance("RSA");
  65. // generate an 1024 bit key
  66. myRSAKeyGen.initialize(1024, myPRNG);
  67. KeyPair myRSAKeyPair= myRSAKeyGen.generateKeyPair();
  68. // store the public and private key individually
  69. Key pbKey = myRSAKeyPair.getPublic();
  70. Key pvKey = myRSAKeyPair.getPrivate();
  71. // init cipher for encryption
  72. myRSA.init(Cipher.ENCRYPT_MODE, pbKey, myPRNG);
  73. // encrypt, as expected we encrypt a symmetric key with RSA rather than a file or some longer stream which should be encrypted with AES
  74. ciphertext = myRSA.doFinal(keyBytes);
  75. // init cipher for decryption
  76. myRSA.init(Cipher.DECRYPT_MODE, pvKey);
  77. // decrypt
  78. plaintext = myRSA.doFinal(ciphertext);
  79. System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
  80. System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
  81. System.out.println("keybytes: " + javax.xml.bind.DatatypeConverter.printHexBinary(keyBytes));
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement