@@ -31,8 +31,8 @@ Java 7+
3131Before using this library, you will need to set up a project in the [ Mastercard Developers Portal] ( https://developer.mastercard.com ) .
3232
3333As part of this set up, you'll receive:
34- * A public request encryption certificate (aka "Client Encryption Keys" )
35- * A private response decryption key (aka "Mastercard Encryption Keys" )
34+ * A public request encryption certificate (aka _ Client Encryption Keys _ )
35+ * A private response decryption key (aka _ Mastercard Encryption Keys _ )
3636
3737### Adding the Library to Your Project <a name =" adding-the-library-to-your-project " ></a >
3838
@@ -68,7 +68,7 @@ Certificate encryptionCertificate = EncryptionUtils.loadEncryptionCertificate("<
6868
6969A ` PrivateKey ` key object can be created from a PKCS #12 file by calling the ` EncryptionUtils.loadDecryptionKey ` method:
7070``` java
71- PrivateKey signingKey = EncryptionUtils . loadDecryptionKey(
71+ PrivateKey decryptionKey = EncryptionUtils . loadDecryptionKey(
7272 " <insert PKCS#12 key file path>" ,
7373 " <insert key alias>" ,
7474 " <insert key password>" );
@@ -83,11 +83,107 @@ PrivateKey decryptionKey = EncryptionUtils.loadDecryptionKey("<insert PKCS#8 fil
8383
8484#### From a PEM file
8585
86+ Reading PEM encoded keys requires an additional step:
87+
86881 . Convert the key using: ` openssl pkcs8 -topk8 -inform PEM -outform DER -in key.pem -out key.der -nocrypt `
87892 . Call ` EncryptionUtils.loadDecryptionKey ` (see above)
8890
8991### Performing Field Level Encryption and Decryption <a name =" performing-field-level-encryption-and-decryption " ></a >
90- TODO
92+ The methods that do all the heavy lifting are ` encryptPayload ` and ` decryptPayload ` in the ` FieldLevelEncryption ` class.
93+
94+ Usage:
95+ ``` java
96+ String encryptedRequestPayload = FieldLevelEncryption . encryptPayload(requestPayload, config);
97+ String responsePayload = FieldLevelEncryption . decryptPayload(encryptedResponsePayload, config);
98+ ```
99+
100+ #### Configuring the Field Level Encryption
101+ Use the ` FieldLevelEncryptionConfigBuilder ` to create ` FieldLevelEncryptionConfig ` instances. Example:
102+ ``` java
103+ FieldLevelEncryptionConfig config = FieldLevelEncryptionConfigBuilder . aFieldLevelEncryptionConfig()
104+ .withEncryptionCertificate(encryptionCertificate)
105+ .withDecryptionKey(decryptionKey)
106+ .withEncryptionPath(" $.path.to.foo" , " $.path.to.encryptedFoo" )
107+ .withDecryptionPath(" $.path.to.encryptedFoo" , " $.path.to.foo" )
108+ .withMgf1ParameterSpec(MGF1ParameterSpec . SHA256 )
109+ .withEncryptedValueFieldName(" encryptedValue" )
110+ .withEncryptedKeyFieldName(" encryptedKey" )
111+ .withIvFieldName(" iv" )
112+ .withFieldValueEncoding(FieldValueEncoding . HEX )
113+ .build();
114+ ```
115+
116+ See also: [ FieldLevelEncryptionConfig.java] ( TODO )
117+
118+ #### Performing Encryption
119+
120+ Call ` FieldLevelEncryption.encryptPayload ` with a JSON request payload and a ` FieldLevelEncryptionConfig ` instance.
121+
122+ Example using the configuration above:
123+
124+ * Request payload:
125+ ``` json
126+ {
127+ "path" : {
128+ "to" : {
129+ "foo" : {
130+ "sensitiveField1" : " sensitiveValue1" ,
131+ "sensitiveField2" : " sensitiveValue2"
132+ }
133+ }
134+ }
135+ }
136+ ```
137+
138+ * Encrypted request payload:
139+ ``` json
140+ {
141+ "path" : {
142+ "to" : {
143+ "encryptedFoo" : {
144+ "iv" : " 7f1105fb0c684864a189fb3709ce3d28" ,
145+ "encryptedKey" : " 67f467d1b653d98dddd7eea5ae411a0c6d3c(...)ffd4c09dd42f713b6b39313503be179bae18a51bff2b48f937c8" ,
146+ "encryptedValue" : " b73aabd267517f79c54e84ff01d8bc09ed72455c2(...)dffb5fa04bf4ca4fff907d67072a75b076e6ce9ade1ff514ed6141" ,
147+ "oaepHashingAlgorithm" : " SHA256"
148+ }
149+ }
150+ }
151+ }
152+ ```
153+
154+ #### Performing Decryption
155+
156+ Call ` FieldLevelEncryption.decryptPayload ` with a JSON response payload and a ` FieldLevelEncryptionConfig ` instance.
157+
158+ Encrypted response payload:
159+ ``` json
160+ {
161+ "path" : {
162+ "to" : {
163+ "encryptedFoo" : {
164+ "iv" : " e5d313c056c411170bf07ac82ede78c9" ,
165+ "encryptedKey" : " e3a56746c0f9109d18b3a2d91619e6cac86bcc7b09652b76(...)f16d8af7e006440f17677eaaeff36b2479652f5c24ae7bd" ,
166+ "encryptedValue" : " 809a09d78257af5379df0c454dfa9c3e2ecf5787430775ebcf(...)409d8d27ab29803353cded59fe72fd4a7735c69da4080e74f" ,
167+ "oaepHashingAlgorithm" : " SHA256"
168+ }
169+ }
170+ }
171+ }
172+ ```
173+
174+ Response payload:
175+ ``` json
176+ {
177+ "path" : {
178+ "to" : {
179+ "foo" : {
180+ "sensitiveField1" : " sensitiveValue1" ,
181+ "sensitiveField2" : " sensitiveValue2"
182+ }
183+ }
184+ }
185+ }
186+ ```
91187
92188### Integrating with OpenAPI Generator API Client Libraries <a name =" integrating-with-openapi-generator-api-client-libraries " ></a >
93189
0 commit comments