· 7 years ago · Nov 19, 2018, 01:30 PM
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package javaapplication68;
7import java.security.Key;
8import java.security.KeyPair;
9import java.security.KeyPairGenerator;
10import java.security.Security;
11import java.security.SecureRandom;
12import javax.crypto.Cipher;
13import java.security.InvalidKeyException;
14import java.security.NoSuchAlgorithmException;
15import java.security.spec.InvalidKeySpecException;
16import java.util.logging.Level;
17import java.util.logging.Logger;
18import javax.crypto.BadPaddingException;
19import javax.crypto.IllegalBlockSizeException;
20import javax.crypto.NoSuchPaddingException;
21import javax.crypto.SecretKey;
22import javax.crypto.SecretKeyFactory;
23import javax.crypto.ShortBufferException;
24import javax.crypto.spec.PBEKeySpec;
25import javax.crypto.spec.SecretKeySpec;
26import javax.rmi.CORBA.Util;
27/**
28 *
29 * @author student
30 */
31public class JavaApplication68 {
32
33 /**
34 * @param args the command line arguments
35 */
36 static SecureRandom myPRNG = new SecureRandom();
37 static char[] set = "ABCD".toCharArray();
38 public static char[] genRandom(int i, int j, int k, int l){
39 char[] ch = {set[i], set[j], set[k], set[l]};
40 return ch;
41 }
42 public static SecretKey genKey(char[] password, byte[] salt) throws InvalidKeySpecException{
43 SecretKey myAESPBKey = null;
44 try {
45
46 int iteration_count = 10000;
47 int key_size = 128;
48
49 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
50 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,key_size);
51 myAESPBKey = new SecretKeySpec(keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
52
53
54 } catch (NoSuchAlgorithmException ex) {
55 Logger.getLogger(JavaApplication68.class.getName()).log(Level.SEVERE, null, ex);
56 }
57 return myAESPBKey;
58 }
59 public static void main(String[] args) throws InvalidKeySpecException {
60 // TODO code application logic here
61
62 char[] password = genRandom(myPRNG.nextInt(4),myPRNG.nextInt(4),myPRNG.nextInt(4),myPRNG.nextInt(4));
63 System.out.println(String.valueOf(password));
64
65 byte[] salt = new byte[16];
66 myPRNG.nextBytes(salt);
67
68 SecretKey key = null;
69 try {
70 key = JavaApplication68.genKey(password,salt);
71 } catch (InvalidKeySpecException ex) {
72 Logger.getLogger(JavaApplication68.class.getName()).log(Level.SEVERE, null, ex);
73 }
74 System.out.println("AES key: " +javax.xml.bind.DatatypeConverter.printHexBinary(key.getEncoded()));
75
76 for(int i=0;i<4;i++)
77 for(int j=0;j<4;j++)
78 for(int k=0;k<4;k++)
79 for(int l=0;l<4;l++)
80 {
81 char[] genPassword = genRandom(i,j,k,l);
82 SecretKey genKey = JavaApplication68.genKey(genPassword,salt);
83
84 if(key.equals(genKey)){
85 System.out.println("Password is:" + String.valueOf(genPassword));
86 break;
87 }
88 }
89 }
90
91}