· 7 years ago · Jun 16, 2018, 07:10 PM
1public void mytestSimple(long code, String password) throws Exception {
2 SecretKey key = new SecretKeySpec(password.getBytes(), "DES");
3 Cipher ecipher = Cipher.getInstance("DES");
4 ecipher.init(Cipher.ENCRYPT_MODE, key);
5 System.out.println(ecipher.getOutputSize(8));
6
7 byte[] encrypted = ecipher.doFinal(numberToBytes(code));
8 System.out.println(encrypted + "--" + encrypted.length);
9
10 Cipher dcipher = Cipher.getInstance("DES");
11 dcipher.init(Cipher.DECRYPT_MODE, key);
12 byte[] decrypted = dcipher.doFinal(encrypted);
13 System.out.println(bytesToNumber(decrypted) + "--" + decrypted.length);
14}
15
16public void testSimple() throws Exception {
17 mytestSimple(981762654986L, "password");
18}
19
20next_pass :
21 for (int pass = 0; pass < 100; pass++) {
22 byte[] key = new byte[16];
23 (new SecureRandom()).nextBytes(key);
24 Cipher ciph = Cipher.getInstance("AES");
25 SecretKeySpec ks = new SecretKeySpec(key, "AES");
26 ByteBuffer bb = ByteBuffer.allocate(16);
27 Set<String> already = new HashSet<String>(100000);
28 int colls = 0;
29 for (int i = 0; i < 200000; i++) {
30 bb.putLong(0, i);
31 ciph.init(Cipher.ENCRYPT_MODE, ks);
32 byte[] encr = ciph.doFinal(bb.array());
33 encr[0] &= 0x7f; // make all numbers positive
34 BigInteger bigint = new BigInteger(encr);
35 String userNo = bigint.toString();
36 userNo = userNo.substring(4, 16);
37 if (!already.add(userNo)) {
38 System.out.println("Coll after " + i);
39 continue next_pass;
40 }
41 }
42 System.out.println("No collision.");
43 }
44
45XOR
46
47public void mytestSimple(long code, String password) throws Exception
48 { SecretKey key = new SecretKeySpec (password.getBytes(),"DES");
49 Cipher ecipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
50 byte[] plaintext = new byte[8];
51
52 for (int i=0; i<8; i++)
53 { plaintext[7-i] = (byte) (code & 0x00FF);
54 >>>= 8;
55 }
56
57 ecipher.init (Cipher.ENCRYPT_MODE, key);
58 System.out.println(ecipher.getOutputSize(8));
59
60 byte[] encrypted = ecipher.doFinal(plaintext);
61 System.out.println("--" + encrypted.length);
62
63 Cipher dcipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
64 dcipher.init(Cipher.DECRYPT_MODE, key);
65
66 byte[] crypttext = dcipher.doFinal(encrypted);
67 long decoded = 0;
68
69 for (int i=0; i<8; i++)
70 { decoded <<= 8;
71 decoded += crypttext[i] & 0x00FF;
72 }
73
74 System.out.println(decode + "--" + crypttext.length);
75 }
76
77public void mytestSimple(long code, String password) throws Exception
78 { SecretKey key = new SecretKeySpec (password.getBytes(),"DES");
79 Cipher ecipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
80 byte[] plaintext = new byte[8];
81
82 for (int i=0; i<8; i++)
83 { plaintext[7-i] = (byte) (code & 0x00FF);
84 >>>= 8;
85 }
86
87 ecipher.init (Cipher.ENCRYPT_MODE, key);
88 System.out.println(ecipher.getOutputSize(8));
89
90 byte[] encrypted = ecipher.doFinal(plaintext);
91 System.out.println("--" + encrypted.length);
92
93 Cipher dcipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
94 dcipher.init(Cipher.DECRYPT_MODE, key);
95
96 byte[] crypttext = dcipher.doFinal(encrypted);
97 long decoded = 0;
98
99 for (int i=0; i<8; i++)
100 { decoded <<= 8;
101 decoded += crypttext[i] & 0x00FF;
102 }
103
104 System.out.println(decode + "--" + crypttext.length);
105 }
106
107public void testSimple() throws NoSuchAlgorithmException {
108 MyKeyPairGenerator x = new MyKeyPairGenerator();
109 x.initialize(40, new SecureRandom("password".getBytes()));
110
111 MyPublicPrivateKey keypair = x.generateKeyPair();
112 System.out.println(keypair);
113
114 BigInteger message = new BigInteger("167890871234");
115 BigInteger encoded = message.modPow(keypair.e, keypair.n);
116 System.out.println(encoded); //gives some encoded value
117 BigInteger decoded = encoded.modPow(keypair.d, keypair.n);
118 System.out.println(decoded); //gives back original value
119}
120
121Input: 4852
122Password: passw0rd1 => hashcode = 37592
123
124a = [10, 11, 12, 13, .. 98, 99]
125
126b = [45, 15, 56, 49, .. 33, 88]
127
128b[48] => 23
129b[52] => 96