-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymmetric.java
More file actions
51 lines (29 loc) · 1.2 KB
/
Symmetric.java
File metadata and controls
51 lines (29 loc) · 1.2 KB
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
import java.security.InvalidKeyException;
import java.security.Key;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Symmetric {
static String algorithm = "DESede";
public static void main(String[] args) throws Exception {
SecretKey symKey = KeyGenerator.getInstance(algorithm).generateKey();
Cipher c = Cipher.getInstance(algorithm);
byte[] encryptionBytes = encryptF("texttoencrypt",symKey,c);
System.out.println("Decrypted: " + decryptF(encryptionBytes,symKey,c));
}
private static byte[] encryptF(String input,Key pkey,Cipher c) throws InvalidKeyException, BadPaddingException,
IllegalBlockSizeException {
c.init(Cipher.ENCRYPT_MODE, pkey);
byte[] inputBytes = input.getBytes();
return c.doFinal(inputBytes);
}
private static String decryptF(byte[] encryptionBytes,Key pkey,Cipher c) throws InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
c.init(Cipher.DECRYPT_MODE, pkey);
byte[] decrypt = c.doFinal(encryptionBytes);
String decrypted = new String(decrypt);
return decrypted;
}
}