· 7 years ago · Apr 27, 2018, 06:30 AM
1package crypt;
2
3import java.security.PrivateKey;
4import java.security.PublicKey;
5import java.security.Signature;
6
7import javax.crypto.Cipher;
8import javax.crypto.SecretKey;
9import javax.crypto.spec.SecretKeySpec;
10
11public class Crypter {
12 private static String HEXES = "0123456789ABCDEF";
13
14 public static String encrypt(String unencrypted, SecretKey key) throws Exception {
15 Cipher cipher = Cipher.getInstance("AES");
16 cipher.init(Cipher.ENCRYPT_MODE, key);
17
18 byte[] encryptedBytes = cipher.doFinal(unencrypted.getBytes());
19
20 return "es" + hexStringFromBytes(encryptedBytes);
21 }
22
23 public static String decrypt(String encrypted, SecretKey key) throws Exception {
24 Cipher cipher = Cipher.getInstance("AES");
25 cipher.init(Cipher.DECRYPT_MODE, key);
26
27 byte[] encryptedMessage = bytesFromHexString(encrypted);
28
29 byte[] decryptedBytes = cipher.doFinal(encryptedMessage);
30
31 return new String(decryptedBytes);
32 }
33
34 public static String encrypt(String unencrypted, SecretKeySpec key) throws Exception {
35 Cipher cipher = Cipher.getInstance("AES");
36 cipher.init(Cipher.ENCRYPT_MODE, key);
37
38 byte[] encryptedBytes = cipher.doFinal(unencrypted.getBytes());
39
40 return "es" + hexStringFromBytes(encryptedBytes);
41 }
42
43 public static String decrypt(String encrypted, SecretKeySpec key) throws Exception {
44 Cipher cipher = Cipher.getInstance("AES");
45 cipher.init(Cipher.DECRYPT_MODE, key);
46
47 byte[] encryptedMessage = bytesFromHexString(encrypted);
48
49 byte[] decryptedBytes = cipher.doFinal(encryptedMessage);
50
51 return new String(decryptedBytes);
52 }
53
54
55 public static String encrypt(String unencrypted, PrivateKey myKey, PublicKey otherPubKey) throws Exception {
56 Cipher publicCipher = Cipher.getInstance("RSA");
57 publicCipher.init(Cipher.ENCRYPT_MODE, otherPubKey);
58
59 // * I'm not sure if this works--Ciphers have a "update" method that is used for piece-wise encryption
60 // * but since we use strings this should still work
61 // * Also, the loop ends when there is still stuff in "unencryptedBytes" since it increments by 116,
62 // * so there are still remaining bytes in the array
63 byte[] unencryptedBytes = unencrypted.getBytes();
64 String retVal = "ea";
65 if(unencryptedBytes.length > 116) {
66 int i;
67 for(i=0; i < unencryptedBytes.length; i+=116) {
68 byte[] temp = new byte[116];
69 System.arraycopy(unencryptedBytes, i, temp, i, 116);
70 byte[] encryptedTemp = publicCipher.doFinal(temp);
71 retVal += hexStringFromBytes(encryptedTemp);
72 }
73 if(unencryptedBytes.length % 116 != 0 ){
74 int j = unencryptedBytes.length % 116;
75 byte[] temp = new byte[j];
76 System.arraycopy(unencryptedBytes, j, temp, j, 116);
77 byte[] encryptedTemp = publicCipher.doFinal(temp);
78 retVal += hexStringFromBytes(encryptedTemp);
79 }
80 }
81
82 byte[] encryptedBytes = publicCipher.doFinal(unencrypted.getBytes());
83
84 Signature sig = Signature.getInstance("SHA1withRSA");
85 sig.initSign(myKey);
86 sig.update(encryptedBytes);
87 byte[] signature = sig.sign();
88
89 return "ea" + hexStringFromBytes(encryptedBytes) + "_" + hexStringFromBytes(signature);
90 }
91
92 public static String decrypt(String encrypted, PrivateKey myKey, PublicKey otherPubKey) throws Exception {
93 String[] sigAndMessage = encrypted.split("_");
94 byte[] encryptedMessage = bytesFromHexString(sigAndMessage[0]);
95 //byte[] signature = bytesFromHexString(sigAndMessage[1]);
96
97 Signature sig = Signature.getInstance("SHA1withRSA");
98 sig.initVerify(otherPubKey);
99 sig.update(encryptedMessage);
100
101 // comment these lines back in if you are a baller and want to try to fix it.
102 //if(sig.verify(signature)) {
103 Cipher privateCipher = Cipher.getInstance("RSA");
104 privateCipher.init(Cipher.DECRYPT_MODE, myKey);
105 byte[] decryptedBytes = privateCipher.doFinal(encryptedMessage);
106
107 return new String(decryptedBytes);
108 //} else {
109 // System.out.println("Doesn't verify.");
110 // return null;
111 //}
112 }
113
114 public static String hexStringFromBytes(byte[] raw) {
115 if (raw == null) {
116 return null;
117 }
118 StringBuilder hex = new StringBuilder(2 * raw.length);
119 for (byte b : raw) {
120 hex.append(HEXES.charAt((b & 0xF0) >> 4))
121 .append(HEXES.charAt((b & 0x0F)));
122 }
123 return hex.toString();
124 }
125
126 public static byte[] bytesFromHexString(String s) {
127 int len = s.length();
128 byte[] data = new byte[len/2];
129 for (int i = 0; i < len; i += 2) {
130 data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
131 + Character.digit(s.charAt(i+1), 16));
132 }
133 return data;
134 }
135}