Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. package com.paytm.pg.crypto;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.spec.IvParameterSpec;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import sun.misc.BASE64Decoder;
  6. import sun.misc.BASE64Encoder;
  7.  
  8. public class AesEncryption
  9. implements Encryption
  10. {
  11. private final BASE64Encoder base64Encoder = new BASE64Encoder();
  12. private final BASE64Decoder base64Decoder = new BASE64Decoder();
  13.  
  14. private final byte[] ivParamBytes = { 64, 64, 64, 64,
  15. 38, 38, 38, 38,
  16. 35, 35, 35, 35,
  17. 36, 36, 36, 36 };
  18.  
  19. public AesEncryption() {}
  20.  
  21. public String encrypt(String toEncrypt, String key) throws Exception {
  22. String encryptedValue = "";
  23.  
  24. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING", "SunJCE");
  25. cipher.init(1, new SecretKeySpec(key.getBytes(), "AES"), new IvParameterSpec(ivParamBytes));
  26. encryptedValue = base64Encoder.encode(cipher.doFinal(toEncrypt.getBytes()));
  27. return encryptedValue;
  28. }
  29.  
  30. public String decrypt(String toDecrypt, String key) throws Exception
  31. {
  32. String decryptedValue = "";
  33.  
  34. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING", "SunJCE");
  35. cipher.init(2, new SecretKeySpec(key.getBytes(), "AES"), new IvParameterSpec(ivParamBytes));
  36. decryptedValue = new String(cipher.doFinal(base64Decoder.decodeBuffer(toDecrypt)));
  37. return decryptedValue;
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement