forked from kasundezoysa/javacrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.java
41 lines (31 loc) · 959 Bytes
/
Object.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
import java.io.*;
import java.security.*;
import javax.crypto.*;
public class Object {
public static void main(String args[]) {
try {
//Generate a key
KeyGenerator gen = KeyGenerator.getInstance("AES");
gen.init(256);
Key sKey = gen.generateKey();
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, sKey);
// do the sealing
SealedObject so = new SealedObject("Secret",c);
// Save Object
FileOutputStream out = new FileOutputStream("Objects");
ObjectOutputStream oOut = new ObjectOutputStream(out);
oOut.writeObject(so);
oOut.writeObject(sKey);
oOut.close();
FileInputStream in = new FileInputStream("Objects");
ObjectInputStream oIn = new ObjectInputStream(in);
SealedObject o= (SealedObject) oIn.readObject();
// c.init(Cipher.DECRYPT_MODE, sKey);
String s = (String)o.getObject(sKey);
System.out.println(s);
} catch (Exception e) {
System.out.println(e);
}
}
}