Repository navigation
Expand file tree
/
Copy pathEncryption.java
More file actions
152 lines (131 loc) · 3.99 KB
/
Copy pathEncryption.java
File metadata and controls
152 lines (131 loc) · 3.99 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.math.BigInteger;
import java.security.SecureRandom;
class InvalidatedEncryptionException extends Exception {
//called if encryption is called without a key being computed
public InvalidatedEncryptionException (String message) {
super(message);
}
}
public class Encryption {
//houses all the methods for encrypting messages
private BigInteger base;
private BigInteger mod;
private BigInteger secret;
private BigInteger key;
public Encryption(byte[] mod) {
//initiates values
SecureRandom r=new SecureRandom();
secret=new BigInteger(127,10,r);
this.mod=new BigInteger(mod);
this.base=BigInteger.valueOf(7);
key=BigInteger.valueOf(-1);
}
/* public static int getBase() {
//generates from a list of primes
int primes[]={2,3,5,7,11,13};
int sel=(int)(Math.random()*6);
return primes[sel];
}*/
public static byte[] ensureByteSize(byte [] n) {
//ensures proper 32 bit bytes before returning
if (n.length==32)return n;
else {
byte g[]=new byte[32];
int i;
for (i=0;i<n.length;i++) {
g[32-n.length+i]=n[i];
}
for (i=0;i<32-n.length;i++) {
g[i]=0;
}
return g;
}
}
public static byte[] calcMod() {
//generate a random modulus
SecureRandom r=new SecureRandom();
BigInteger tmp=new BigInteger(255,10,r);
return ensureByteSize(tmp.toByteArray());
}
public static String cipher(String text,String cipher) {
//XOR encrypts
String ret="";
int paslen=cipher.length();
for (int i=0;i<text.length();i++) {
ret+=text.charAt(i)^cipher.charAt(i%paslen);
}
return ret;
}
public void changeBase(int b) {
base=BigInteger.valueOf(b);
}
public byte[] DiffieHellmanComputeKey() { //will need bigInteger
//algorithm taken from http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange
//UNTESTD!
return ensureByteSize(base.modPow(secret,mod).toByteArray());
//return base^secret %mod
}
public void calcKey(byte[] v) {//UNTESTED
//computes key given input
BigInteger tmp=new BigInteger(v);
key=tmp.modPow(secret,mod);
System.out.println("SCRET KEY IS"+key);
}
public String encryptText(String input) throws InvalidatedEncryptionException{
if (key.equals(-1)) {
throw new InvalidatedEncryptionException("No calculated key");
}
char t[]=input.toCharArray();
byte btearray[]=key.toByteArray();
//for each letter, bitwise for each 8bit portion of the key
for (int i=0;i<t.length-1;i++) {
//for each 8 bits
//Xor with each 8 bits of key
for (int j=0;j<btearray.length;j++) {
t[i]=(char)((byte)t[i]^btearray[j]);
//will stay under 0xff
}
//adds each result to next, just to scramble it some more
t[i+1]=(char)((byte)t[i]+(byte)(t[i+1])&0xff);
//&0xff is to prevent overflow
}
for (int j=0;j<btearray.length;j++) {
t[t.length-1]=(char)((byte)t[t.length-1]^btearray[j]);
}
return new String(t);
}
public String decryptText(String input) throws InvalidatedEncryptionException{
//basically, does opposite of encryption algorithm
if (key.equals(-1)) {
throw new InvalidatedEncryptionException("No calculated key");
}
char t[]=input.toCharArray();
byte btearray[]=key.toByteArray();
String ret="";
char let=t[0];
for (int j=0;j<btearray.length;j++) {
//order for bitwise shouldn't matter
let=(char)((byte)let^btearray[j]);
}
ret+=let;
for (int i=1;i<t.length;i++) {
let=t[i];
for (int j=0;j<btearray.length;j++) {
let=(char)((byte)let^btearray[j]);
}
let=(char)((byte)(let-t[i-1])&0xff); //ensures proper sign
ret+=let;
}
return ret;
}
/* public static void main(String args[]) throws Exception{
byte bt[]={4,1,1,1,1,3,1,1,2,1,1,2,1,1,1,2,1,1,1,1};
bt=ensureByteSize(bt);
System.out.println(bt.length);
int numone=0;
for (byte i:bt) {
System.out.print(i+" ");
if (i==1) numone++;
}
}*/
}