Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. package ssl;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4. import java.security.InvalidAlgorithmParameterException;
  5. import java.security.InvalidKeyException;
  6. import java.security.NoSuchAlgorithmException;
  7. import java.security.NoSuchProviderException;
  8.  
  9. import javax.crypto.BadPaddingException;
  10. import javax.crypto.Cipher;
  11. import javax.crypto.IllegalBlockSizeException;
  12. import javax.crypto.NoSuchPaddingException;
  13. import javax.crypto.spec.IvParameterSpec;
  14. import javax.crypto.spec.SecretKeySpec;
  15.  
  16. /**
  17. * AES encryption stream
  18. *
  19. * @author phoenix
  20. *
  21. */
  22. public class AESStream {
  23.  
  24. /** ???? */
  25. private String IV = "AAAAAAAAAAAAAAAA";
  26.  
  27. /** Encryption key */
  28. private byte[] key = null;
  29.  
  30. /** Used cipher suide */
  31. private Cipher cipher;
  32.  
  33. public AESStream() {
  34. this.key = null;
  35. try {
  36. cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
  37. } catch (NoSuchAlgorithmException e) {
  38. throw new RuntimeException("Encryption algorithm not supported", e);
  39. } catch (NoSuchProviderException e) {
  40. throw new RuntimeException("Encryption provider not supported", e);
  41. } catch (NoSuchPaddingException e) {
  42. throw new RuntimeException("Padding algoritm not supported", e);
  43. }
  44. }
  45.  
  46. /**
  47. *
  48. * @param key
  49. * UTF-8 encoded encryption key
  50. */
  51. public AESStream(final String key) {
  52. this();
  53. try {
  54. this.key = key.getBytes("UTF-8");
  55. } catch (UnsupportedEncodingException e) {
  56. throw new RuntimeException("Unsupported UTF-8 encoding", e);
  57. }
  58. }
  59.  
  60. /**
  61. *
  62. * @param key
  63. * UTF-8 encoded encryption key
  64. */
  65. public AESStream(final byte[] key) {
  66. this();
  67. this.key = key;
  68. }
  69.  
  70. /**
  71. * Do byte paddding, based on 16 bytes
  72. *
  73. * @param input
  74. * to be padded
  75. * @return extended byte array, that is a multiple of 16 long
  76. */
  77. private static byte[] padding(final byte[] input) {
  78. return padding(input, 16);
  79. }
  80.  
  81. /**
  82. * Do byte paddding, based on n bytes (n = size)
  83. *
  84. * @param input
  85. * to be padded
  86. * @param size
  87. * return block will be a multiple of this number
  88. * @return extended byte array, that is a multiple of size
  89. */
  90. private static byte[] padding(final byte[] input, final int size) {
  91. if (input.length % size == 0)
  92. return input;
  93.  
  94. final int newSize = (int) (Math.ceil(input.length / (float) size)) * size;
  95. final byte[] ret = new byte[newSize];
  96. for (int i = 0; i < input.length; i++)
  97. ret[i] = input[i];
  98. for (int i = input.length; i < newSize; i++)
  99. ret[i] = '\0';
  100. return ret;
  101.  
  102. }
  103.  
  104. /**
  105. * Remove tailing '\0' strings
  106. *
  107. * @param input
  108. * to be de-padded
  109. * @return de-padded byte array
  110. */
  111. private static byte[] depadding(final byte[] input) {
  112. int len = input.length;
  113. while (len > 0 && input[len - 1] == '\0')
  114. len--;
  115.  
  116. if (len == input.length)
  117. return input;
  118. else {
  119. final byte[] ret = new byte[len];
  120. for (int i = 0; i < len; i++)
  121. ret[i] = input[i];
  122. return ret;
  123. }
  124. }
  125.  
  126. public byte[] getKey() {
  127. return key;
  128. }
  129.  
  130. public void setKey(byte[] key) {
  131. this.key = padding(key);
  132. }
  133.  
  134. public void setKey(final String key) {
  135. try {
  136. setKey(padding(key.getBytes("UTF-8")));
  137. } catch (UnsupportedEncodingException e) {
  138. throw new RuntimeException("Unsupported UTF-8 encoding", e);
  139. }
  140. }
  141.  
  142. public byte[] encrypt(final String plainText) {
  143. try {
  144. return encrypt(padding(plainText.getBytes("UTF-8")));
  145. } catch (UnsupportedEncodingException e) {
  146. throw new RuntimeException("Unsupported encoding", e);
  147. }
  148. }
  149.  
  150. public byte[] encrypt(final byte[] buffer) {
  151. final SecretKeySpec key = new SecretKeySpec(this.key, "AES");
  152. synchronized (this.cipher) {
  153. try {
  154. cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
  155. return cipher.doFinal(buffer);
  156. } catch (InvalidKeyException e) {
  157. throw new RuntimeException("Invalid key", e);
  158. } catch (InvalidAlgorithmParameterException e) {
  159. throw new RuntimeException("Invalid algorithm", e);
  160. } catch (UnsupportedEncodingException e) {
  161. throw new RuntimeException("Unsupported encoding", e);
  162. } catch (IllegalBlockSizeException e) {
  163. throw new RuntimeException("Illegal block size", e);
  164. } catch (BadPaddingException e) {
  165. throw new RuntimeException("Bad padding", e);
  166. }
  167.  
  168. }
  169. }
  170.  
  171. public String decryptStr(byte[] cipherText) {
  172. final byte[] buf = decrypt(cipherText);
  173. try {
  174. return new String(buf, "UTF-8");
  175. } catch (UnsupportedEncodingException e) {
  176. throw new RuntimeException("Unsupported encoding", e);
  177. }
  178. }
  179.  
  180. public String decryptStr(final String cipherText) {
  181. try {
  182. return decryptStr(cipherText.getBytes("UTF-8"));
  183. } catch (UnsupportedEncodingException e) {
  184. throw new RuntimeException("Unsupported encoding", e);
  185. }
  186. }
  187.  
  188. public byte[] decrypt(final String cipherText) {
  189. try {
  190. return decrypt(cipherText.getBytes("UTF-8"));
  191. } catch (UnsupportedEncodingException e) {
  192. throw new RuntimeException("Unsupported encoding", e);
  193. }
  194. }
  195.  
  196. public byte[] decrypt(byte[] cipherText) {
  197. if (cipherText.length % 16 != 0)
  198. throw new RuntimeException("Input block size wrong");
  199.  
  200. synchronized (this.cipher) {
  201. final SecretKeySpec key = new SecretKeySpec(this.key, "AES");
  202. try {
  203. cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
  204. return depadding(cipher.doFinal(cipherText));
  205. } catch (InvalidKeyException e) {
  206. throw new RuntimeException("Invalid key", e);
  207. } catch (InvalidAlgorithmParameterException e) {
  208. throw new RuntimeException("Invalid algorithm", e);
  209. } catch (UnsupportedEncodingException e) {
  210. throw new RuntimeException("Unsupported encoding", e);
  211. } catch (IllegalBlockSizeException e) {
  212. throw new RuntimeException("Illegal block size", e);
  213. } catch (BadPaddingException e) {
  214. throw new RuntimeException("Bad padding", e);
  215. }
  216. }
  217. }
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement