· 9 years ago · Jan 30, 2017, 07:06 AM
1import android.util.Base64;
2
3import java.security.InvalidKeyException;
4import java.security.NoSuchAlgorithmException;
5
6import javax.crypto.BadPaddingException;
7import javax.crypto.Cipher;
8import javax.crypto.IllegalBlockSizeException;
9import javax.crypto.NoSuchPaddingException;
10import javax.crypto.spec.IvParameterSpec;
11import javax.crypto.spec.SecretKeySpec;
12import javax.inject.Inject;
13
14/**
15 * @author Created by Ahmed Mabrook , a.mabrok@yellow.com.eg on 9/15/15.
16 * @package com.yellow.eg.Utils
17 * @className MCrypt
18 * <br/> this class is used for encryption and decryption of any url
19 **/
20public class MCrypt {
21
22 private String iv = "m*w&h+e!p$X#b$h!";
23 private IvParameterSpec ivspec;
24 private SecretKeySpec keyspec;
25 private Cipher cipher;
26 private String mSecretKey = "Y@@LL00WK#Y**\0\0\0";
27 static private byte[] mNewKeyBytes;
28
29
30 @Inject
31 public MCrypt() {
32 ivspec = new IvParameterSpec(iv.getBytes());
33
34 try {
35 cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
36 } catch (NoSuchAlgorithmException e) {
37 e.printStackTrace();
38 } catch (NoSuchPaddingException e) {
39 e.printStackTrace();
40 }
41 }
42
43 public String encrypt(String text) {
44 return encrypt(text, mSecretKey);
45
46 }
47
48 public byte[] decrypt(String code) throws Exception {
49 if (code == null || code.length() == 0)
50 return null;
51
52 byte[] decrypted = null;
53
54 try {
55 cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
56
57 decrypted = cipher.doFinal(hexToBytes(code));
58 } catch (Exception e) {
59 throw new Exception("[decrypt] " + e.getMessage());
60 }
61 return decrypted;
62 }
63
64 public byte[] hexToBytes(String str) {
65 if (str == null) {
66 return null;
67 } else if (str.length() < 2) {
68 return null;
69 } else {
70 int len = str.length() / 2;
71 byte[] buffer = new byte[len];
72 for (int i = 0; i < len; i++) {
73 buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
74 }
75 return buffer;
76 }
77 }
78
79
80 /**
81 * @param input
82 * @param key
83 * @return String encrypted
84 */
85 public static String encrypt(String input, String key) {
86
87 byte[] crypted = null;
88
89 SecretKeySpec SecretKey = new SecretKeySpec(key.getBytes(), "AES");
90
91 try {
92 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
93 cipher.init(Cipher.ENCRYPT_MODE, SecretKey);
94 crypted = cipher.doFinal(input.getBytes());
95 } catch (InvalidKeyException e) {
96 e.printStackTrace();
97 } catch (IllegalBlockSizeException e) {
98 e.printStackTrace();
99 } catch (BadPaddingException e) {
100 e.printStackTrace();
101 } catch (NoSuchAlgorithmException e) {
102 e.printStackTrace();
103 } catch (NoSuchPaddingException e) {
104 e.printStackTrace();
105 }
106
107
108 String encrypted = Base64.encodeToString(crypted, Base64.DEFAULT);
109 return encrypted.replace('+', '-');
110
111
112 }
113
114
115 public static byte[] decrypt(String input, String key) {
116 byte[] output = null;
117 try {
118 SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
119 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
120 cipher.init(Cipher.DECRYPT_MODE, skey);
121 //output = cipher.doFinal(Base64.decodeBase64(input.getBytes()));
122 } catch (Exception e) {
123 System.out.println(e.toString());
124 }
125 return output; //new String(output);
126 }
127}