SHARE
TWEET

re_encrypt.java

a guest Dec 16th, 2018 6 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Base64;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.spec.IvParameterSpec;
  4. import javax.crypto.spec.SecretKeySpec;
  5.  
  6.  
  7. public class Simple
  8. {  
  9.   public static String encrypt(String key, String initVector, String value) {
  10.     try {
  11.       IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
  12.       SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
  13.      
  14.       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
  15.       cipher.init(1, skeySpec, iv);
  16.      
  17.       byte[] encrypted = cipher.doFinal(value.getBytes());
  18.      
  19.       return Base64.getUrlEncoder().encodeToString(encrypted);
  20.     } catch (Exception ex) {
  21.  
  22.     }
  23.    
  24.     return null;
  25.   }
  26.  
  27.   public static String decrypt(String key, String initVector, String encrypted) {
  28.     try {
  29.       IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
  30.       SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
  31.      
  32.       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
  33.       cipher.init(2, skeySpec, iv);
  34.      
  35.       byte[] original = cipher.doFinal(Base64.getUrlDecoder().decode(encrypted));
  36.      
  37.       return new String(original);
  38.     } catch (Exception ex) {
  39.  
  40.     }
  41.    
  42.     return null;
  43.   }
  44. public static void main(String args[]){
  45. System.out.println(Simple.encrypt("c0dehack1nghere1", "0123456789abcdef", "tsug0d"));
  46. }
  47.  
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top