-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.java
More file actions
124 lines (94 loc) · 4.16 KB
/
Main.java
File metadata and controls
124 lines (94 loc) · 4.16 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import com.dyadicsec.advapi.SDEKey;
import com.dyadicsec.advapi.SDESessionKey;
import com.unbound.provider.UBCryptoProvider;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.UUID;
/**
*
* This sample contains 2 basic SDE operations :
* + string order preserving encryption.
* + credit card type preserving encryption.
*
*/
public class Main {
public static void main(String[] args) throws NoSuchProviderException, KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
if (args.length < 2) {
System.out.println("Invalid args, should be : <partition> <key_name> [user] [password]");
System.exit(1);
}
String partition = args[0];
String keyName = args[1];
String user = "user"; //optional. default is "user"
if (args.length > 2) user = args[2];
String pwd = null; //optional. default is blank
if (args.length > 3) pwd = args[3];
System.out.println(String.format("Partition : %s", partition));
System.out.println(String.format("Key Name : %s", keyName));
System.out.println(String.format("User : %s", user));
Provider provider = new UBCryptoProvider();
Security.addProvider(provider);
KeyStore keyStore = KeyStore.getInstance("PKCS11", "UNBOUND");
String auth = String.format("{\"username\":\"%s\", \"password\":\"%s\"}", user, pwd);
keyStore.load(null, auth.toCharArray());
// find key by name and partition
SDEKey sdeKey = SDEKey.findKey(partition, keyName);
if (sdeKey == null) {
System.out.println(String.format("Key not found, You can create it using 'ucl generate -t PRF --name %s -p %s'",
keyName
, partition));
System.exit(1);
} else {
System.out.println("Key successfully loaded");
}
encryptOrderPreserving(sdeKey);
encryptTypePreserving(sdeKey);
}
/**
* generate SDE session key
*
* @param sdeKey
* @param purpose
* @return
*/
private static SDESessionKey generateSessionKey(SDEKey sdeKey, int purpose) {
String tweak = UUID.randomUUID().toString();
return sdeKey.generateSessionKey(purpose, tweak);
}
/**
* encrypt string order preserving
*
* @param sdeKey
*/
private static void encryptOrderPreserving(SDEKey sdeKey) {
System.out.println("Encrypt String Order Preserving");
SDESessionKey sdeSessionKey = generateSessionKey(sdeKey, SDEKey.PURPOSE_OP_ENC);
String str1 = UUID.randomUUID().toString();
String str2 = UUID.randomUUID().toString();
String encStr1 = sdeSessionKey.encryptOrderPreserving(str1, 36);
String encStr2 = sdeSessionKey.encryptOrderPreserving(str2, 36);
String decStr1 = sdeSessionKey.decryptOrderPreserving(encStr1);
String decStr2 = sdeSessionKey.decryptOrderPreserving(encStr2);
System.out.println(String.format(" Validating order : %s",
str1.compareTo(str2) * encStr1.compareTo(encStr2) >= 0 ? "OK" : "FAILED"));
System.out.println(String.format(" Validating correctness : %s",
str1.equals(decStr1) && str2.compareTo(decStr2) >= 0 ? "OK" : "FAILED"));
}
/**
* encrypt credit card type preserving
*
* @param sdeKey
*/
private static void encryptTypePreserving(SDEKey sdeKey) {
System.out.println("Encrypt Credit Card Type Preserving");
SDESessionKey sdeSessionKey = generateSessionKey(sdeKey, SDEKey.PURPOSE_CREDIT_CARD_ENC);
String creditCard = "378282246310005";
String encCreditCard = sdeSessionKey.encryptCreditCard(creditCard);
String decCreditCard = sdeSessionKey.decryptCreditCard(encCreditCard);
System.out.println(String.format(" Original Credit Card : %s", creditCard));
System.out.println(String.format(" Encrypted Credit Card : %s", encCreditCard));
System.out.println(String.format(" Validating correctness : %s",
creditCard.equals(decCreditCard) ? "OK" : "FAILED"));
}
}