|
| 1 | +package com.pubnub.api; |
| 2 | + |
| 3 | + |
| 4 | +import java.io.*; |
| 5 | +import java.security.InvalidAlgorithmParameterException; |
| 6 | +import java.security.InvalidKeyException; |
| 7 | +import java.security.MessageDigest; |
| 8 | +import java.security.NoSuchAlgorithmException; |
| 9 | +import java.security.spec.AlgorithmParameterSpec; |
| 10 | + |
| 11 | +import javax.crypto.BadPaddingException; |
| 12 | +import javax.crypto.Cipher; |
| 13 | +import javax.crypto.IllegalBlockSizeException; |
| 14 | +import javax.crypto.NoSuchPaddingException; |
| 15 | +import javax.crypto.spec.IvParameterSpec; |
| 16 | +import javax.crypto.spec.SecretKeySpec; |
| 17 | + |
| 18 | +import android.util.Base64; |
| 19 | + |
| 20 | +/** |
| 21 | + * PubNub 3.1 Cryptography |
| 22 | + * |
| 23 | + */ |
| 24 | +abstract class PubnubCryptoCore { |
| 25 | + |
| 26 | + byte[] keyBytes = null; |
| 27 | + byte[] ivBytes = null; |
| 28 | + String IV = "0123456789012345"; |
| 29 | + String CIPHER_KEY; |
| 30 | + boolean INIT = false; |
| 31 | + protected static Logger log = new Logger(Worker.class); |
| 32 | + |
| 33 | + public PubnubCryptoCore(String CIPHER_KEY) { |
| 34 | + this.CIPHER_KEY = CIPHER_KEY; |
| 35 | + } |
| 36 | + public PubnubCryptoCore(String CIPHER_KEY, String initialization_vector) { |
| 37 | + if (initialization_vector != null) this.IV = initialization_vector; |
| 38 | + this.CIPHER_KEY = CIPHER_KEY; |
| 39 | + } |
| 40 | + |
| 41 | + public void InitCiphers() throws PubnubException { |
| 42 | + if (INIT) return; |
| 43 | + try { |
| 44 | + |
| 45 | + keyBytes = new String(hexEncode(sha256(this.CIPHER_KEY.getBytes("UTF-8"))), |
| 46 | + "UTF-8").substring(0, 32).toLowerCase().getBytes("UTF-8"); |
| 47 | + ivBytes = IV.getBytes("UTF-8"); |
| 48 | + INIT = true; |
| 49 | + } catch (UnsupportedEncodingException e) { |
| 50 | + throw new PubnubException(newCryptoError(11, e.toString())); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + public static byte[] hexEncode(byte[] input) throws PubnubException { |
| 56 | + StringBuffer result = new StringBuffer(); |
| 57 | + for (byte byt : input) result.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1)); |
| 58 | + try { |
| 59 | + return result.toString().getBytes("UTF-8"); |
| 60 | + } catch (UnsupportedEncodingException e) { |
| 61 | + throw new PubnubException(newCryptoError(12, e.toString())); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static PubnubError newCryptoError(int code, String message) { |
| 66 | + return PubnubError.getErrorObject(PubnubError.PNERROBJ_CRYPTO_ERROR, code, message); |
| 67 | + } |
| 68 | + |
| 69 | + public String encrypt(String input) throws PubnubException { |
| 70 | + try { |
| 71 | + InitCiphers(); |
| 72 | + AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); |
| 73 | + SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES"); |
| 74 | + Cipher cipher = null; |
| 75 | + cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); |
| 76 | + cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec); |
| 77 | + return new String(Base64Encoder.encode(cipher.doFinal(input.getBytes("UTF-8")))); |
| 78 | + } catch (NoSuchAlgorithmException e) { |
| 79 | + throw new PubnubException(newCryptoError(13, e.toString())); |
| 80 | + } catch (NoSuchPaddingException e) { |
| 81 | + throw new PubnubException(newCryptoError(14, e.toString())); |
| 82 | + } catch (InvalidKeyException e) { |
| 83 | + throw new PubnubException(newCryptoError(15, e.toString())); |
| 84 | + } catch (InvalidAlgorithmParameterException e) { |
| 85 | + throw new PubnubException(newCryptoError(16, e.toString())); |
| 86 | + } catch (UnsupportedEncodingException e) { |
| 87 | + throw new PubnubException(newCryptoError(17, e.toString())); |
| 88 | + } catch (IllegalBlockSizeException e) { |
| 89 | + throw new PubnubException(newCryptoError(18, e.toString())); |
| 90 | + } catch (BadPaddingException e) { |
| 91 | + throw new PubnubException(newCryptoError(19, e.toString())); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Decrypt |
| 98 | + * |
| 99 | + * @param cipher_text |
| 100 | + * @return String |
| 101 | + * @throws PubnubException |
| 102 | + */ |
| 103 | + public String decrypt(String cipher_text) throws PubnubException { |
| 104 | + try { |
| 105 | + InitCiphers(); |
| 106 | + AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); |
| 107 | + SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES"); |
| 108 | + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); |
| 109 | + cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec); |
| 110 | + return new String(cipher.doFinal(Base64Encoder.decode(cipher_text)), "UTF-8"); |
| 111 | + } catch (IllegalArgumentException e) { |
| 112 | + throw new PubnubException(newCryptoError(111, e.toString())); |
| 113 | + } catch (UnsupportedEncodingException e) { |
| 114 | + throw new PubnubException(newCryptoError(112, e.toString())); |
| 115 | + } catch (IllegalBlockSizeException e) { |
| 116 | + throw new PubnubException(newCryptoError(113, e.toString())); |
| 117 | + } catch (BadPaddingException e) { |
| 118 | + throw new PubnubException(newCryptoError(114, e.toString())); |
| 119 | + } catch (InvalidKeyException e) { |
| 120 | + throw new PubnubException(newCryptoError(115, e.toString())); |
| 121 | + } catch (InvalidAlgorithmParameterException e) { |
| 122 | + throw new PubnubException(newCryptoError(116, e.toString())); |
| 123 | + } catch (NoSuchAlgorithmException e) { |
| 124 | + throw new PubnubException(newCryptoError(117, e.toString())); |
| 125 | + } catch (NoSuchPaddingException e) { |
| 126 | + throw new PubnubException(newCryptoError(118, e.toString())); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + public static byte[] hexStringToByteArray(String s) { |
| 132 | + int len = s.length(); |
| 133 | + byte[] data = new byte[len / 2]; |
| 134 | + for (int i = 0; i < len; i += 2) { |
| 135 | + data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character |
| 136 | + .digit(s.charAt(i + 1), 16)); |
| 137 | + } |
| 138 | + return data; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Get MD5 |
| 143 | + * |
| 144 | + * @param input |
| 145 | + * @return byte[] |
| 146 | + * @throws PubnubException |
| 147 | + */ |
| 148 | + public static byte[] md5(String input) throws PubnubException { |
| 149 | + MessageDigest digest; |
| 150 | + try { |
| 151 | + digest = MessageDigest.getInstance("MD5"); |
| 152 | + byte[] hashedBytes = digest.digest(input.getBytes("UTF-8")); |
| 153 | + return hashedBytes; |
| 154 | + } catch (NoSuchAlgorithmException e) { |
| 155 | + throw new PubnubException(newCryptoError(118, e.toString())); |
| 156 | + } catch (UnsupportedEncodingException e) { |
| 157 | + throw new PubnubException(newCryptoError(119, e.toString())); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Get SHA256 |
| 163 | + * |
| 164 | + * @param input |
| 165 | + * @return byte[] |
| 166 | + * @throws PubnubException |
| 167 | + */ |
| 168 | + public static byte[] sha256(byte[] input) throws PubnubException { |
| 169 | + MessageDigest digest; |
| 170 | + try { |
| 171 | + digest = MessageDigest.getInstance("SHA-256"); |
| 172 | + byte[] hashedBytes = digest.digest(input); |
| 173 | + return hashedBytes; |
| 174 | + } catch (NoSuchAlgorithmException e) { |
| 175 | + throw new PubnubException(newCryptoError(1111, e.toString())); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments