-
Notifications
You must be signed in to change notification settings - Fork 33
/
IDEA.java
54 lines (46 loc) · 1.66 KB
/
IDEA.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
package cryptography.ciphers.idea;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.GeneralSecurityException;
import java.security.Security;
/**
* Source code:
* https://github.com/bcgit/bc-java/blob/master/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/IDEA.java
* https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/crypto/engines/IDEAEngine.java
*/
public class IDEA {
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(key.getBytes(), "IDEA");
Cipher cipher = Cipher.getInstance("IDEA");
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(key.getBytes(), "IDEA");
Cipher cipher = Cipher.getInstance("IDEA");
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;
}
}