Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import javax.crypto.Cipher
  2. import javax.crypto.KeyGenerator
  3. import java.security.Key
  4.  
  5.  
  6. String input = "This is PlainText!"
  7.  
  8. println "==>> Plain text: ${input} <<=="
  9.  
  10. String algorithm = "DESede"
  11. Cipher cipher = Cipher.getInstance(algorithm)
  12. Key generatedKey = KeyGenerator.getInstance(algorithm).generateKey()
  13. String encodedGeneratedKey = generatedKey.encoded.encodeBase64()
  14.  
  15. println "==>> Base64 encoded symkey: ${encodedGeneratedKey} <<=="
  16.  
  17. cipher.init(Cipher.ENCRYPT_MODE, generatedKey)
  18. byte[] cipherText = cipher.doFinal(input.bytes)
  19.  
  20. println "==>> Ciphertext: ${new String(cipherText)} <<=="
  21.  
  22. cipher.init(Cipher.DECRYPT_MODE, generatedKey)
  23. String decrypted = new String(cipher.doFinal(cipherText))
  24.  
  25. println "==>> Decrypted text: ${decrypted} <<=="
  26.  
  27. assert input == decrypted
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement