Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.14 KB | None | 0 0
  1.  
  2. Different methods of string encryption (USE THIS CODE)
  3. // CIPHER / GENERATORS
  4. import javax.crypto.Cipher;
  5. import javax.crypto.SecretKey;
  6. import javax.crypto.KeyGenerator;
  7.  
  8. // KEY SPECIFICATIONS
  9. import java.security.spec.KeySpec;
  10. import java.security.spec.AlgorithmParameterSpec;
  11. import javax.crypto.spec.PBEKeySpec;
  12. import javax.crypto.SecretKeyFactory;
  13. import javax.crypto.spec.PBEParameterSpec;
  14.  
  15. // EXCEPTIONS
  16. import java.security.InvalidAlgorithmParameterException;
  17. import java.security.NoSuchAlgorithmException;
  18. import java.security.InvalidKeyException;
  19. import java.security.spec.InvalidKeySpecException;
  20. import javax.crypto.NoSuchPaddingException;
  21. import javax.crypto.BadPaddingException;
  22. import javax.crypto.IllegalBlockSizeException;
  23. import java.io.UnsupportedEncodingException;
  24. import java.io.IOException;
  25.  
  26.  
  27. public class StringEncrypter {
  28.  
  29. Cipher ecipher;
  30. Cipher dcipher;
  31.  
  32.  
  33. /**
  34. * Constructor used to create this object. Responsible for setting
  35. * and initializing this object's encrypter and decrypter Chipher instances
  36. * given a Secret Key and algorithm.
  37. * @param key Secret Key used to initialize both the encrypter and
  38. * decrypter instances.
  39. * @param algorithm Which algorithm to use for creating the encrypter and
  40. * decrypter instances.
  41. */
  42. StringEncrypter(SecretKey key, String algorithm) {
  43. try {
  44. ecipher = Cipher.getInstance(algorithm);
  45. dcipher = Cipher.getInstance(algorithm);
  46. ecipher.init(Cipher.ENCRYPT_MODE, key);
  47. dcipher.init(Cipher.DECRYPT_MODE, key);
  48. } catch (NoSuchPaddingException e) {
  49. System.out.println("EXCEPTION: NoSuchPaddingException");
  50. } catch (NoSuchAlgorithmException e) {
  51. System.out.println("EXCEPTION: NoSuchAlgorithmException");
  52. } catch (InvalidKeyException e) {
  53. System.out.println("EXCEPTION: InvalidKeyException");
  54. }
  55. }
  56.  
  57.  
  58. /**
  59. * Constructor used to create this object. Responsible for setting
  60. * and initializing this object's encrypter and decrypter Chipher instances
  61. * given a Pass Phrase and algorithm.
  62. * @param passPhrase Pass Phrase used to initialize both the encrypter and
  63. * decrypter instances.
  64. */
  65. StringEncrypter(String passPhrase) {
  66.  
  67. // 8-bytes Salt
  68. byte[] salt = {
  69. (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
  70. (byte)0x56, (byte)0x34, (byte)0xE3, (byte)0x03
  71. };
  72.  
  73. // Iteration count
  74. int iterationCount = 19;
  75.  
  76. try {
  77.  
  78. KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
  79. SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
  80.  
  81. ecipher = Cipher.getInstance(key.getAlgorithm());
  82. dcipher = Cipher.getInstance(key.getAlgorithm());
  83.  
  84. // Prepare the parameters to the cipthers
  85. AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
  86.  
  87. ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
  88. dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
  89.  
  90. } catch (InvalidAlgorithmParameterException e) {
  91. System.out.println("EXCEPTION: InvalidAlgorithmParameterException");
  92. } catch (InvalidKeySpecException e) {
  93. System.out.println("EXCEPTION: InvalidKeySpecException");
  94. } catch (NoSuchPaddingException e) {
  95. System.out.println("EXCEPTION: NoSuchPaddingException");
  96. } catch (NoSuchAlgorithmException e) {
  97. System.out.println("EXCEPTION: NoSuchAlgorithmException");
  98. } catch (InvalidKeyException e) {
  99. System.out.println("EXCEPTION: InvalidKeyException");
  100. }
  101. }
  102.  
  103.  
  104. /**
  105. * Takes a single String as an argument and returns an Encrypted version
  106. * of that String.
  107. * @param str String to be encrypted
  108. * @return <code>String</code> Encrypted version of the provided String
  109. */
  110. public String encrypt(String str) {
  111. try {
  112. // Encode the string into bytes using utf-8
  113. byte[] utf8 = str.getBytes("UTF8");
  114.  
  115. // Encrypt
  116. byte[] enc = ecipher.doFinal(utf8);
  117.  
  118. // Encode bytes to base64 to get a string
  119. return new sun.misc.BASE64Encoder().encode(enc);
  120.  
  121. } catch (BadPaddingException e) {
  122. } catch (IllegalBlockSizeException e) {
  123. } catch (UnsupportedEncodingException e) {
  124. } catch (IOException e) {
  125. }
  126. return null;
  127. }
  128.  
  129.  
  130. /**
  131. * Takes a encrypted String as an argument, decrypts and returns the
  132. * decrypted String.
  133. * @param str Encrypted String to be decrypted
  134. * @return <code>String</code> Decrypted version of the provided String
  135. */
  136. public String decrypt(String str) {
  137.  
  138. try {
  139.  
  140. // Decode base64 to get bytes
  141. byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
  142.  
  143. // Decrypt
  144. byte[] utf8 = dcipher.doFinal(dec);
  145.  
  146. // Decode using utf-8
  147. return new String(utf8, "UTF8");
  148.  
  149. } catch (BadPaddingException e) {
  150. } catch (IllegalBlockSizeException e) {
  151. } catch (UnsupportedEncodingException e) {
  152. } catch (IOException e) {
  153. }
  154. return null;
  155. }
  156.  
  157.  
  158. /**
  159. * The following method is used for testing the String Encrypter class.
  160. * This method is responsible for encrypting and decrypting a sample
  161. * String using several symmetric temporary Secret Keys.
  162. */
  163. public static void testUsingSecretKey() {
  164. try {
  165.  
  166. System.out.println();
  167. System.out.println("+----------------------------------------+");
  168. System.out.println("| -- Test Using Secret Key Method -- |");
  169. System.out.println("+----------------------------------------+");
  170. System.out.println();
  171.  
  172. String secretString = "Secret key method";
  173.  
  174. // Generate a temporary key for this example. In practice, you would
  175. // save this key somewhere. Keep in mind that you can also use a
  176. // Pass Phrase.
  177. SecretKey desKey = KeyGenerator.getInstance("DES").generateKey();
  178. SecretKey blowfishKey = KeyGenerator.getInstance("Blowfish").generateKey();
  179. SecretKey desedeKey = KeyGenerator.getInstance("DESede").generateKey();
  180.  
  181. // Create encrypter/decrypter class
  182. StringEncrypter desEncrypter = new StringEncrypter(desKey, desKey.getAlgorithm());
  183. StringEncrypter blowfishEncrypter = new StringEncrypter(blowfishKey, blowfishKey.getAlgorithm());
  184. StringEncrypter desedeEncrypter = new StringEncrypter(desedeKey, desedeKey.getAlgorithm());
  185.  
  186. // Encrypt the string
  187. String desEncrypted = desEncrypter.encrypt(secretString);
  188. String blowfishEncrypted = blowfishEncrypter.encrypt(secretString);
  189. String desedeEncrypted = desedeEncrypter.encrypt(secretString);
  190.  
  191. // Decrypt the string
  192. String desDecrypted = desEncrypter.decrypt(desEncrypted);
  193. String blowfishDecrypted = blowfishEncrypter.decrypt(blowfishEncrypted);
  194. String desedeDecrypted = desedeEncrypter.decrypt(desedeEncrypted);
  195.  
  196. // Print out values
  197. System.out.println(desKey.getAlgorithm() + " Encryption algorithm");
  198. System.out.println(" Original String : " + secretString);
  199. System.out.println(" Encrypted String : " + desEncrypted);
  200. System.out.println(" Decrypted String : " + desDecrypted);
  201. System.out.println();
  202.  
  203. System.out.println(blowfishKey.getAlgorithm() + " Encryption algorithm");
  204. System.out.println(" Original String : " + secretString);
  205. System.out.println(" Encrypted String : " + blowfishEncrypted);
  206. System.out.println(" Decrypted String : " + blowfishDecrypted);
  207. System.out.println();
  208.  
  209. System.out.println(desedeKey.getAlgorithm() + " Encryption algorithm");
  210. System.out.println(" Original String : " + secretString);
  211. System.out.println(" Encrypted String : " + desedeEncrypted);
  212. System.out.println(" Decrypted String : " + desedeDecrypted);
  213. System.out.println();
  214.  
  215. } catch (NoSuchAlgorithmException e) {
  216. }
  217. }
  218.  
  219.  
  220. /**
  221. * The following method is used for testing the String Encrypter class.
  222. * This method is responsible for encrypting and decrypting a sample
  223. * String using using a Pass Phrase.
  224. */
  225. public static void testUsingPassPhrase() {
  226.  
  227. System.out.println();
  228. System.out.println("+----------------------------------------+");
  229. System.out.println("| -- Test Using Pass Phrase Method -- |");
  230. System.out.println("+----------------------------------------+");
  231. System.out.println();
  232.  
  233. String secretString = "Pass phrase method";
  234. String passPhrase = "My Pass Phrase";
  235.  
  236. // Create encrypter/decrypter class
  237. StringEncrypter desEncrypter = new StringEncrypter(passPhrase);
  238.  
  239. // Encrypt the string
  240. String desEncrypted = desEncrypter.encrypt(secretString);
  241.  
  242. // Decrypt the string
  243. String desDecrypted = desEncrypter.decrypt(desEncrypted);
  244.  
  245. // Print out values
  246. System.out.println("PBEWithMD5AndDES Encryption algorithm");
  247. System.out.println(" Original String : " + secretString);
  248. System.out.println(" Encrypted String : " + desEncrypted);
  249. System.out.println(" Decrypted String : " + desDecrypted);
  250. System.out.println();
  251.  
  252. }
  253.  
  254.  
  255. /**
  256. * Sole entry point to the class and application used for testing the
  257. * String Encrypter class.
  258. * @param args Array of String arguments.
  259. */
  260. public static void main(String[] args) {
  261. testUsingSecretKey();
  262. testUsingPassPhrase();
  263. }
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement