· 8 years ago · Jan 13, 2018, 04:16 PM
1import android.os.Bundle;
2import android.support.v7.app.AppCompatActivity;
3import android.util.Base64;
4import android.view.View;
5import android.widget.Button;
6import android.widget.ImageView;
7import android.widget.TextView;
8import android.widget.Toast;
9import java.security.MessageDigest;
10import java.util.Arrays;
11import javax.crypto.Cipher;
12import javax.crypto.spec.SecretKeySpec;
13
14public class aa_test extends AppCompatActivity {
15 ImageView imageView;
16
17 private static String decryptedString;
18 private static String encryptedString;
19 private static byte[] key;
20 private static SecretKeySpec secretKey;
21 Button de_button;
22 TextView decryptTextView;
23 Button en_button;
24 TextView encryptTextView;
25
26
27
28 public static String decrypt(String paramString)
29 {
30 try
31 {
32 Cipher localCipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
33 localCipher.init(2, secretKey);
34 paramString = new String(localCipher.doFinal(Base64.decode(paramString, 0)));
35 return paramString;
36 }
37 catch (Exception e)
38 {
39 System.out.println("Error while decrypting: " + paramString.toString());
40 }
41 return null;
42 }
43
44 public static String encrypt(String paramString)
45 {
46 try
47 {
48 Cipher localCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
49 localCipher.init(1, secretKey);
50 paramString = Base64.encodeToString(localCipher.doFinal(paramString.getBytes("UTF-8")), 0);
51 return paramString;
52
53 }
54 catch (Exception e)
55 {
56 System.out.println("Error while encrypting: " + paramString.toString());
57 }
58 return null;
59 }
60
61
62 @Override
63 protected void onCreate(Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
65 setContentView(R.layout.activity_aa_test);
66
67 this.encryptTextView = ((TextView)findViewById(R.id.textView_encryptTextView));
68 this.decryptTextView = ((TextView)findViewById(R.id.textView_decryptTextView));
69 setKey("pin123");
70
71
72 }
73
74 public void setKey(String paramString)
75 {
76 try
77 {
78 key = paramString.getBytes("UTF-8");
79 System.out.println(key.length);
80 key = MessageDigest.getInstance("SHA-1").digest(key);
81 key = Arrays.copyOf(key, 16);
82 System.out.println(key.length);
83 System.out.println(new String(key, "UTF-8"));
84 secretKey = new SecretKeySpec(key, "AES");
85 return;
86 }
87 catch (Exception e)
88 {
89 //----
90 return;
91 }
92
93 }
94
95 public void Encrypt_String(View view) {
96 String paramAnonymousView = encryptTextView.getText().toString();
97 if (paramAnonymousView.length() > 0){
98 decryptTextView.setText(encrypt(paramAnonymousView));
99 }else{Toast.makeText(aa_test.this.getApplicationContext(), "Please enter the value", Toast.LENGTH_LONG).show();}
100 }
101
102 public void Decrypt_String(View view) {
103 String paramAnonymousView = encryptTextView.getText().toString();
104 if (paramAnonymousView.length() > 0){
105 decryptTextView.setText(decrypt(paramAnonymousView));
106 }else{Toast.makeText(aa_test.this.getApplicationContext(), "Please enter the value", Toast.LENGTH_LONG).show();}
107 }
108}
109
110<?xml version="1.0" encoding="utf-8"?>
111<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
112xmlns:app="http://schemas.android.com/apk/res-auto"
113xmlns:tools="http://schemas.android.com/tools"
114android:layout_width="match_parent"
115android:layout_height="match_parent"
116android:paddingBottom="@dimen/activity_vertical_margin"
117android:paddingLeft="@dimen/activity_horizontal_margin"
118android:paddingRight="@dimen/activity_horizontal_margin"
119android:paddingTop="@dimen/activity_vertical_margin"
120android:orientation="vertical"
121tools:context="com.test.test.aa_test">
122
123<TextView
124 android:text="Hello"
125 android:textSize="10pt"
126 android:layout_width="match_parent"
127 android:layout_height="wrap_content"
128 android:id="@+id/textView_encryptTextView"
129 android:gravity="center" />
130
131<TextView
132 android:text="- - - -"
133
134 android:textSize="10pt"
135 android:layout_width="match_parent"
136 android:layout_height="wrap_content"
137 android:id="@+id/textView_decryptTextView"
138 android:gravity="center" />
139
140<Button
141 android:text="Encrypt"
142 android:onClick="Encrypt_String"
143 android:layout_width="match_parent"
144 android:layout_height="wrap_content"
145 android:id="@+id/en_button" />
146
147<Button
148 android:text="Decrypt"
149 android:onClick="Decrypt_String"
150 android:layout_width="match_parent"
151 android:layout_height="wrap_content"
152 android:id="@+id/de_button" />
153</LinearLayout>