Skip to content

Commit 68d4460

Browse files
committed
Added encrypt decrypt snippets for java
1 parent ed8e6b9 commit 68d4460

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

pubnub-gson/pubnub-gson-docs/src/main/java/com/pubnub/docs/miscellaneous/MiscellaneousOthers.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package com.pubnub.docs.miscellaneous;
22

3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
35
import com.pubnub.api.PubNubException;
46
import com.pubnub.api.UserId;
57
import com.pubnub.api.crypto.CryptoModule;
8+
import com.pubnub.api.java.PubNub;
9+
import com.pubnub.api.java.util.TimetokenUtil;
610
import com.pubnub.api.java.v2.PNConfiguration;
711
import com.pubnub.api.models.consumer.push.payload.PushPayloadHelper;
812
import com.pubnub.docs.SnippetBase;
9-
import com.pubnub.api.java.PubNub;
10-
import com.pubnub.api.java.util.TimetokenUtil;
1113

1214
import java.io.InputStream;
15+
import java.nio.charset.StandardCharsets;
1316
import java.time.Instant;
1417
import java.time.LocalDateTime;
1518
import java.util.HashMap;
@@ -195,4 +198,37 @@ private void createCryptoModuleBasic() {
195198
PubNub pubNub = PubNub.create(configBuilder.build());
196199
// snippet.end
197200
}
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+
}
198234
}

pubnub-kotlin/pubnub-kotlin-impl/src/test/kotlin/com/pubnub/api/crypto/CryptoModuleTest.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.pubnub.api.crypto
22

3+
import com.google.gson.JsonObject
4+
import com.google.gson.JsonParser
35
import com.pubnub.api.PubNubError
46
import com.pubnub.api.PubNubException
57
import com.pubnub.api.crypto.cryptor.Cryptor
@@ -22,7 +24,7 @@ import org.junit.jupiter.params.provider.Arguments
2224
import org.junit.jupiter.params.provider.MethodSource
2325
import java.io.ByteArrayInputStream
2426
import java.io.InputStream
25-
import java.util.Base64
27+
import java.util.*
2628
import org.hamcrest.Matchers.`is` as iz
2729

2830
class CryptoModuleTest {
@@ -149,6 +151,25 @@ class CryptoModuleTest {
149151
// then
150152
assertArrayEquals(msgToEncrypt, decryptedMsg)
151153
}
154+
@Test
155+
fun mytest() {
156+
157+
// https://www.pubnub.com/docs/general/setup/data-security#apns-example
158+
159+
// snippet.encryptApnsBasic
160+
val clearData = JsonObject()
161+
clearData.addProperty("test_name", "pregnancy")
162+
clearData.addProperty("results", "positive")
163+
clearData.addProperty("notes", "You are having twins!")
164+
val clearBytes: ByteArray = clearData.toString().toByteArray(Charsets.UTF_8)
165+
val cipherKey = "enigma"
166+
val aesCbcCryptoModule = CryptoModule.createAesCbcCryptoModule(cipherKey)
167+
val encryptedData = aesCbcCryptoModule.encrypt(clearBytes)
168+
169+
val decryptedBytes = aesCbcCryptoModule.decrypt(encryptedData);
170+
val decryptedJson: JsonObject = JsonParser.parseString(decryptedBytes.toString(Charsets.UTF_8)).asJsonObject
171+
assertEquals(clearData, decryptedJson)
172+
}
152173

153174
@Test
154175
fun `using AesCbcCryptoModule can decrypt message that was encrypted with LegacyCryptor with staticIV `() {

0 commit comments

Comments
 (0)