-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRSACryptoSystem.java
65 lines (57 loc) · 1.87 KB
/
RSACryptoSystem.java
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
//package ecc.rsa;
//import ecc.*;
import java.math.BigInteger;
import java.io.*;
import java.util.*;
import java.security.MessageDigest;
public class RSACryptoSystem implements CryptoSystem {
MessageDigest hash;
public RSACryptoSystem() {
try {
hash = MessageDigest.getInstance("SHA-1");
} catch (java.security.NoSuchAlgorithmException e) {
System.out.println("RSACryptoSystem: THIS CANNOT HAPPEN\n"+e);
System.exit(0);
}
}
public int blockSize() {
return 20;
}
public byte[] encrypt(byte[] input, int numbytes, Key key) {
RSAKey k = (RSAKey) key;
hash.reset();
BigInteger hashelem = new BigInteger(k.n.bitLength() + 17, Rand.om).mod(k.n);
byte[] cryptelm = hashelem.modPow(k.e, k.n).toByteArray();
byte[] res = new byte[cryptelm.length+numbytes+2];
res[0] = (byte)((cryptelm.length)>>8);
res[1] = (byte)cryptelm.length;
System.arraycopy(cryptelm,0,res,2,cryptelm.length);
hash.update(hashelem.toByteArray());
byte[] digest = hash.digest();
for(int j = 0; j < numbytes; j++) {
res[cryptelm.length+2+j] = (byte)(input[j]^digest[j]);
}
return res;
}
public byte[] decrypt(byte[] input, Key key) {
RSAKey k = (RSAKey) key;
byte[] cryptelm = new byte[((input[0]&255)<<8)+input[1]&255];
byte[] res = new byte[input.length-2-cryptelm.length];
System.arraycopy(input, 2, cryptelm, 0, cryptelm.length);
hash.reset();
hash.update(new BigInteger(cryptelm).modPow(k.d, k.n).toByteArray());
byte[] digest = hash.digest();
for(int j = 0; j < res.length; j++) {
res[j] = (byte)(input[cryptelm.length+2+j]^digest[j]);
}
return res;
}
/** This method generates a new key for the cryptosystem.
*@return the new key generated*/
public Key generateKey() {
return new RSAKey(1024);
}
public String toString(){
return "RSA-Cryptosystem";
}
}