· 7 years ago · Jan 16, 2019, 06:22 PM
1public class RSATest
2 {
3 public static void main(String[] args)
4 {
5 try
6 {
7 if (args[0].equals("-genkey"))
8 {
9 KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
10 SecureRandom random = new SecureRandom();
11 pairgen.initialize(KEYSIZE, random);
12 KeyPair keyPair = pairgen.generateKeyPair();
13 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
14 out.writeObject(keyPair.getPublic());
15 out.close();
16 out = new ObjectOutputStream(new FileOutputStream(args[2]));
17 out.writeObject(keyPair.getPrivate());
18 out.close();
19 }
20 else if (args[0].equals("-encrypt"))
21 {
22 KeyGenerator keygen = KeyGenerator.getInstance("AES");
23 SecureRandom random = new SecureRandom();
24 keygen.init(random);
25 SecretKey key = keygen.generateKey();
26
27 // wrap with RSA public key
28 ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
29 Key publicKey = (Key) keyIn.readObject();
30 keyIn.close();
31
32 Cipher cipher = Cipher.getInstance("RSA");
33 cipher.init(Cipher.WRAP_MODE, publicKey);
34 byte[] wrappedKey = cipher.wrap(key);
35 DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));
36 out.writeInt(wrappedKey.length);
37 out.write(wrappedKey);
38
39 InputStream in = new FileInputStream(args[1]);
40 cipher = Cipher.getInstance("AES");
41 cipher.init(Cipher.ENCRYPT_MODE, key);
42 crypt(in, out, cipher);
43 in.close();
44 out.close();
45 }
46 else
47 {
48 DataInputStream in = new DataInputStream(new FileInputStream(args[1]));
49 int length = in.readInt();
50 byte[] wrappedKey = new byte[length];
51 in.read(wrappedKey, 0, length);
52
53 // unwrap with RSA private key
54 ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
55 Key privateKey = (Key) keyIn.readObject();
56 keyIn.close();
57
58 Cipher cipher = Cipher.getInstance("RSA");
59 cipher.init(Cipher.UNWRAP_MODE, privateKey);
60 Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
61
62 OutputStream out = new FileOutputStream(args[2]);
63 cipher = Cipher.getInstance("AES");
64 cipher.init(Cipher.DECRYPT_MODE, key);
65
66 crypt(in, out, cipher);
67 in.close();
68 out.close();
69 }
70 }
71 catch (IOException e)
72 {
73 e.printStackTrace();
74 }
75 catch (GeneralSecurityException e)
76 {
77 e.printStackTrace();
78 }
79 catch (ClassNotFoundException e)
80 {
81 e.printStackTrace();
82 }
83}
84
85 /**
86 Uses a cipher to transform the bytes in an input stream
87 and sends the transformed bytes to an output stream.
88 @param in the input stream
89 @param out the output stream
90 @param cipher the cipher that transforms the bytes
91*/
92 public static void crypt(InputStream in, OutputStream out,
93 Cipher cipher) throws IOException, GeneralSecurityException
94 {
95 int blockSize = cipher.getBlockSize();
96 int outputSize = cipher.getOutputSize(blockSize);
97 byte[] inBytes = new byte[blockSize];
98 byte[] outBytes = new byte[outputSize];
99
100 int inLength = 0;;
101 boolean more = true;
102 while (more)
103 {
104 inLength = in.read(inBytes);
105 if (inLength == blockSize)
106 {
107 int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
108 out.write(outBytes, 0, outLength);
109 }
110 else more = false;
111 }
112 if (inLength > 0)
113 outBytes = cipher.doFinal(inBytes, 0, inLength);
114 else
115 outBytes = cipher.doFinal();
116 out.write(outBytes);
117 }
118
119 private static final int KEYSIZE = 512;
120 }
121
122
123
124
125
126 this is my login activity
127
128
129
130
131
132
133
134
135
136
137
138
139public class LoginScreen extends Activity implements OnClickListener{
140
141 EditText mobile;
142 EditText pin;
143 Button btnLogin;
144 Button btnClear;
145
146 public void onCreate(Bundle icicle)
147 {
148 super.onCreate(icicle);
149 setContentView(R.layout.login.xml);
150
151TextView lblMobileNo = (TextView)findViewById(R.id.lblMobileNo);
152lblMobileNo.setTextColor(getResources().getColor(R.color.text_color_red));
153
154mobile = (EditText)findViewById(R.id.txtMobileNo);
155
156TextView lblPinNo = (TextView)findViewById(R.id.lblPinNo);
157lblPinNo.setTextColor(getResources().getColor(R.color.text_color_red));
158
159 pin = (EditText)findViewById(R.id.txtPinNo);
160
161 btnLogin = (Button)findViewById(R.id.btnLogin);
162 btnClear = (Button)findViewById(R.id.btnClear);
163
164 btnLogin.setOnClickListener(new OnClickListener() {
165 public void onClick(View view) {
166 postLoginData();
167 }
168 });
169
170
171 btnClear.setOnClickListener(new OnClickListener()
172 {
173 public void onClick(View v)
174
175 {
176 cleartext();
177 }
178
179 });
180
181 /*
182
183btnClear.setOnClickListener(new OnClickListener() {
184 public void onClick(View arg0) {
185
186 }
187 });
188 */
189
190 }
191
192 public void postLoginData()
193
194
195 {
196
197 Intent i = new Intent(this.getApplicationContext(),NEWCLASS.class);
198 Bundle bundle = new Bundle();
199 bundle.putString("mno", mobile.getText().toString());
200 bundle.putString("pinno", pin.getText().toString());
201 i.putExtras(bundle);
202 startActivity(i); }
203 }
204
205 @Override
206 public void onClick(View v) {
207
208 }
209
210
211 public void cleartext() {
212
213
214 {
215 pin.setText("") ;
216 mobile.setText("");
217 }
218
219
220 }
221
222
223 }