Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. public static void main(String args[]) {
  2.  
  3. int max = 9999999999999999;
  4. int min =  1;
  5.  
  6. double random = Math.random() * 49 + 1;
  7. or
  8. int key = (int )(Math.random() * 50 + 1);
  9.  
  10.  
  11.     Test t = new Test();
  12.     String encrypt = new String(t.encrypt("mypassword"));
  13.     System.out.println("decrypted value:" + t.decrypt(key, encrypt));
  14. }
  15.  
  16. public String encrypt(String value) {
  17.     try {
  18.         byte[] raw = new byte[]{'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};
  19.         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  20.         Cipher cipher = Cipher.getInstance("AES");
  21.         cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  22.         byte[] encrypted = cipher.doFinal(value.getBytes());
  23.         System.out.println("encrypted string:" + (new String(encrypted)));
  24.         return new String(skeySpec.getEncoded());
  25.     } catch (NoSuchAlgorithmException ex) {
  26.         Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  27.     } catch (IllegalBlockSizeException ex) {
  28.         Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  29.     } catch (BadPaddingException ex) {
  30.         Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  31.     } catch (InvalidKeyException ex) {
  32.         Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  33.     } catch (NoSuchPaddingException ex) {
  34.         Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  35.     }
  36.     return null;
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43. public String decrypt(String key, String encrypted) {
  44.     try {
  45.         SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
  46.         Cipher cipher = Cipher.getInstance("AES");
  47.         cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(skeySpec.getEncoded(), "AES"));
  48.         //getting error here
  49.         byte[] original = cipher.doFinal(encrypted.getBytes());
  50.         return new String(original);
  51.     } catch (IllegalBlockSizeException ex) {
  52.         Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  53.     } catch (BadPaddingException ex) {
  54.         Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  55.     } catch (InvalidKeyException ex) {
  56.         Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  57.     } catch (NoSuchAlgorithmException ex) {
  58.         Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  59.     } catch (NoSuchPaddingException ex) {
  60.         Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  61.     }
  62.     return null;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement