-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKeyGeneratorExample.java
More file actions
29 lines (22 loc) · 893 Bytes
/
KeyGeneratorExample.java
File metadata and controls
29 lines (22 loc) · 893 Bytes
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
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;
public class KeyGeneratorExample {
public static void main(String args[]) throws Exception{
//Creating a KeyGenerator object
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
//Creating a SecureRandom object
SecureRandom secRandom = new SecureRandom();
//Initializing the KeyGenerator
keyGen.init(secRandom);
//Creating/Generating a key
Key key = keyGen.generateKey();
System.out.println(key);
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(cipher.ENCRYPT_MODE, key);
String msg = new String("Hi how are you");
byte[] bytes = cipher.doFinal(msg.getBytes());
System.out.println(bytes);
}
}