Skip to content

Commit e366de3

Browse files
author
Devendra
committed
removing encryption and bouncycastle
1 parent d6cff1f commit e366de3

6 files changed

Lines changed: 45 additions & 172 deletions

File tree

‎android/Pubnub-Android-3.7.0.jar‎

-1.73 KB
Binary file not shown.
-2.29 KB
Binary file not shown.

‎java/libs/bcprov-jdk15on-1.47.jar‎

-1.9 MB
Binary file not shown.

‎java/libs/lcrypto-jdk15.jar‎

-798 KB
Binary file not shown.

‎java/srcPubnubApi/com/pubnub/api/PubnubCore.java‎

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.pubnub.api;
22

3-
import org.bouncycastle.crypto.DataLengthException;
4-
import org.bouncycastle.crypto.InvalidCipherTextException;
5-
import org.bouncycastle.util.encoders.Hex;
63
import org.json.JSONArray;
74
import org.json.JSONException;
85
import org.json.JSONObject;
@@ -814,7 +811,19 @@ protected void publish(Hashtable args, Callback callback) {
814811
args.put("callback", callback);
815812
publish(args);
816813
}
817-
814+
815+
final private char[] hexArray = "0123456789abcdef".toCharArray();
816+
private String bytesToHex(byte[] bytes) {
817+
char[] hexChars = new char[bytes.length * 2];
818+
for ( int j = 0; j < bytes.length; j++ ) {
819+
int v = bytes[j] & 0xFF;
820+
hexChars[j * 2] = hexArray[v >>> 4];
821+
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
822+
}
823+
return new String(hexChars);
824+
}
825+
826+
818827
/**
819828
* Send a message to a channel.
820829
*
@@ -841,18 +850,10 @@ protected void publish(Hashtable args) {
841850
msgStr = "\"" + msgStr + "\"";
842851
}
843852
msgStr = "\"" + pc.encrypt(msgStr) + "\"";
844-
} catch (DataLengthException e) {
845-
callback.errorCallback(channel,
846-
PubnubError.getErrorObject(PubnubError.PNERROBJ_ENCRYPTION_ERROR, 1, msgStr));
847-
return;
848853
} catch (IllegalStateException e) {
849854
callback.errorCallback(channel,
850855
PubnubError.getErrorObject(PubnubError.PNERROBJ_ENCRYPTION_ERROR, 2, msgStr));
851856
return;
852-
} catch (InvalidCipherTextException e) {
853-
callback.errorCallback(channel,
854-
PubnubError.getErrorObject(PubnubError.PNERROBJ_ENCRYPTION_ERROR, 3, msgStr));
855-
return;
856857
} catch (Exception e) {
857858
callback.errorCallback(channel,
858859
PubnubError.getErrorObject(PubnubError.PNERROBJ_ENCRYPTION_ERROR, 4, msgStr + " : " + e.toString()));
@@ -872,15 +873,10 @@ protected void publish(Hashtable args) {
872873
string_to_sign.append(this.PUBLISH_KEY).append('/')
873874
.append(this.SUBSCRIBE_KEY).append('/')
874875
.append(this.SECRET_KEY).append('/').append(channel)
875-
.append('/').append(msgStr);
876+
.append('/').append(msgStr);
876877

877-
// Sign Message
878-
try {
879-
signature = new String(Hex.encode(PubnubCrypto
880-
.md5(string_to_sign.toString())), "UTF-8");
881-
} catch (UnsupportedEncodingException e) {
882-
883-
}
878+
signature = bytesToHex(PubnubCrypto
879+
.md5(string_to_sign.toString()));
884880
}
885881
String[] urlComponents = { getPubnubUrl(), "publish", this.PUBLISH_KEY,
886882
this.SUBSCRIBE_KEY, PubnubUtil.urlEncode(signature),
@@ -1480,15 +1476,9 @@ public void handleResponse(HttpRequest hreq, String response) {
14801476
} catch (JSONException e) {
14811477
cb.errorCallback(channel,
14821478
PubnubError.getErrorObject(PubnubError.PNERROBJ_JSON_ERROR, 3));
1483-
} catch (DataLengthException e) {
1484-
cb.errorCallback(channel,
1485-
PubnubError.getErrorObject(PubnubError.PNERROBJ_DECRYPTION_ERROR, 6, response));
14861479
} catch (IllegalStateException e) {
14871480
cb.errorCallback(channel,
14881481
PubnubError.getErrorObject(PubnubError.PNERROBJ_DECRYPTION_ERROR, 7, response));
1489-
} catch (InvalidCipherTextException e) {
1490-
cb.errorCallback(channel,
1491-
PubnubError.getErrorObject(PubnubError.PNERROBJ_DECRYPTION_ERROR, 8, response));
14921482
} catch (IOException e) {
14931483
cb.errorCallback(channel,
14941484
PubnubError.getErrorObject(PubnubError.PNERROBJ_DECRYPTION_ERROR, 9, response));
@@ -2282,7 +2272,7 @@ private void callErrorCallbacks(String[] channelList, PubnubError error) {
22822272
}
22832273
}
22842274

2285-
private void decryptJSONArray(JSONArray messages) throws JSONException, DataLengthException, IllegalStateException, InvalidCipherTextException, IOException {
2275+
private void decryptJSONArray(JSONArray messages) throws JSONException, IllegalStateException, IOException {
22862276

22872277
if (CIPHER_KEY.length() > 0) {
22882278
for (int i = 0; i < messages.length(); i++) {
@@ -2577,34 +2567,13 @@ private void invokeSubscribeCallback(String channel, Callback callback, Object m
25772567
.successCallback(
25782568
channel,
25792569
PubnubUtil.parseJSON(PubnubUtil.stringToJSON(message.toString())));
2580-
} catch (DataLengthException e) {
2581-
if (!isWorkerDead(hreq)) callback
2582-
.errorCallback(
2583-
channel,
2584-
PubnubError.getErrorObject(
2585-
PubnubError.PNERROBJ_DECRYPTION_ERROR, 11,
2586-
message.toString()));
25872570
} catch (IllegalStateException e) {
25882571
if (!isWorkerDead(hreq)) callback
25892572
.errorCallback(
25902573
channel,
25912574
PubnubError.getErrorObject(
25922575
PubnubError.PNERROBJ_DECRYPTION_ERROR, 12,
25932576
message.toString()));
2594-
} catch (InvalidCipherTextException e) {
2595-
if (!isWorkerDead(hreq)) callback
2596-
.errorCallback(
2597-
channel,
2598-
PubnubError.getErrorObject(
2599-
PubnubError.PNERROBJ_DECRYPTION_ERROR, 13,
2600-
message.toString()));
2601-
} catch (IOException e) {
2602-
if (!isWorkerDead(hreq)) callback
2603-
.errorCallback(
2604-
channel,
2605-
PubnubError.getErrorObject(
2606-
PubnubError.PNERROBJ_DECRYPTION_ERROR, 14,
2607-
message.toString()));
26082577
} catch (Exception e) {
26092578
if (!isWorkerDead(hreq)) callback
26102579
.errorCallback(
Lines changed: 28 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,34 @@
11
package com.pubnub.api;
22

3-
import org.bouncycastle.crypto.DataLengthException;
4-
import org.bouncycastle.crypto.Digest;
5-
import org.bouncycastle.crypto.InvalidCipherTextException;
6-
import org.bouncycastle.crypto.digests.MD5Digest;
7-
import org.bouncycastle.crypto.digests.SHA256Digest;
8-
import org.bouncycastle.crypto.engines.AESEngine;
9-
import org.bouncycastle.crypto.modes.CBCBlockCipher;
10-
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
11-
import org.bouncycastle.crypto.params.KeyParameter;
12-
import org.bouncycastle.crypto.params.ParametersWithIV;
13-
import org.bouncycastle.util.encoders.Hex;
143

154
import java.io.*;
5+
import java.security.MessageDigest;
6+
import java.security.NoSuchAlgorithmException;
167

178
/**
189
* PubNub 3.1 Cryptography
1910
*
2011
*/
2112
abstract class PubnubCryptoCore {
2213

23-
PaddedBufferedBlockCipher encryptCipher = null;
24-
PaddedBufferedBlockCipher decryptCipher = null;
25-
byte[] buf = new byte[16]; // input buffer
26-
byte[] obuf = new byte[512]; // output buffer
27-
byte[] key = null;
28-
String IV = "0123456789012345";
29-
public static int blockSize = 16;
14+
3015
String CIPHER_KEY;
3116

3217
public PubnubCryptoCore(String CIPHER_KEY) {
3318
this.CIPHER_KEY = CIPHER_KEY;
3419
}
3520
public PubnubCryptoCore(String CIPHER_KEY, String initialization_vector) {
36-
if (initialization_vector != null) this.IV = initialization_vector;
37-
this.CIPHER_KEY = CIPHER_KEY;
38-
}
39-
40-
public void InitCiphers() throws UnsupportedEncodingException {
41-
42-
key = new String(Hex.encode(sha256(this.CIPHER_KEY.getBytes("UTF-8"))),
43-
"UTF-8").substring(0, 32).toLowerCase().getBytes("UTF-8");
44-
45-
encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
46-
new AESEngine()));
47-
48-
decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
49-
new AESEngine()));
5021

51-
// create the IV parameter
52-
ParametersWithIV parameterIV = new ParametersWithIV(new KeyParameter(
53-
key), IV.getBytes("UTF-8"));
54-
55-
encryptCipher.init(true, parameterIV);
56-
decryptCipher.init(false, parameterIV);
57-
}
58-
59-
public void ResetCiphers() {
60-
if (encryptCipher != null) {
61-
encryptCipher.reset();
62-
}
63-
if (decryptCipher != null) {
64-
decryptCipher.reset();
65-
}
6622
}
6723

68-
public String encrypt(String input) throws DataLengthException,
69-
IllegalStateException, InvalidCipherTextException {
70-
try {
71-
InputStream st = new ByteArrayInputStream(input.getBytes("UTF-8"));
72-
ByteArrayOutputStream ou = new ByteArrayOutputStream();
73-
CBCEncryptOrDecrypt(st, ou, true);
74-
String s = new String(Base64Encoder.encode(ou.toByteArray()));
75-
return s;
76-
} catch (IOException ex) {
77-
ex.printStackTrace();
78-
}
79-
return "NULL";
24+
public String encrypt(String input) {
25+
return input;
8026
}
8127

82-
/**
83-
* Decrypt
84-
*
85-
* @param cipher_text
86-
* @return String
87-
* @throws DataLengthException
88-
* @throws IllegalStateException
89-
* @throws InvalidCipherTextException
90-
* @throws IOException
91-
* @throws IllegalArgumentException
92-
*/
93-
public String decrypt(String cipher_text) throws DataLengthException,
94-
IllegalStateException, InvalidCipherTextException, IOException, IllegalArgumentException {
95-
96-
byte[] cipher = Base64Encoder.decode(cipher_text);
97-
InputStream st = new ByteArrayInputStream(cipher);
98-
ByteArrayOutputStream ou = new ByteArrayOutputStream();
99-
CBCEncryptOrDecrypt(st, ou, false);
100-
101-
return new String(ou.toByteArray());
102-
28+
public String decrypt(String cipher_text) {
29+
return cipher_text;
10330
}
10431

105-
public void CBCEncryptOrDecrypt(InputStream in, OutputStream out,
106-
boolean encrypt) throws DataLengthException, IllegalStateException,
107-
InvalidCipherTextException, IOException {
108-
if (encryptCipher == null || decryptCipher == null) {
109-
InitCiphers();
110-
}
111-
PaddedBufferedBlockCipher cipher = (encrypt) ? encryptCipher
112-
: decryptCipher;
113-
int noBytesRead = 0; // number of bytes read from input
114-
int noBytesProcessed = 0; // number of bytes processed
115-
116-
while ((noBytesRead = in.read(buf)) >= 0) {
117-
noBytesProcessed = cipher
118-
.processBytes(buf, 0, noBytesRead, obuf, 0);
119-
out.write(obuf, 0, noBytesProcessed);
120-
}
121-
122-
noBytesProcessed = cipher.doFinal(obuf, 0);
123-
out.write(obuf, 0, noBytesProcessed);
124-
out.flush();
125-
in.close();
126-
out.close();
127-
}
12832

12933
public static byte[] hexStringToByteArray(String s) {
13034
int len = s.length();
@@ -143,20 +47,17 @@ public static byte[] hexStringToByteArray(String s) {
14347
* @return byte[]
14448
*/
14549
public static byte[] md5(String input) {
146-
MD5Digest digest = new MD5Digest();
147-
byte[] bytes = input.getBytes();
148-
digest.update(bytes, 0, bytes.length);
149-
byte[] md5 = new byte[digest.getDigestSize()];
150-
digest.doFinal(md5, 0);
151-
StringBuffer hex = new StringBuffer(md5.length * 2);
152-
for (int i = 0; i < md5.length; i++) {
153-
byte b = md5[i];
154-
if ((b & 0xFF) < 0x10) {
155-
hex.append("0");
156-
}
157-
hex.append(Integer.toHexString(b & 0xFF));
158-
}
159-
return hexStringToByteArray(hex.toString());
50+
51+
MessageDigest md;
52+
try {
53+
md = MessageDigest.getInstance("MD5");
54+
//Add input bytes to digest
55+
md.update(input.getBytes());
56+
//Get the hash's bytes
57+
return md.digest();
58+
} catch (NoSuchAlgorithmException e) {
59+
return null;
60+
}
16061
}
16162

16263
/**
@@ -166,13 +67,16 @@ public static byte[] md5(String input) {
16667
* @return byte[]
16768
*/
16869
public static byte[] sha256(byte[] input) {
169-
170-
Digest digest = new SHA256Digest();
171-
byte[] resBuf = new byte[digest.getDigestSize()];
172-
byte[] bytes = input;
173-
digest.update(bytes, 0, bytes.length);
174-
digest.doFinal(resBuf, 0);
175-
return resBuf;
70+
MessageDigest sha;
71+
try {
72+
sha = MessageDigest.getInstance("SHA-256");
73+
//Add input bytes to digest
74+
sha.update(input);
75+
//Get the hash's bytes
76+
return sha.digest();
77+
} catch (NoSuchAlgorithmException e) {
78+
return null;
79+
}
17680
}
17781

17882
}

0 commit comments

Comments
 (0)