· 7 years ago · Jun 26, 2018, 08:24 PM
1package net.merge.dancechallange;
2
3import android.util.Base64;
4
5import java.io.UnsupportedEncodingException;
6import java.security.InvalidKeyException;
7import java.security.NoSuchAlgorithmException;
8import java.security.spec.InvalidKeySpecException;
9
10import javax.crypto.BadPaddingException;
11import javax.crypto.Cipher;
12import javax.crypto.IllegalBlockSizeException;
13import javax.crypto.NoSuchPaddingException;
14import javax.crypto.SecretKey;
15import javax.crypto.SecretKeyFactory;
16import javax.crypto.spec.DESKeySpec;
17
18/**
19 * Created by Noah on 4/24/2018.
20 */
21
22public class managmentApp {
23
24 public static String decryptIt(String value) {
25 try {
26 DESKeySpec keySpec = new DESKeySpec("ä»–åª½çš„ä½ ".getBytes("UTF8"));
27 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
28 SecretKey key = keyFactory.generateSecret(keySpec);
29
30 byte[] encrypedPwdBytes = Base64.decode(value, Base64.DEFAULT);
31 // cipher is not thread safe
32 Cipher cipher = Cipher.getInstance("DES");
33 cipher.init(Cipher.DECRYPT_MODE, key);
34 byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));
35
36 String decrypedValue = new String(decrypedValueBytes);
37 return decrypedValue;
38
39 } catch (InvalidKeyException e) {
40 e.printStackTrace();
41 } catch (UnsupportedEncodingException e) {
42 e.printStackTrace();
43 } catch (InvalidKeySpecException e) {
44 e.printStackTrace();
45 } catch (NoSuchAlgorithmException e) {
46 e.printStackTrace();
47 } catch (BadPaddingException e) {
48 e.printStackTrace();
49 } catch (NoSuchPaddingException e) {
50 e.printStackTrace();
51 } catch (IllegalBlockSizeException e) {
52 e.printStackTrace();
53 }
54 return value;
55 }
56
57 public static String encryptIt(String value) {
58 try {
59 DESKeySpec keySpec = new DESKeySpec("ä»–åª½çš„ä½ ".getBytes("UTF8"));
60 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
61 SecretKey key = keyFactory.generateSecret(keySpec);
62
63 byte[] clearText = value.getBytes("UTF8");
64 // Cipher is not thread safe
65 Cipher cipher = Cipher.getInstance("DES");
66 cipher.init(Cipher.ENCRYPT_MODE, key);
67
68 String encrypedValue = Base64.encodeToString(cipher.doFinal(clearText), Base64.DEFAULT);
69 return encrypedValue;
70
71 } catch (InvalidKeyException e) {
72 e.printStackTrace();
73 } catch (UnsupportedEncodingException e) {
74 e.printStackTrace();
75 } catch (InvalidKeySpecException e) {
76 e.printStackTrace();
77 } catch (NoSuchAlgorithmException e) {
78 e.printStackTrace();
79 } catch (BadPaddingException e) {
80 e.printStackTrace();
81 } catch (NoSuchPaddingException e) {
82 e.printStackTrace();
83 } catch (IllegalBlockSizeException e) {
84 e.printStackTrace();
85 }
86 return value;
87 }
88
89
90}