-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKeyGenerator.java
More file actions
77 lines (62 loc) · 2.25 KB
/
Copy pathKeyGenerator.java
File metadata and controls
77 lines (62 loc) · 2.25 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
package cryptography;
/*
Uses the permutation and combination of armsrtong
numbers to generate a numeric key from string input.
*/
class KeyGenerator
{
//private : for internal use of this class only
//static data : one allocation for all the objects of the class
private static int armstrongNumbers[]= {153, 370, 371, 407};
private static int baseTable[]= {1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321};
//non static:
//data belongs to the object of the class
private String key, numericKey;
//constructor:
//A special member of the class, that resemebles a method but has a special signature.
//It is an initializer for the objects of the class.
//It is auto invoked as object of the class is created.
//FYI: if a constructor is applied a return type
//then it converts into a method.
KeyGenerator(String k)
{
//this reference of Java == self reference of Python
//this is implicitly declared and used.
this.key = k;
this.numericKey = "";
}
String getNumericKey()
{
if(numericKey.equals(""))
generateNumericKey();
return numericKey;
}
void generateNumericKey()
{//key: How old is my computer?
int tot = 0;
int l, i;
l = key.length();
for(i =0; i < l; i++)
{//add the ASCII of characters of key
tot += key.charAt(i);
//python: tot += ord(key[i])
}
//example : tot = 2141
int permutation = baseTable[tot % baseTable.length];
//example : permutation = baseTable[5] = 1432
String temp= "";
System.out.println("***" + permutation);
while(permutation > 0)
{
temp = armstrongNumbers[permutation % 10 -1] + temp;
permutation /= 10;
}
//numericKey = partA+partB
numericKey = temp + tot;
}
void display()
{
System.out.println("key " + key);
System.out.println("numeric key " + numericKey);
}
}