· 5 years ago · Nov 21, 2019, 01:00 PM
1_______________________ogmessage
2package javaapplication6;
3import java.math.BigInteger;
4
5
6
7public class Message {
8
9private static BigInteger NaiveSquareRootSearch(BigInteger a, BigInteger left,BigInteger right){
10 BigInteger root = left.add(right).shiftRight(1);
11 if(!((root.pow(2).compareTo(a) == -1)&&(root.add(BigInteger.ONE).pow(2).compareTo(a) == 1))){
12 if (root.pow(2).compareTo(a) == -1) root = NaiveSquareRootSearch(a, root,right);
13 if (root.pow(2).compareTo(a) == 1) root = NaiveSquareRootSearch(a, left,root);
14 }
15 return root;
16 }
17
18public static BigInteger SquareRoot(BigInteger a){
19 return NaiveSquareRootSearch(a, BigInteger.ZERO, a);
20}
21
22BigInteger p = new BigInteger("13");
23BigInteger q = new BigInteger("17");
24BigInteger c_p = new BigInteger("10");
25BigInteger c_q = new BigInteger("6");
26BigInteger four = new BigInteger("4");
27BigInteger two = new BigInteger("2");
28BigInteger one = new BigInteger("1");
29BigInteger zero = new BigInteger("0");
30BigInteger minusOne = zero.subtract(one);
31BigInteger pq = p.multiply(q);
32BigInteger pPlusq = p.add(q);
33BigInteger delta = ((pPlusq.multiply(pPlusq)).subtract(four.multiply(pq)));
34BigInteger x1 = pPlusq.add(SquareRoot(delta)).divide(two);
35BigInteger x2 = pPlusq.subtract(SquareRoot(delta)).divide(two);
36BigInteger d = c_q.modPow(minusOne, pq);
37
38public void Print(){
39 System.out.println(d);
40}
41
42}
43____________________________________________appl
44package javaapplication6;
45
46import java.security.NoSuchAlgorithmException;
47import java.security.SecureRandom;
48import java.security.spec.InvalidKeySpecException;
49import javax.crypto.SecretKey;
50import javax.crypto.SecretKeyFactory;
51import javax.crypto.spec.PBEKeySpec;
52import javax.crypto.spec.SecretKeySpec;
53
54public class JavaApplication6 {
55
56 public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException {
57 char[] password = "short_password".toCharArray();
58 byte[] salt = new byte[16];
59 int iteration_count = 10000;
60 int key_size = 32;
61 SecureRandom myPRNG = new SecureRandom();
62 myPRNG.nextBytes(salt);
63
64 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
65 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count, key_size);
66 SecretKey myAESPBKey = new SecretKeySpec(keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
67 System.out.println("AES key: " +javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
68 }
69
70}