|
1 | 1 | package com.pubnub.docs.miscellaneous;
|
2 | 2 |
|
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonParser; |
3 | 5 | import com.pubnub.api.PubNubException;
|
4 | 6 | import com.pubnub.api.UserId;
|
5 | 7 | import com.pubnub.api.crypto.CryptoModule;
|
| 8 | +import com.pubnub.api.java.PubNub; |
| 9 | +import com.pubnub.api.java.util.TimetokenUtil; |
6 | 10 | import com.pubnub.api.java.v2.PNConfiguration;
|
7 | 11 | import com.pubnub.api.models.consumer.push.payload.PushPayloadHelper;
|
8 | 12 | import com.pubnub.docs.SnippetBase;
|
9 |
| -import com.pubnub.api.java.PubNub; |
10 |
| -import com.pubnub.api.java.util.TimetokenUtil; |
11 | 13 |
|
12 | 14 | import java.io.InputStream;
|
| 15 | +import java.nio.charset.StandardCharsets; |
13 | 16 | import java.time.Instant;
|
14 | 17 | import java.time.LocalDateTime;
|
15 | 18 | import java.util.HashMap;
|
@@ -195,4 +198,37 @@ private void createCryptoModuleBasic() {
|
195 | 198 | PubNub pubNub = PubNub.create(configBuilder.build());
|
196 | 199 | // snippet.end
|
197 | 200 | }
|
| 201 | + |
| 202 | + private void encryptApnsBasic() { |
| 203 | + // https://www.pubnub.com/docs/general/setup/data-security#apns-example |
| 204 | + |
| 205 | + // snippet.encryptApnsBasic |
| 206 | + JsonObject clearData = new JsonObject(); |
| 207 | + clearData.addProperty("test_name", "pregnancy"); |
| 208 | + clearData.addProperty("results", "positive"); |
| 209 | + clearData.addProperty("notes", "You are having twins!"); |
| 210 | + byte[] clearBytes = clearData.toString().getBytes(StandardCharsets.UTF_8); |
| 211 | + |
| 212 | + CryptoModule aesCbcCryptoModule = CryptoModule.createAesCbcCryptoModule("myCipherKey01", true); |
| 213 | + |
| 214 | + byte[] encryptedData = aesCbcCryptoModule.encrypt(clearBytes); |
| 215 | + // [80, 78, 69, 68, 1, 65, 67, 82, 72, 16, 83, 67, -54, -1, 98, -51, 91, 120, 31, 121, 100, 95, 75, 54, -95, 60, -74, 32, 26, 108, 77, 107, -47, 50, -45, -8, 86, -67, 72, 30, -106, -9, 45, -92, -111, 118, 50, -55, -48, -103, -90, -115, -70, 120, -47, -107, 41, 7, -61, 52, -37, -61, 83, -20, 34, -30, -64, 61, 104, 24, 3, 25, 41, -122, -36, 60, 98, -16, 34, 81, -41, 46, 102, 7, -97, 64, -37, -10, 124, 67, -41, 101, 35, -80, -103, -27, -26, -34, -50, -86, -2, -84, 105, 16, -84, 4] |
| 216 | + // snippet.end |
| 217 | + } |
| 218 | + |
| 219 | + private void decryptingMessage() { |
| 220 | + // https://www.pubnub.com/docs/general/setup/data-security#decrypting-messages |
| 221 | + |
| 222 | + // snippet.decryptingMessage |
| 223 | + // parse the received message and pass the encrypted parts to decrypt API. |
| 224 | + CryptoModule aesCbcCryptoModule = CryptoModule.createAesCbcCryptoModule("myCipherKey01", true); |
| 225 | + byte[] encryptedData = new byte[]{80, 78, 69, 68, 1, 65, 67, 82, 72, 16, 83, 67, -54, -1, 98, -51, 91, 120, 31, 121, 100, 95, 75, 54, -95, 60, -74, 32, 26, 108, 77, 107, -47, 50, -45, -8, 86, -67, 72, 30, -106, -9, 45, -92, -111, 118, 50, -55, -48, -103, -90, -115, -70, 120, -47, -107, 41, 7, -61, 52, -37, -61, 83, -20, 34, -30, -64, 61, 104, 24, 3, 25, 41, -122, -36, 60, 98, -16, 34, 81, -41, 46, 102, 7, -97, 64, -37, -10, 124, 67, -41, 101, 35, -80, -103, -27, -26, -34, -50, -86, -2, -84, 105, 16, -84, 4}; |
| 226 | + |
| 227 | + byte[] decryptedBytes = aesCbcCryptoModule.decrypt(encryptedData); |
| 228 | + JsonObject decryptedJson = JsonParser.parseString(new String(decryptedBytes, StandardCharsets.UTF_8)).getAsJsonObject(); |
| 229 | + assert decryptedJson.get("test_name").getAsString().equals("pregnancy"); |
| 230 | + assert decryptedJson.get("results").getAsString().equals("positive"); |
| 231 | + assert decryptedJson.get("notes").getAsString().equals("You are having twins!"); |
| 232 | + // snippet.end |
| 233 | + } |
198 | 234 | }
|
0 commit comments