· 8 years ago · Dec 04, 2017, 05:08 AM
1package com.af2g.treinus.utils;
2
3import android.content.Context;
4import android.provider.Settings;
5import android.util.Base64;
6
7import java.security.Key;
8import java.security.NoSuchAlgorithmException;
9import java.security.SecureRandom;
10
11import javax.crypto.Cipher;
12import javax.crypto.KeyGenerator;
13import javax.crypto.SecretKey;
14import javax.crypto.SecretKeyFactory;
15import javax.crypto.spec.IvParameterSpec;
16import javax.crypto.spec.PBEKeySpec;
17import javax.crypto.spec.PBEParameterSpec;
18import javax.crypto.spec.SecretKeySpec;
19
20//import Decoder.BASE64Encoder;
21//import Decoder.BASE64Decoder;
22//import Decoder.BASE64Encoder;
23
24public class Cripto {
25
26 private static final String CRIPT = "ASdalkd34mofmoef0fjnflsnfd943nt" ;
27
28 public static String Encrypt(String raw) throws Exception {
29 Cipher c = getCipher(Cipher.ENCRYPT_MODE);
30
31 byte[] encryptedVal = c.doFinal(raw.getBytes("UTF-8"));
32 return Base64.encodeToString(encryptedVal, Base64.DEFAULT);
33 }
34
35 public static Cipher getCipher(int mode) throws Exception {
36 Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
37
38 //a random Init. Vector. just for testing
39 //byte[] iv = "e675f725e675f725".getBytes("UTF-8");
40
41 byte[] iv = {12, 34, 56, 78, 90, 102, 114, 126, 12, 34, 56, 78, 90, 102, 114, 126};
42
43 c.init(mode, generateKey(), new IvParameterSpec(iv));
44 return c;
45 }
46
47 public static String Decrypt(String encrypted) throws Exception {
48
49 byte[] decodedValue = Base64.decode(encrypted, Base64.DEFAULT);
50
51 Cipher c = getCipher(Cipher.DECRYPT_MODE);
52 byte[] decValue = c.doFinal(decodedValue);
53
54 return new String(decValue);
55 }
56
57 private static Key generateKey() throws Exception {
58
59 byte[] encoded = "tv93h58sk1zh5x8v".getBytes("UTF-8");
60 SecretKeySpec specs = new SecretKeySpec(encoded, "AES");
61
62 System.out.println(specs.getEncoded());
63 return specs;
64 }
65
66 public static String criptString(String string){
67 Cripto c = new Cripto();
68 String paramsCrip = null;
69 try {
70 paramsCrip = c.Encrypt(string);
71 } catch (Exception e1) {
72 e1.printStackTrace();
73 }
74 return paramsCrip;
75 }
76
77 public static String decriptString(String string){
78 Cripto c = new Cripto();
79 String paramsCrip = null;
80 try {
81 paramsCrip = c.Decrypt(string);
82 } catch (Exception e1) {
83 e1.printStackTrace();
84 }
85 return paramsCrip;
86 }
87
88
89 public static SecretKey newGenerateKey() throws NoSuchAlgorithmException {
90 // Generate a 256-bit key
91 final int outputKeyLength = 256;
92
93 SecureRandom secureRandom = new SecureRandom();
94 // Do *not* seed secureRandom! Automatically seeded from system entropy.
95 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
96 keyGenerator.init(outputKeyLength, secureRandom);
97 SecretKey key = keyGenerator.generateKey();
98 return key;
99 }
100
101
102
103 public static String encryptNew(Context context, String value ) {
104
105 try {
106 final byte[] bytes = value!=null ? value.getBytes("utf-8") : new byte[0];
107 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
108 SecretKey key = keyFactory.generateSecret(new PBEKeySpec(CRIPT.toCharArray()));
109 Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
110 pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(Settings.Secure.getString(context.getContentResolver(), Settings.System.ANDROID_ID).getBytes("utf-8"), 20));
111 return new String(Base64.encode(pbeCipher.doFinal(bytes), Base64.NO_WRAP),"utf-8");
112
113 } catch( Exception e ) {
114 throw new RuntimeException(e);
115 }
116
117 }
118
119 public static String decryptNew(Context context,String value){
120 try {
121 final byte[] bytes = value!=null ? Base64.decode(value,Base64.DEFAULT) : new byte[0];
122 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
123 SecretKey key = keyFactory.generateSecret(new PBEKeySpec(CRIPT.toCharArray()));
124 Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
125 pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(Settings.Secure.getString(context.getContentResolver(),Settings.System.ANDROID_ID).getBytes("utf-8"), 20));
126 return new String(pbeCipher.doFinal(bytes),"utf-8");
127
128 } catch( Exception e) {
129 throw new RuntimeException(e);
130 }
131 }
132
133}