Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.math.BigInteger;
  2. import java.security.NoSuchAlgorithmException;
  3. import java.util.Arrays;
  4.  
  5. import javax.crypto.Cipher;
  6. import javax.crypto.KeyGenerator;
  7. import javax.crypto.NoSuchPaddingException;
  8. import javax.crypto.SecretKey;
  9. import javax.crypto.spec.SecretKeySpec;
  10.  
  11.  
  12.  
  13. import sun.misc.Cache;
  14.  
  15. public class cipher {
  16.    
  17.     private String key;
  18.     private SecretKeySpec keySpec;
  19.     private Cipher cipher;
  20.    
  21.     public void setSecretKey(String key)
  22.     {
  23.         this.key = key;
  24.     }
  25.     public void setAlgorithm(String algorithm)
  26.     {
  27.         byte[] raw = stringToBytes(key);
  28.         keySpec = new SecretKeySpec(raw , algorithm);
  29.         try
  30.         {
  31.             cipher = Cipher.getInstance(algorithm);
  32.         }
  33.         catch(NoSuchAlgorithmException e)
  34.         {
  35.             e.printStackTrace();
  36.         }
  37.         catch(NoSuchPaddingException e)
  38.         {
  39.             e.printStackTrace();
  40.         }
  41.     }
  42.    
  43.     public String encrypt(String str)
  44.     {
  45.         try
  46.         {
  47.             byte[] encrypted = null;
  48.             synchronized (cipher)
  49.             {
  50.                 cipher.init(Cipher, ENCRYPT_MODE,keySpec);
  51.                 encrypted = cipher.doFinal(str,getBytes());
  52.             }
  53.             return new String(Hex,encodeHex(encrypted));
  54.         }
  55.         catch(Exception e)
  56.         {
  57.             throw new RuntimeException("encryption Fail",e);
  58.         }
  59.     }
  60.     public String decrypt(String str)
  61.     {
  62.         try
  63.         {
  64.             byte[] decrypted = null;
  65.             synchronized (cipher)
  66.             {
  67.                 cipher.init(Cipher, DECRYPT_MODE,keySpec);
  68.                 decrypted = cipher.doFinal(str,getBytes());
  69.             }
  70.             return new String(Hex,encodeHex(decrypted));
  71.         }
  72.         catch(Exception e)
  73.         {
  74.             throw new RuntimeException("decryption Fial",e);
  75.         }
  76.     }
  77.    
  78.     public static String generateRandomSecretKey(String algorithm) throws Exception
  79.     {
  80.         KeyGenerator KeyGen = KeyGenerator.getInstance(algorithm);
  81.         KeyGen.init(128);
  82.         SecretKey key = KeyGen.generateKey();
  83.         byte[] raw  = key.getEncoded();
  84.         return bytesToString(raw);
  85.     }
  86.    
  87.     private static String bytesToString(byte[] bytes)
  88.     {
  89.         byte[] b2 = new byte[bytes.length+1];
  90.         b2[0] = 1;
  91.         System.arraycopy(bytes, 0, b2, 1, bytes.length);
  92.         return new BigInteger(b2).toString(Character.MAX_RADIX);
  93.     }
  94.    
  95.     private static byte[] stringToBytes(String str)
  96.     {
  97.         byte[] bytes = new BigInteger(str,Character.MAX_RADIX).toByteArray();
  98.         return Arrays.copyOfRange(bytes, 1, bytes.length);
  99.     }
  100.     public static void main(String args[]) throws Exception
  101.     {
  102.         String key = generateRandomSecretKey("AES");
  103.         System.out.println(key);
  104.         cipher cs = new cipher();
  105.         cs.setSecretKey(key);
  106.         cs.setAlgorithm("AES");
  107.         String encStr = cs.encrypt("This is Java Class");
  108.         System.out.println(encStr);
  109.         String decStr = cs.decrypt(encStr);
  110.         System.out.println(decStr);
  111.     }
  112.    
  113.    
  114.  
  115. }