×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: digas
Added: Sep 26, 2014 2:43 PM
Views: 21
  1. package io.syntx.examples.security;
  2.  
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.security.InvalidAlgorithmParameterException;
  6. import java.security.InvalidKeyException;
  7. import java.security.Key;
  8. import java.security.NoSuchAlgorithmException;
  9. import java.security.SecureRandom;
  10. import java.sql.SQLException;
  11.  
  12. import javax.crypto.BadPaddingException;
  13. import javax.crypto.Cipher;
  14. import javax.crypto.IllegalBlockSizeException;
  15. import javax.crypto.KeyGenerator;
  16. import javax.crypto.NoSuchPaddingException;
  17. import javax.crypto.spec.IvParameterSpec;
  18.  
  19. import org.apache.commons.codec.binary.Base64;
  20.  
  21. public class SymmetricEncryptionUtility {
  22.  
  23. public final static Key generateKey() throws NoSuchAlgorithmException{
  24.       KeyGenerator kg = KeyGenerator.getInstance("AES");
  25.       SecureRandom random = new SecureRandom();
  26.       kg.init(random);
  27.       return kg.generateKey();
  28. }
  29.  
  30. public static final String encrypt(final String message, final Key key, final IvParameterSpec iv) throws IllegalBlockSizeException,
  31. BadPaddingException, NoSuchAlgorithmException,
  32. NoSuchPaddingException, InvalidKeyException,
  33.  
  34.       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  35.       cipher.init(Cipher.ENCRYPT_MODE,key,iv);
  36.  
  37.       byte[] stringBytes = message.getBytes();
  38.  
  39.       byte[] raw = cipher.doFinal(stringBytes);
  40.  
  41.       return Base64.encodeBase64String(raw);
  42. }
  43.  
  44. public static final String decrypt(final String encrypted,final Key key, final IvParameterSpec iv) throws InvalidKeyException,
  45. NoSuchAlgorithmException, NoSuchPaddingException,
  46. IllegalBlockSizeException, BadPaddingException, IOException, InvalidAlgorithmParameterException {
  47.  
  48.       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  49.       cipher.init(Cipher.DECRYPT_MODE, key,iv);
  50.  
  51.       byte[] raw = Base64.decodeBase64(encrypted);
  52.  
  53.       byte[] stringBytes = cipher.doFinal(raw);
  54.  
  55.       String clearText = new String(stringBytes, "UTF8");
  56.       return clearText;
  57. }
  58.  
  59.  
  60. public static void main(String[] args) throws Exception{
  61.  
  62.       Key k = SymmetricEncryptionUtility.generateKey();
  63.       SecureRandom random = new SecureRandom();
  64.       IvParameterSpec iv = new IvParameterSpec(random.generateSeed(16));
  65.  
  66.       String clearText = "hello world";
  67.       System.out.println("Clear Text:" + clearText);
  68.       String encryptedString = SymmetricEncryptionUtility.encrypt(clearText,k,iv);
  69.       System.out.println("Encrypted String:" + encryptedString);
  70.       System.out.println("Decrypted String:"+SymmetricEncryptionUtility.decrypt(encryptedString,k,iv));
  71.       }
  72.  
  73. }
  74.