Advertisement
salron3

Activation Token Demo

Jul 16th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package com.tracker.service.impl;
  2.  
  3. import lombok.extern.slf4j.Slf4j;
  4.  
  5. import javax.crypto.BadPaddingException;
  6. import javax.crypto.Cipher;
  7. import javax.crypto.IllegalBlockSizeException;
  8. import javax.crypto.spec.SecretKeySpec;
  9. import java.security.InvalidKeyException;
  10. import java.security.Key;
  11. import java.time.Instant;
  12. import java.util.Base64;
  13. import java.util.UUID;
  14.  
  15. @Slf4j
  16. public class CryptoExample {
  17.  
  18.     private static Base64.Encoder encoder = Base64.getUrlEncoder();
  19.     private static Base64.Decoder decoder = Base64.getUrlDecoder();
  20.  
  21.     private static final String SIGNING_KEY = "CD1DB294C533DCE6"; // 128 bit key
  22.     private static final Key AES_KEY = new SecretKeySpec(SIGNING_KEY.getBytes(), "AES");
  23.     private static final String SECRET_VALUE = String.format("%s:%s", UUID.randomUUID().toString(), Instant.now().getEpochSecond());
  24.  
  25.     public static void main(String[] args) throws Exception {
  26.         // Create key and cipher
  27.         Cipher cipher = Cipher.getInstance("AES");
  28.  
  29.         // encrypt the text
  30.         String encoded = encrypt(SECRET_VALUE, cipher);
  31.         log.info("Encrypted: {}", encoded);
  32.  
  33.         // decrypt the text
  34.         String decryptedText = decrypt(encoded, cipher);
  35.         log.info("Decrypted: {}",decryptedText);
  36.         log.info("Does decrypted text matches: {}", decryptedText.equals(SECRET_VALUE));
  37.     }
  38.  
  39.     private static String encrypt(String text, Cipher cipher) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
  40.         cipher.init(Cipher.ENCRYPT_MODE, AES_KEY);
  41.         byte[] encrypted = cipher.doFinal(text.getBytes());
  42.         return encoder.encodeToString(encrypted);
  43.     }
  44.  
  45.     private static String decrypt(String encrypted64, Cipher cipher) throws Exception {
  46.         byte[] encrypted = decoder.decode(encrypted64);
  47.         cipher.init(Cipher.DECRYPT_MODE, AES_KEY);
  48.         return new String(cipher.doFinal(encrypted));
  49.     }
  50.  
  51.  
  52.     //=================BouncyCastle=================
  53. /*    public static void main(String[] args) throws Exception {
  54.         Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
  55.  
  56.         Key key = new SecretKeySpec(SIGNING_KEY.getBytes(), "AES");
  57.         Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
  58.  
  59.         log.info(new String(SECRET_VALUE.getBytes()));
  60.  
  61.         // encryption pass
  62.         cipher.init(Cipher.ENCRYPT_MODE, key);
  63.  
  64.         byte[] cipherText = new byte[cipher.getOutputSize(SECRET_VALUE.length())];
  65.         int ctLength = cipher.update(SECRET_VALUE.getBytes(), 0, SECRET_VALUE.length(), cipherText, 0);
  66.         ctLength += cipher.doFinal(cipherText, ctLength);
  67.  
  68.         log.info("Encrypted: {}", encoder.encodeToString(cipherText));
  69.         log.info("ct: {}", ctLength);
  70.  
  71.         // decryption pass
  72.         cipher.init(Cipher.DECRYPT_MODE, key);
  73.         byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
  74.         int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
  75.         ptLength += cipher.doFinal(plainText, ptLength);
  76.  
  77.         log.info("Decrypted: {}", new String(plainText));
  78.         log.info("pt: {}", ptLength);
  79.     }*/
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement