-
Notifications
You must be signed in to change notification settings - Fork 0
/
AesCtr.java
141 lines (125 loc) · 4.33 KB
/
AesCtr.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
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
package javacryption.aes;
import java.util.Date;
import javacryption.exception.CryptoException;
import org.apache.commons.codec.binary.Base64;
public class AesCtr
{
public static String encrypt(String plaintext, String password, int nBits)
{
Rijndael aes = new Rijndael();
if ((nBits != 128) && (nBits != 192) && (nBits != 256)) {
throw new CryptoException("Invalid key size (" + nBits + " bits)");
}
int nBytes = nBits / 8;
byte[] pwBytes = new byte[nBytes];
for (int i = 0; i < nBytes; i++) {
pwBytes[i] = ((byte)password.charAt(i));
}
aes.makeKey(pwBytes, 256, 1);
byte[] key = aes.encryptBlock(pwBytes, new byte[16]);
aes.finalize();
if (nBytes > 16)
{
byte[] keySlice = new byte[nBytes - 16];
for (int i = 0; i < nBytes - 16; i++) {
keySlice[i] = key[i];
}
key = Util.addByteArrays(key, keySlice);
}
byte[] counterBlock = new byte[16];
long nonce = new Date().getTime();
int nonceMs = (int)nonce % 1000;
int nonceSec = (int)Math.floor(nonce / 1000L);
int nonceRnd = (int)Math.floor(Math.random() * 65535.0D);
for (int i = 0; i < 2; i++) {
counterBlock[i] = ((byte)(nonceMs >>> i * 8 & 0xFF));
}
for (int i = 0; i < 2; i++) {
counterBlock[(i + 2)] = ((byte)(nonceRnd >>> i * 8 & 0xFF));
}
for (int i = 0; i < 4; i++) {
counterBlock[(i + 4)] = ((byte)(nonceSec >>> i * 8 & 0xFF));
}
byte[] ctrTxt = new byte[8];
for (int i = 0; i < 8; i++) {
ctrTxt[i] = counterBlock[i];
}
aes.makeKey(key, 256, 1);
int blockCount = (int)Math.ceil(new Float(plaintext.length()).floatValue() /
16.0F);
byte[] ciphertxt = new byte[plaintext.length()];
for (int b = 0; b < blockCount; b++)
{
for (int c = 0; c < 4; c++) {
counterBlock[(15 - c)] = ((byte)(b >>> c * 8 & 0xFF));
}
for (int c = 0; c < 4; c++) {
counterBlock[(15 - c - 4)] = 0;
}
byte[] cipherCntr = aes.encryptBlock(counterBlock,
new byte[16]);
int blockLength = b < blockCount - 1 ? 16 :
(plaintext.length() - 1) % 16 + 1;
for (int i = 0; i < blockLength; i++) {
ciphertxt[(b * 16 + i)] =
((byte)(cipherCntr[i] ^ plaintext.charAt(b * 16 + i)));
}
}
aes.finalize();
byte[] ciphertext = Util.addByteArrays(ctrTxt, ciphertxt);
String ciphertext64 = new String(Base64.encodeBase64(ciphertext));
return ciphertext64;
}
public static String decrypt(String ciphertext, String password, int nBits)
{
Rijndael aes = new Rijndael();
if ((nBits != 128) && (nBits != 192) && (nBits != 256)) {
return null;
}
byte[] cipherByte = Base64.decodeBase64(ciphertext.getBytes());
int nBytes = nBits / 8;
byte[] pwBytes = new byte[nBytes];
for (int i = 0; i < nBytes; i++) {
pwBytes[i] = ((byte)password.charAt(i));
}
aes.makeKey(pwBytes, 256, 1);
byte[] key = aes.encryptBlock(pwBytes, new byte[16]);
aes.finalize();
if (nBytes > 16)
{
byte[] keySlice = new byte[nBytes - 16];
for (int i = 0; i < nBytes - 16; i++) {
keySlice[i] = key[i];
}
key = Util.addByteArrays(key, keySlice);
}
byte[] counterBlock = new byte[16];
for (int i = 0; i < 8; i++) {
counterBlock[i] = cipherByte[i];
}
aes.makeKey(key, 256, 1);
int blockCount = (int)Math.ceil(new Float(cipherByte.length - 8).floatValue() /
16.0F);
byte[] plaintxt = new byte[cipherByte.length - 8];
for (int b = 0; b < blockCount; b++)
{
for (int c = 0; c < 4; c++) {
counterBlock[(15 - c)] = ((byte)(b >>> c * 8 & 0xFF));
}
for (int c = 0; c < 4; c++) {
counterBlock[(15 - c - 4)] = 0;
}
byte[] cipherCntr = aes.encryptBlock(counterBlock,
new byte[16]);
int blockLength = b < blockCount - 1 ? 16 :
(cipherByte.length - 9) % 16 + 1;
for (int i = 0; i < blockLength; i++) {
plaintxt[(b * 16 + i)] =
((byte)(cipherCntr[i] ^ cipherByte[(8 + b * 16 + i)]));
}
}
aes.finalize();
String plaintext = new String(plaintxt);
return plaintext;
}
}