- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.java
More file actions
Latest commit
113 lines (92 loc) · 3.21 KB
/
Copy pathEncryption.java
File metadata and controls
113 lines (92 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
packagePasswordManagerGUI;
importjava.math.BigInteger;
importjava.util.Random;
publicclassEncryption {
/**
* generates prime larger than given number. It's capable of creating huge numbers due to raising parameter to the power of 15. Thanks to the Big Integer implementation it handels that well.
*
* @param number
* @return
*/
staticpublicBigIntegerprimeLargerThan(BigIntegernumber) //returns prime number larger than given number
{
for (; ; ) {
number = number.pow(15);
number = number.nextProbablePrime();
if (number.isProbablePrime(1))
returnnumber;
}
}
/**
* Decrypts a signle sign
*
* @param encryptedpswd encrypted password
* @param d part of the RSA
* @param n part of the RSA
* @return returns a decrypted single sign due to some length issues
*/
publicStringdecrypt(BigIntegerencryptedpswd, BigIntegerd, BigIntegern) {
BigIntegeradditional;
BigIntegerresult;
Stringdecryptedpsgn = ""; //decrytping sign-by-sign
//for (int i = 0; i<encryptedpswd.length; i++)
//{
additional = encryptedpswd;
result = additional.modPow(d, n);
//System.out.println(result);
decryptedpsgn += Character.toString(result.intValue());
//}
returndecryptedpsgn;
}
/**
* encrypts given string
*
* @param pswd string to encrpyt
* @param e part of the RSA
* @param n part of the RSA
* @return encrypted password as a Big Integer array
*/
publicBigInteger[] encrypt(Stringpswd, BigIntegere, BigIntegern) {
BigInteger[] encryptedpswd = newBigInteger[pswd.length()];
BigIntegeradditional;
BigIntegerresult;
for (inti = 0; i < pswd.length(); i++) {
encryptedpswd[i] = BigInteger.valueOf(pswd.charAt(i));
additional = encryptedpswd[i];
result = additional.modPow(e, n);
encryptedpswd[i] = result;
}
returnencryptedpswd;
}
/**
* @param d part of the RSA
* @param phi Euler's totient function
* @return modular multiplicative inverse
*/
publicintmodularInverse(intd, intphi) // returns modular inverse
{
for (inti = 1; i < phi; i++) {
if ((d * i) % phi == 1)
returni;
}
return -1;
}
staticpublicBigIntegergetE(BigIntegerphi) //returns random e, 1<e<phi(n)
{
for (; ; ) {
BigIntegere = newBigInteger(phi.bitLength(), newRandom());
if (e.gcd(phi).intValue() == 1 && phi.compareTo(e) == 1 && phi.compareTo(BigInteger.valueOf(0)) == 1)
returne;
}
}
/**
* returns Euler's totient function by calculating (p-1)(q-1)
*
* @param p
* @param q
* @return Euler's totient
*/
staticpublicBigIntegergetPhi(BigIntegerp, BigIntegerq) {
return (p.subtract(BigInteger.valueOf(1))).multiply(q.subtract(BigInteger.valueOf(1)));
}
}