11package 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
154import java .io .*;
5+ import java .security .MessageDigest ;
6+ import java .security .NoSuchAlgorithmException ;
167
178/**
189 * PubNub 3.1 Cryptography
1910 *
2011 */
2112abstract 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