Skip to content

Commit 749d257

Browse files
authored
Add Java code samples for fpe (GoogleCloudPlatform#3118)
* Add Java code samples for fpe To be linked from https://cloud.google.com/dlp/docs/pseudonymization * Update DeIdentifyTableWithFpe.java * Update ReIdentifyTableWithFpe.java
1 parent c0d7ae9 commit 749d257

File tree

5 files changed

+595
-0
lines changed

5 files changed

+595
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dlp.snippets;
18+
19+
// [START dlp_deidentify_table_fpe]
20+
21+
import com.google.cloud.dlp.v2.DlpServiceClient;
22+
import com.google.common.io.BaseEncoding;
23+
import com.google.privacy.dlp.v2.ContentItem;
24+
import com.google.privacy.dlp.v2.CryptoKey;
25+
import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig;
26+
import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig.FfxCommonNativeAlphabet;
27+
import com.google.privacy.dlp.v2.DeidentifyConfig;
28+
import com.google.privacy.dlp.v2.DeidentifyContentRequest;
29+
import com.google.privacy.dlp.v2.DeidentifyContentResponse;
30+
import com.google.privacy.dlp.v2.FieldId;
31+
import com.google.privacy.dlp.v2.FieldTransformation;
32+
import com.google.privacy.dlp.v2.InfoType;
33+
import com.google.privacy.dlp.v2.InfoTypeTransformations;
34+
import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation;
35+
import com.google.privacy.dlp.v2.InspectConfig;
36+
import com.google.privacy.dlp.v2.KmsWrappedCryptoKey;
37+
import com.google.privacy.dlp.v2.LocationName;
38+
import com.google.privacy.dlp.v2.PrimitiveTransformation;
39+
import com.google.privacy.dlp.v2.RecordTransformations;
40+
import com.google.privacy.dlp.v2.Table;
41+
import com.google.privacy.dlp.v2.Table.Row;
42+
import com.google.privacy.dlp.v2.Value;
43+
import com.google.protobuf.ByteString;
44+
import java.io.IOException;
45+
import java.util.Arrays;
46+
47+
public class DeIdentifyTableWithFpe {
48+
49+
public static void deIdentifyTableWithFpe() throws IOException {
50+
// TODO(developer): Replace these variables before running the sample.
51+
String projectId = "your-project-id";
52+
String kmsKeyName =
53+
"projects/YOUR_PROJECT/"
54+
+ "locations/YOUR_KEYRING_REGION/"
55+
+ "keyRings/YOUR_KEYRING_NAME/"
56+
+ "cryptoKeys/YOUR_KEY_NAME";
57+
String wrappedAesKey = "YOUR_ENCRYPTED_AES_256_KEY";
58+
Table tableToDeIdentify = Table.newBuilder()
59+
.addHeaders(FieldId.newBuilder().setName("Employee ID").build())
60+
.addHeaders(FieldId.newBuilder().setName("Date").build())
61+
.addHeaders(FieldId.newBuilder().setName("Compensation").build())
62+
.addRows(Row.newBuilder()
63+
.addValues(Value.newBuilder().setStringValue("11111").build())
64+
.addValues(Value.newBuilder().setStringValue("2015").build())
65+
.addValues(Value.newBuilder().setStringValue("$10").build())
66+
.build())
67+
.addRows(Row.newBuilder()
68+
.addValues(Value.newBuilder().setStringValue("11111").build())
69+
.addValues(Value.newBuilder().setStringValue("2016").build())
70+
.addValues(Value.newBuilder().setStringValue("$20").build())
71+
.build())
72+
.addRows(Row.newBuilder()
73+
.addValues(Value.newBuilder().setStringValue("22222").build())
74+
.addValues(Value.newBuilder().setStringValue("2016").build())
75+
.addValues(Value.newBuilder().setStringValue("$15").build())
76+
.build())
77+
.build();
78+
deIdentifyTableWithFpe(projectId, tableToDeIdentify, kmsKeyName, wrappedAesKey);
79+
}
80+
81+
public static void deIdentifyTableWithFpe(
82+
String projectId, Table tableToDeIdentify, String kmsKeyName, String wrappedAesKey)
83+
throws IOException {
84+
// Initialize client that will be used to send requests. This client only needs to be created
85+
// once, and can be reused for multiple requests. After completing all of your requests, call
86+
// the "close" method on the client to safely clean up any remaining background resources.
87+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
88+
// Specify what content you want the service to de-identify.
89+
ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build();
90+
91+
// Specify an encrypted AES-256 key and the name of the Cloud KMS key that encrypted it
92+
KmsWrappedCryptoKey kmsWrappedCryptoKey =
93+
KmsWrappedCryptoKey.newBuilder()
94+
.setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedAesKey)))
95+
.setCryptoKeyName(kmsKeyName)
96+
.build();
97+
CryptoKey cryptoKey = CryptoKey.newBuilder().setKmsWrapped(kmsWrappedCryptoKey).build();
98+
99+
// Specify how the content should be encrypted.
100+
CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig =
101+
CryptoReplaceFfxFpeConfig.newBuilder()
102+
.setCryptoKey(cryptoKey)
103+
// Set of characters in the input text. For more info, see
104+
// https://cloud.google.com/dlp/docs/reference/rest/v2/organizations.deidentifyTemplates#DeidentifyTemplate.FfxCommonNativeAlphabet
105+
.setCommonAlphabet(FfxCommonNativeAlphabet.NUMERIC)
106+
.build();
107+
PrimitiveTransformation primitiveTransformation =
108+
PrimitiveTransformation.newBuilder()
109+
.setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig)
110+
.build();
111+
112+
// Specify field to be encrypted.
113+
FieldId fieldId = FieldId.newBuilder().setName("Employee ID").build();
114+
115+
// Associate the encryption with the specified field.
116+
FieldTransformation fieldTransformation =
117+
FieldTransformation.newBuilder()
118+
.setPrimitiveTransformation(primitiveTransformation)
119+
.addFields(fieldId)
120+
.build();
121+
RecordTransformations transformations =
122+
RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build();
123+
124+
DeidentifyConfig deidentifyConfig =
125+
DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build();
126+
127+
// Combine configurations into a request for the service.
128+
DeidentifyContentRequest request =
129+
DeidentifyContentRequest.newBuilder()
130+
.setParent(LocationName.of(projectId, "global").toString())
131+
.setItem(contentItem)
132+
.setDeidentifyConfig(deidentifyConfig)
133+
.build();
134+
135+
// Send the request and receive response from the service.
136+
DeidentifyContentResponse response = dlp.deidentifyContent(request);
137+
138+
// Print the results.
139+
System.out.println(
140+
"Table after format-preserving encryption: " + response.getItem().getTable());
141+
}
142+
}
143+
}
144+
// [END dlp_deidentify_table_fpe]
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dlp.snippets;
18+
19+
// [START dlp_deidentify_text_fpe]
20+
21+
import com.google.cloud.dlp.v2.DlpServiceClient;
22+
import com.google.common.io.BaseEncoding;
23+
import com.google.privacy.dlp.v2.ContentItem;
24+
import com.google.privacy.dlp.v2.CryptoKey;
25+
import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig;
26+
import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig.FfxCommonNativeAlphabet;
27+
import com.google.privacy.dlp.v2.DeidentifyConfig;
28+
import com.google.privacy.dlp.v2.DeidentifyContentRequest;
29+
import com.google.privacy.dlp.v2.DeidentifyContentResponse;
30+
import com.google.privacy.dlp.v2.InfoType;
31+
import com.google.privacy.dlp.v2.InfoTypeTransformations;
32+
import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation;
33+
import com.google.privacy.dlp.v2.InspectConfig;
34+
import com.google.privacy.dlp.v2.KmsWrappedCryptoKey;
35+
import com.google.privacy.dlp.v2.LocationName;
36+
import com.google.privacy.dlp.v2.PrimitiveTransformation;
37+
import com.google.protobuf.ByteString;
38+
import java.io.IOException;
39+
import java.util.Arrays;
40+
41+
public class DeIdentifyTextWithFpe {
42+
43+
public static void deIdentifyTextWithFpe() throws IOException {
44+
// TODO(developer): Replace these variables before running the sample.
45+
String projectId = "your-project-id";
46+
String textToDeIdentify = "I'm Gary and my email is gary@example.com";
47+
String kmsKeyName =
48+
"projects/YOUR_PROJECT/"
49+
+ "locations/YOUR_KEYRING_REGION/"
50+
+ "keyRings/YOUR_KEYRING_NAME/"
51+
+ "cryptoKeys/YOUR_KEY_NAME";
52+
String wrappedAesKey = "YOUR_ENCRYPTED_AES_256_KEY";
53+
deIdentifyTextWithFpe(projectId, textToDeIdentify, kmsKeyName, wrappedAesKey);
54+
}
55+
56+
public static void deIdentifyTextWithFpe(
57+
String projectId, String textToDeIdentify, String kmsKeyName, String wrappedAesKey)
58+
throws IOException {
59+
// Initialize client that will be used to send requests. This client only needs to be created
60+
// once, and can be reused for multiple requests. After completing all of your requests, call
61+
// the "close" method on the client to safely clean up any remaining background resources.
62+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
63+
// Specify what content you want the service to de-identify.
64+
ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).build();
65+
66+
// Specify the type of info you want the service to de-identify.
67+
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types.
68+
InfoType infoType = InfoType.newBuilder().setName("PHONE_NUMBER").build();
69+
InspectConfig inspectConfig =
70+
InspectConfig.newBuilder().addAllInfoTypes(Arrays.asList(infoType)).build();
71+
72+
// Specify an encrypted AES-256 key and the name of the Cloud KMS key that encrypted it.
73+
KmsWrappedCryptoKey kmsWrappedCryptoKey =
74+
KmsWrappedCryptoKey.newBuilder()
75+
.setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedAesKey)))
76+
.setCryptoKeyName(kmsKeyName)
77+
.build();
78+
CryptoKey cryptoKey = CryptoKey.newBuilder().setKmsWrapped(kmsWrappedCryptoKey).build();
79+
80+
// Specify how the info from the inspection should be encrypted.
81+
InfoType surrogateInfoType = InfoType.newBuilder().setName("PHONE_TOKEN").build();
82+
CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig =
83+
CryptoReplaceFfxFpeConfig.newBuilder()
84+
.setCryptoKey(cryptoKey)
85+
// Set of characters in the input text. For more info, see
86+
// https://cloud.google.com/dlp/docs/reference/rest/v2/organizations.deidentifyTemplates#DeidentifyTemplate.FfxCommonNativeAlphabet
87+
.setCommonAlphabet(FfxCommonNativeAlphabet.NUMERIC)
88+
.setSurrogateInfoType(surrogateInfoType)
89+
.build();
90+
PrimitiveTransformation primitiveTransformation =
91+
PrimitiveTransformation.newBuilder()
92+
.setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig)
93+
.build();
94+
InfoTypeTransformation infoTypeTransformation =
95+
InfoTypeTransformation.newBuilder()
96+
.setPrimitiveTransformation(primitiveTransformation)
97+
.build();
98+
InfoTypeTransformations transformations =
99+
InfoTypeTransformations.newBuilder().addTransformations(infoTypeTransformation).build();
100+
101+
DeidentifyConfig deidentifyConfig =
102+
DeidentifyConfig.newBuilder().setInfoTypeTransformations(transformations).build();
103+
104+
// Combine configurations into a request for the service.
105+
DeidentifyContentRequest request =
106+
DeidentifyContentRequest.newBuilder()
107+
.setParent(LocationName.of(projectId, "global").toString())
108+
.setItem(contentItem)
109+
.setInspectConfig(inspectConfig)
110+
.setDeidentifyConfig(deidentifyConfig)
111+
.build();
112+
113+
// Send the request and receive response from the service.
114+
DeidentifyContentResponse response = dlp.deidentifyContent(request);
115+
116+
// Print the results.
117+
System.out.println(
118+
"Text after format-preserving encryption: " + response.getItem().getValue());
119+
}
120+
}
121+
}
122+
// [END dlp_deidentify_text_fpe]
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dlp.snippets;
18+
19+
// [START dlp_reidentify_table_fpe]
20+
21+
import com.google.cloud.dlp.v2.DlpServiceClient;
22+
import com.google.common.io.BaseEncoding;
23+
import com.google.privacy.dlp.v2.ContentItem;
24+
import com.google.privacy.dlp.v2.CryptoKey;
25+
import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig;
26+
import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig.FfxCommonNativeAlphabet;
27+
import com.google.privacy.dlp.v2.CustomInfoType;
28+
import com.google.privacy.dlp.v2.CustomInfoType.SurrogateType;
29+
import com.google.privacy.dlp.v2.DeidentifyConfig;
30+
import com.google.privacy.dlp.v2.FieldId;
31+
import com.google.privacy.dlp.v2.FieldTransformation;
32+
import com.google.privacy.dlp.v2.InfoType;
33+
import com.google.privacy.dlp.v2.InfoTypeTransformations;
34+
import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation;
35+
import com.google.privacy.dlp.v2.InspectConfig;
36+
import com.google.privacy.dlp.v2.KmsWrappedCryptoKey;
37+
import com.google.privacy.dlp.v2.LocationName;
38+
import com.google.privacy.dlp.v2.PrimitiveTransformation;
39+
import com.google.privacy.dlp.v2.RecordTransformations;
40+
import com.google.privacy.dlp.v2.ReidentifyContentRequest;
41+
import com.google.privacy.dlp.v2.ReidentifyContentResponse;
42+
import com.google.privacy.dlp.v2.Table;
43+
import com.google.privacy.dlp.v2.Table.Row;
44+
import com.google.privacy.dlp.v2.Value;
45+
import com.google.protobuf.ByteString;
46+
import java.io.IOException;
47+
48+
public class ReIdentifyTableWithFpe {
49+
50+
public static void reIdentifyTableWithFpe() throws IOException {
51+
// TODO(developer): Replace these variables before running the sample.
52+
String projectId = "your-project-id";
53+
String kmsKeyName =
54+
"projects/YOUR_PROJECT/"
55+
+ "locations/YOUR_KEYRING_REGION/"
56+
+ "keyRings/YOUR_KEYRING_NAME/"
57+
+ "cryptoKeys/YOUR_KEY_NAME";
58+
String wrappedAesKey = "YOUR_ENCRYPTED_AES_256_KEY";
59+
Table tableToReIdentify = Table.newBuilder()
60+
.addHeaders(FieldId.newBuilder().setName("Employee ID").build())
61+
.addRows(
62+
Row.newBuilder().addValues(
63+
Value.newBuilder().setStringValue("28777").build())
64+
.build())
65+
.build();
66+
reIdentifyTableWithFpe(projectId, tableToReIdentify, kmsKeyName, wrappedAesKey);
67+
}
68+
69+
public static void reIdentifyTableWithFpe(
70+
String projectId, Table tableToReIdentify, String kmsKeyName, String wrappedAesKey)
71+
throws IOException {
72+
// Initialize client that will be used to send requests. This client only needs to be created
73+
// once, and can be reused for multiple requests. After completing all of your requests, call
74+
// the "close" method on the client to safely clean up any remaining background resources.
75+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
76+
// Specify what content you want the service to re-identify.
77+
ContentItem contentItem = ContentItem.newBuilder().setTable(tableToReIdentify).build();
78+
79+
// Specify an encrypted AES-256 key and the name of the Cloud KMS key that encrypted it.
80+
KmsWrappedCryptoKey kmsWrappedCryptoKey =
81+
KmsWrappedCryptoKey.newBuilder()
82+
.setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedAesKey)))
83+
.setCryptoKeyName(kmsKeyName)
84+
.build();
85+
CryptoKey cryptoKey = CryptoKey.newBuilder().setKmsWrapped(kmsWrappedCryptoKey).build();
86+
87+
// Specify how to un-encrypt the previously de-identified information.
88+
CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig =
89+
CryptoReplaceFfxFpeConfig.newBuilder()
90+
.setCryptoKey(cryptoKey)
91+
// Set of characters in the input text. For more info, see
92+
// https://cloud.google.com/dlp/docs/reference/rest/v2/organizations.deidentifyTemplates#DeidentifyTemplate.FfxCommonNativeAlphabet
93+
.setCommonAlphabet(FfxCommonNativeAlphabet.NUMERIC)
94+
.build();
95+
PrimitiveTransformation primitiveTransformation =
96+
PrimitiveTransformation.newBuilder()
97+
.setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig)
98+
.build();
99+
100+
// Specify field to be decrypted.
101+
FieldId fieldId = FieldId.newBuilder().setName("Employee ID").build();
102+
103+
// Associate the decryption with the specified field.
104+
FieldTransformation fieldTransformation =
105+
FieldTransformation.newBuilder()
106+
.setPrimitiveTransformation(primitiveTransformation)
107+
.addFields(fieldId)
108+
.build();
109+
RecordTransformations transformations =
110+
RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build();
111+
112+
DeidentifyConfig reidentifyConfig =
113+
DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build();
114+
115+
// Combine configurations into a request for the service.
116+
ReidentifyContentRequest request =
117+
ReidentifyContentRequest.newBuilder()
118+
.setParent(LocationName.of(projectId, "global").toString())
119+
.setItem(contentItem)
120+
.setReidentifyConfig(reidentifyConfig)
121+
.build();
122+
123+
// Send the request and receive response from the service
124+
ReidentifyContentResponse response = dlp.reidentifyContent(request);
125+
126+
// Print the results
127+
System.out.println("Table after re-identification: " + response.getItem().getValue());
128+
}
129+
}
130+
}
131+
// [END dlp_reidentify_table_fpe]

0 commit comments

Comments
 (0)