-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathARIA.java
63 lines (52 loc) · 1.86 KB
/
ARIA.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
package cryptography.ciphers.aria;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
/**
* Source code: https://github.com/bcgit/bc-java/blob/master/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/ARIA.java
* Look all providers. 'provider.addAlgorithm...'
*/
public class ARIA {
public static void main(String[] args) {
}
// Provider
static {
Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 1);
}
public static String encrypt(String input, String key) {
org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
String output = "";
try {
SecretKeySpec secretKey = new SecretKeySpec(getKey(key), "ARIA");
Cipher cipher = Cipher.getInstance("ARIA");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
output = base64.encodeToString(cipher.doFinal(input.getBytes()));
} catch (GeneralSecurityException e) {
e.printStackTrace();
return e.toString();
}
return output;
}
public static String decrypt(String input, String key) {
org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
String output = "";
try {
SecretKeySpec secretKey = new SecretKeySpec(getKey(key), "ARIA");
Cipher cipher = Cipher.getInstance("ARIA");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
output = new String(cipher.doFinal(base64.decode(input.getBytes())));
} catch (GeneralSecurityException e) {
e.printStackTrace();
return e.toString();
}
return output;
}
private static byte[] getKey(String key) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(key.getBytes());
return md.digest();
}
}