· 8 years ago · Sep 26, 2017, 06:12 AM
1import java.io.*;
2import javax.crypto.*;
3import java.util.*;
4import javax.crypto.spec.*;
5
6public class test
7{
8 public static void main( String[] args )
9 {
10 new test();
11 }
12
13 public String user = "";
14 public String pass = "";
15
16 public test()
17 {
18 readUsername();
19 System.out.println( user + " " + pass );
20 }
21
22
23 private void readUsername()
24 {
25 try
26 {
27 File lastLogin = new File( "./lastlogin" );
28 Cipher cipher = getCipher( 2, "passwordfile" );
29 DataInputStream dis;
30 if ( cipher != null )
31 dis = new DataInputStream( new CipherInputStream( new FileInputStream(lastLogin), cipher ) );
32 else
33 {
34 dis = new DataInputStream( new FileInputStream( lastLogin ) );
35 }
36 user = dis.readUTF();
37 pass = dis.readUTF();
38 dis.close();
39 }
40 catch (Exception e)
41 {
42 e.printStackTrace();
43 }
44 }
45
46 private Cipher getCipher(int mode, String password) throws Exception
47 {
48 Random random = new Random(43287234L);
49 byte[] salt = new byte[8];
50 random.nextBytes(salt);
51 PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 5);
52
53 SecretKey pbeKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(password.toCharArray()));
54 Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
55 cipher.init(mode, pbeKey, pbeParamSpec);
56 return cipher;
57 }
58}