-
Notifications
You must be signed in to change notification settings - Fork 33
/
RC.java
161 lines (139 loc) · 5.95 KB
/
RC.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package cryptography.ciphers.rc;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import org.spongycastle.crypto.BlockCipher;
import org.spongycastle.crypto.BufferedBlockCipher;
import org.spongycastle.crypto.DataLengthException;
import org.spongycastle.crypto.InvalidCipherTextException;
import org.spongycastle.crypto.engines.RC2Engine;
import org.spongycastle.crypto.engines.RC4Engine;
import org.spongycastle.crypto.engines.RC6Engine;
import org.spongycastle.crypto.modes.CBCBlockCipher;
import org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.spongycastle.crypto.params.KeyParameter;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import cryptography.Mode;
public class RC {
public static void main(String[] args) {
}
// Provider
static {
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
}
/**
* Source code:
* https://github.com/rtyley/spongycastle/blob/spongy-master/core/src/main/java/org/spongycastle/crypto/engines/RC2Engine.java
*
* @param mode cipher mode
* @param input string
* @param key cipher key
* @return Hash string
*/
public static String rc2(Mode mode, String input, String key) {
org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
try {
if (mode == Mode.ENCRYPT) {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8),
new RC2Engine().getAlgorithmName());
Cipher cipher = Cipher.getInstance(new RC2Engine().getAlgorithmName());
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return base64.encodeToString(cipher.doFinal(input.getBytes(StandardCharsets.UTF_8)));
}
if (mode == Mode.DECRYPT) {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8),
new RC2Engine().getAlgorithmName());
Cipher cipher = Cipher.getInstance(new RC2Engine().getAlgorithmName());
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return new String(cipher.doFinal(base64.decode(input.getBytes(StandardCharsets.UTF_8))));
}
} catch (NoSuchAlgorithmException | BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException
| InvalidKeyException e) {
e.printStackTrace();
return null;
}
return null;
}
/**
* Source code:
* https://github.com/rtyley/spongycastle/blob/spongy-master/core/src/main/java/org/spongycastle/crypto/engines/RC4Engine.java
*
* @param mode cipher mode
* @param input string
* @param key cipher key
* @return Hash string
*/
public static String rc4(Mode mode, String input, String key) {
org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
try {
if (mode == Mode.ENCRYPT) {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8),
new RC4Engine().getAlgorithmName());
Cipher cipher = Cipher.getInstance(new RC4Engine().getAlgorithmName());
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return base64.encodeToString(cipher.doFinal(input.getBytes(StandardCharsets.UTF_8)));
}
if (mode == Mode.DECRYPT) {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8),
new RC4Engine().getAlgorithmName());
Cipher cipher = Cipher.getInstance(new RC4Engine().getAlgorithmName());
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return new String(cipher.doFinal(base64.decode(input.getBytes(StandardCharsets.UTF_8))));
}
} catch (NoSuchAlgorithmException | BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException
| InvalidKeyException e) {
e.printStackTrace();
return null;
}
return null;
}
/**
* Source code:
* https://github.com/rtyley/spongycastle/blob/spongy-master/core/src/main/java/org/spongycastle/crypto/engines/RC6Engine.java
* See also:
* https://github.com/rtyley/spongycastle/blob/spongy-master/core/src/main/java/org/spongycastle/crypto/BufferedBlockCipher.java
* Modes:
* https://github.com/rtyley/spongycastle/blob/spongy-master/core/src/main/java/org/spongycastle/crypto/modes/CBCBlockCipher.java
* Wiki:
* https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_block_chaining_(CBC)
*
* @param mode cipher mode
* @param input string
* @param key cipher key
* @return Hash string
*/
public static String rc6(Mode mode, String input, String key) {
org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
try {
if (mode == Mode.ENCRYPT) {
BlockCipher engine = new RC6Engine();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
cipher.init(true, new KeyParameter(key.getBytes(StandardCharsets.UTF_8)));
byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
byte[] rv = new byte[cipher.getOutputSize(inputBytes.length)];
int numOutBytes = cipher.processBytes(inputBytes, 0, inputBytes.length, rv, 0);
cipher.doFinal(rv, numOutBytes);
return base64.encodeToString(rv);
}
if (mode == Mode.DECRYPT) {
BlockCipher engine = new RC6Engine();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
cipher.init(false, new KeyParameter(key.getBytes(StandardCharsets.UTF_8)));
byte[] inputBytes = base64.decode(input.getBytes(StandardCharsets.UTF_8));
byte[] rv = new byte[cipher.getOutputSize(inputBytes.length)];
int numOutBytes = cipher.processBytes(inputBytes, 0, inputBytes.length, rv, 0);
cipher.doFinal(rv, numOutBytes);
return new String(rv, StandardCharsets.UTF_8).replaceAll("\u0000.*", "");
}
} catch (DataLengthException | IllegalStateException | InvalidCipherTextException e) {
e.printStackTrace();
return null;
}
return null;
}
}