quocvuongdn

#Java: #RC4 + Base64

Dec 25th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.security.Key;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.spec.SecretKeySpec;
  4.  
  5. /*...*/
  6.  
  7. public static String encrypt(String key, String clearText) throws Exception{         
  8.     Cipher c = Cipher.getInstance(RC4_NAME);
  9.     Key mainKey = new SecretKeySpec(key.getBytes(), "RC4");
  10.     c.init(Cipher.ENCRYPT_MODE, mainKey);
  11.     byte[] encVal = c.doFinal(clearText.getBytes(UNICODE));        
  12.     String encryptedValue =  Base64.encodeToString(encVal, false);
  13.     return encryptedValue;        
  14. }
  15.  
  16. public static String decrypt(String key, String encryptedData) throws Exception {      
  17.     Cipher c = Cipher.getInstance(RC4_NAME);
  18.     Key mainKey = new SecretKeySpec(key.getBytes(), "RC4");
  19.     c.init(Cipher.DECRYPT_MODE, mainKey);
  20.     byte[] decordedValue = Base64.decode(encryptedData);
  21.     byte[] decValue = c.doFinal(decordedValue);
  22.     String decryptedValue = new String(decValue, UNICODE);        
  23.     return decryptedValue;        
  24. }
Advertisement
Add Comment
Please, Sign In to add comment