@@ -44,6 +44,11 @@ public class Pubnub {
4444 private String SUBSCRIBE_KEY = "" ;
4545 private String SECRET_KEY = "" ;
4646 public String CIPHER_KEY = "" ;
47+ public String INITIALIZATION_VECTOR = "" ;
48+
49+ /** The default initialization vector. */
50+ private static final String DEFAULT_IV = "0123456789012345" ;
51+
4752 private boolean SSL = false ;
4853 private String sessionUUID = "" ;
4954 private String parameters = "" ;
@@ -69,10 +74,34 @@ private class ChannelStatus {
6974 * @param String
7075 * Cipher Key.
7176 * @param boolean SSL Enabled.
77+ *
78+ * @param initialization_vector The initialization vector used to create the cipher.
79+ */
80+ public Pubnub (String publish_key , String subscribe_key , String secret_key ,
81+ String cipher_key , boolean ssl_on , String initialization_vector ) {
82+ this .init (publish_key , subscribe_key , secret_key , cipher_key , ssl_on , initialization_vector );
83+ }
84+
85+ /**
86+ * PubNub 3.1 with Cipher Key
87+ *
88+ * Prepare PubNub State.
89+ *
90+ * @param String
91+ * Publish Key.
92+ * @param String
93+ * Subscribe Key.
94+ * @param String
95+ * Secret Key.
96+ * @param String
97+ * Cipher Key.
98+ * @param boolean SSL Enabled.
99+ *
100+ * .
72101 */
73102 public Pubnub (String publish_key , String subscribe_key , String secret_key ,
74- String cipher_key , boolean ssl_on ) {
75- this .init (publish_key , subscribe_key , secret_key , cipher_key , ssl_on );
103+ String cipher_key , boolean ssl_on ) {
104+ this .init (publish_key , subscribe_key , secret_key , cipher_key , ssl_on , DEFAULT_IV );
76105 }
77106
78107 /**
@@ -90,7 +119,7 @@ public Pubnub(String publish_key, String subscribe_key, String secret_key,
90119 */
91120 public Pubnub (String publish_key , String subscribe_key , String secret_key ,
92121 boolean ssl_on ) {
93- this .init (publish_key , subscribe_key , secret_key , "" , ssl_on );
122+ this .init (publish_key , subscribe_key , secret_key , "" , ssl_on , "" );
94123 }
95124
96125 /**
@@ -104,7 +133,7 @@ public Pubnub(String publish_key, String subscribe_key, String secret_key,
104133 * Subscribe Key.
105134 */
106135 public Pubnub (String publish_key , String subscribe_key ) {
107- this .init (publish_key , subscribe_key , "" , "" , false );
136+ this .init (publish_key , subscribe_key , "" , "" , false , "" );
108137 }
109138
110139 /**
@@ -120,7 +149,7 @@ public Pubnub(String publish_key, String subscribe_key) {
120149 * Secret Key.
121150 */
122151 public Pubnub (String publish_key , String subscribe_key , String secret_key ) {
123- this .init (publish_key , subscribe_key , secret_key , "" , false );
152+ this .init (publish_key , subscribe_key , secret_key , "" , false , "" );
124153 }
125154
126155 /**
@@ -137,13 +166,15 @@ public Pubnub(String publish_key, String subscribe_key, String secret_key) {
137166 * @param String
138167 * Cipher Key.
139168 * @param boolean SSL Enabled.
169+ * @param String Initialization vector used to configure the cipher.
140170 */
141171 public void init (String publish_key , String subscribe_key ,
142- String secret_key , String cipher_key , boolean ssl_on ) {
172+ String secret_key , String cipher_key , boolean ssl_on , String initialization_vector ) {
143173 this .PUBLISH_KEY = publish_key ;
144174 this .SUBSCRIBE_KEY = subscribe_key ;
145175 this .SECRET_KEY = secret_key ;
146176 this .CIPHER_KEY = cipher_key ;
177+ this .INITIALIZATION_VECTOR = initialization_vector ;
147178 this .SSL = ssl_on ;
148179
149180 if (this .sessionUUID .equals ("" ))
@@ -192,7 +223,7 @@ public JSONArray publish(HashMap<String, Object> args) {
192223 JSONObject obj = (JSONObject ) message ;
193224 if (this .CIPHER_KEY .length () > 0 ) {
194225 // Encrypt Message
195- PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY );
226+ PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
196227 message = pc .encrypt (obj );
197228 } else {
198229 message = obj ;
@@ -202,7 +233,7 @@ public JSONArray publish(HashMap<String, Object> args) {
202233 String obj = (String ) message ;
203234 if (this .CIPHER_KEY .length () > 0 ) {
204235 // Encrypt Message
205- PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY );
236+ PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
206237 try {
207238 message = pc .encrypt (obj );
208239 } catch (Exception e ) {
@@ -218,7 +249,7 @@ public JSONArray publish(HashMap<String, Object> args) {
218249
219250 if (this .CIPHER_KEY .length () > 0 ) {
220251 // Encrypt Message
221- PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY );
252+ PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
222253 message = pc .encryptJSONArray (obj );
223254 } else {
224255 message = obj ;
@@ -437,7 +468,7 @@ private void _subscribe(HashMap<String, Object> args) {
437468
438469 if (this .CIPHER_KEY .length () > 0 ) {
439470 // Decrypt Message
440- PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY );
471+ PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
441472 message = pc .decrypt (message );
442473 }
443474 if (callback != null )
@@ -448,7 +479,7 @@ private void _subscribe(HashMap<String, Object> args) {
448479 if (arr != null ) {
449480 if (this .CIPHER_KEY .length () > 0 ) {
450481 PubnubCrypto pc = new PubnubCrypto (
451- this .CIPHER_KEY );
482+ this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
452483 arr = pc .decryptJSONArray (arr );
453484 ;
454485 }
@@ -458,7 +489,7 @@ private void _subscribe(HashMap<String, Object> args) {
458489 String msgs = messages .getString (0 );
459490 if (this .CIPHER_KEY .length () > 0 ) {
460491 PubnubCrypto pc = new PubnubCrypto (
461- this .CIPHER_KEY );
492+ this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
462493 msgs = pc .decrypt (msgs );
463494 }
464495 if (callback != null )
@@ -658,7 +689,7 @@ private void _presence(HashMap<String, Object> args) {
658689
659690 if (this .CIPHER_KEY .length () > 0 ) {
660691 // Decrypt Message
661- PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY );
692+ PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
662693 message = pc .decrypt (message );
663694 }
664695 if (callback != null )
@@ -669,7 +700,7 @@ private void _presence(HashMap<String, Object> args) {
669700 if (arr != null ) {
670701 if (this .CIPHER_KEY .length () > 0 ) {
671702 PubnubCrypto pc = new PubnubCrypto (
672- this .CIPHER_KEY );
703+ this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
673704 arr = pc .decryptJSONArray (arr );
674705 }
675706 if (callback != null )
@@ -678,7 +709,7 @@ private void _presence(HashMap<String, Object> args) {
678709 String msgs = messages .getString (0 );
679710 if (this .CIPHER_KEY .length () > 0 ) {
680711 PubnubCrypto pc = new PubnubCrypto (
681- this .CIPHER_KEY );
712+ this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
682713 msgs = pc .decrypt (msgs );
683714 }
684715 if (callback != null )
@@ -717,7 +748,7 @@ public JSONArray here_now(String channel) {
717748
718749 if (this .CIPHER_KEY .length () > 0 ) {
719750 // Decrypt Messages
720- PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY );
751+ PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
721752 return pc .decryptJSONArray (response );
722753 } else {
723754 return response ;
@@ -767,7 +798,7 @@ public JSONArray history(HashMap<String, Object> args) {
767798
768799 if (this .CIPHER_KEY .length () > 0 ) {
769800 // Decrypt Messages
770- PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY );
801+ PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
771802 return pc .decryptJSONArray (response );
772803 } else {
773804 return response ;
@@ -804,7 +835,7 @@ public JSONArray detailedHistory(String channel, long start, long end, int count
804835 JSONArray response = _request (url );
805836
806837 if (this .CIPHER_KEY .length () > 0 ) {
807- PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY );
838+ PubnubCrypto pc = new PubnubCrypto (this .CIPHER_KEY , this . INITIALIZATION_VECTOR );
808839 try {
809840 return pc .decryptJSONArray (response .getJSONArray (0 ));
810841 } catch (JSONException e ) {
@@ -1190,8 +1221,10 @@ class PubnubCrypto {
11901221
11911222 private final String CIPHER_KEY ;
11921223
1193- public PubnubCrypto (String CIPHER_KEY ) {
1224+ private final String INITIALIZATION_VECTOR ;
1225+ public PubnubCrypto (String CIPHER_KEY , String INITIALIZATION_VECTOR ) {
11941226 this .CIPHER_KEY = CIPHER_KEY ;
1227+ this .INITIALIZATION_VECTOR = INITIALIZATION_VECTOR ;
11951228 }
11961229
11971230 /**
@@ -1336,7 +1369,7 @@ public String decrypt(String cipher_text) throws Exception {
13361369 */
13371370 private byte [] transform (boolean encrypt_or_decrypt , byte [] input_bytes ) throws Exception {
13381371 ByteArrayOutputStream output = new ByteArrayOutputStream ();
1339- byte [] iv_bytes = "0123456789012345" .getBytes ();
1372+ byte [] iv_bytes = INITIALIZATION_VECTOR .getBytes ();
13401373 byte [] key_bytes = md5 (this .CIPHER_KEY );
13411374
13421375 SecretKeySpec key = new SecretKeySpec (key_bytes , "AES" );
0 commit comments