PageRenderTime 781ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/aoi-2.8.1/SPManager/src/artofillusion/spmanager/StringEncrypter.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 312 lines | 170 code | 53 blank | 89 comment | 0 complexity | fc211deee6475b9e8c49b43254c48050 MD5 | raw file
  1. // -----------------------------------------------------------------------------
  2. // StringEncrypter.java
  3. // -----------------------------------------------------------------------------
  4. package artofillusion.spmanager;
  5. // CIPHER / GENERATORS
  6. import javax.crypto.Cipher;
  7. import javax.crypto.SecretKey;
  8. import javax.crypto.KeyGenerator;
  9. // KEY SPECIFICATIONS
  10. import java.security.spec.KeySpec;
  11. import java.security.spec.AlgorithmParameterSpec;
  12. import javax.crypto.spec.PBEKeySpec;
  13. import javax.crypto.SecretKeyFactory;
  14. import javax.crypto.spec.PBEParameterSpec;
  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. * The following example implements a class for encrypting and decrypting
  28. * strings using several Cipher algorithms. The class is created with a key and
  29. * can be used repeatedly to encrypt and decrypt strings using that key. Some
  30. * of the more popular algorithms are: Blowfish DES DESede PBEWithMD5AndDES
  31. * PBEWithMD5AndTripleDES TripleDES -----------------------------------------------------------------------------
  32. *
  33. *@author not me ! See http://www.idevelopment.info/, which hase several
  34. * java code examples
  35. *@created 23 mars 2004
  36. */
  37. public class StringEncrypter
  38. {
  39. Cipher ecipher;
  40. Cipher dcipher;
  41. /**
  42. * Constructor used to create this object. Responsible for setting and
  43. * initializing this object's encrypter and decrypter Chipher instances
  44. * given a Secret Key and algorithm.
  45. *
  46. *@param key Secret Key used to initialize both the encrypter and
  47. * decrypter instances.
  48. *@param algorithm Which algorithm to use for creating the encrypter and
  49. * decrypter instances.
  50. */
  51. StringEncrypter( SecretKey key, String algorithm )
  52. {
  53. try
  54. {
  55. ecipher = Cipher.getInstance( algorithm );
  56. dcipher = Cipher.getInstance( algorithm );
  57. ecipher.init( Cipher.ENCRYPT_MODE, key );
  58. dcipher.init( Cipher.DECRYPT_MODE, key );
  59. }
  60. catch ( NoSuchPaddingException e )
  61. {
  62. System.out.println( "EXCEPTION: NoSuchPaddingException" );
  63. }
  64. catch ( NoSuchAlgorithmException e )
  65. {
  66. System.out.println( "EXCEPTION: NoSuchAlgorithmException" );
  67. }
  68. catch ( InvalidKeyException e )
  69. {
  70. System.out.println( "EXCEPTION: InvalidKeyException" );
  71. }
  72. }
  73. /**
  74. * Constructor used to create this object. Responsible for setting and
  75. * initializing this object's encrypter and decrypter Chipher instances
  76. * given a Pass Phrase and algorithm.
  77. *
  78. *@param passPhrase Pass Phrase used to initialize both the encrypter and
  79. * decrypter instances.
  80. */
  81. StringEncrypter( String passPhrase )
  82. {
  83. // 8-bytes Salt
  84. byte[] salt = {
  85. (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
  86. (byte) 0x56, (byte) 0x34, (byte) 0xE3, (byte) 0x03
  87. };
  88. // Iteration count
  89. int iterationCount = 19;
  90. try
  91. {
  92. KeySpec keySpec = new PBEKeySpec( passPhrase.toCharArray(), salt, iterationCount );
  93. SecretKey key = SecretKeyFactory.getInstance( "PBEWithMD5AndDES" ).generateSecret( keySpec );
  94. ecipher = Cipher.getInstance( "PBEWithMD5AndDES" );
  95. // changed only this line of code, which went :
  96. // ecipher = Cipher.getInstance( key.getAlgorithm() ); and raised an exception
  97. dcipher = Cipher.getInstance( "PBEWithMD5AndDES" );
  98. // Prepare the parameters to the ciphers
  99. AlgorithmParameterSpec paramSpec = new PBEParameterSpec( salt, iterationCount );
  100. ecipher.init( Cipher.ENCRYPT_MODE, key, paramSpec );
  101. dcipher.init( Cipher.DECRYPT_MODE, key, paramSpec );
  102. }
  103. catch ( InvalidAlgorithmParameterException e )
  104. {
  105. System.out.println( "EXCEPTION: InvalidAlgorithmParameterException" );
  106. e.printStackTrace();
  107. }
  108. catch ( InvalidKeySpecException e )
  109. {
  110. System.out.println( "EXCEPTION: InvalidKeySpecException" );
  111. e.printStackTrace();
  112. }
  113. catch ( NoSuchPaddingException e )
  114. {
  115. System.out.println( "EXCEPTION: NoSuchPaddingException" );
  116. e.printStackTrace();
  117. }
  118. catch ( NoSuchAlgorithmException e )
  119. {
  120. System.out.println( "EXCEPTION: NoSuchAlgorithmException" );
  121. e.printStackTrace();
  122. }
  123. catch ( InvalidKeyException e )
  124. {
  125. System.out.println( "EXCEPTION: InvalidKeyException" );
  126. e.printStackTrace();
  127. }
  128. }
  129. /**
  130. * Takes a single String as an argument and returns an Encrypted version of
  131. * that String.
  132. *
  133. *@param str String to be encrypted
  134. *@return <code>String</code> Encrypted version of the provided String
  135. */
  136. public String encrypt( String str )
  137. {
  138. try
  139. {
  140. // Encode the string into bytes using utf-8
  141. byte[] utf8 = str.getBytes( "UTF8" );
  142. // Encrypt
  143. byte[] enc = ecipher.doFinal( utf8 );
  144. // Encode bytes to base64 to get a string
  145. return new sun.misc.BASE64Encoder().encode( enc );
  146. }
  147. catch (Exception e) {}
  148. return null;
  149. }
  150. /**
  151. * Takes a encrypted String as an argument, decrypts and returns the
  152. * decrypted String.
  153. *
  154. *@param str Encrypted String to be decrypted
  155. *@return <code>String</code> Decrypted version of the provided String
  156. */
  157. public String decrypt( String str )
  158. {
  159. try
  160. {
  161. // Decode base64 to get bytes
  162. byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer( str );
  163. // Decrypt
  164. byte[] utf8 = dcipher.doFinal( dec );
  165. // Decode using utf-8
  166. return new String( utf8, "UTF8" );
  167. }
  168. catch (Exception e) {}
  169. return null;
  170. }
  171. /**
  172. * The following method is used for testing the String Encrypter class.
  173. * This method is responsible for encrypting and decrypting a sample String
  174. * using several symmetric temporary Secret Keys.
  175. */
  176. public static void testUsingSecretKey()
  177. {
  178. try
  179. {
  180. System.out.println();
  181. System.out.println( "+----------------------------------------+" );
  182. System.out.println( "| -- Test Using Secret Key Method -- |" );
  183. System.out.println( "+----------------------------------------+" );
  184. System.out.println();
  185. String secretString = "Attack at dawn!";
  186. // Generate a temporary key for this example. In practice, you would
  187. // save this key somewhere. Keep in mind that you can also use a
  188. // Pass Phrase.
  189. SecretKey desKey = KeyGenerator.getInstance( "DES" ).generateKey();
  190. SecretKey blowfishKey = KeyGenerator.getInstance( "Blowfish" ).generateKey();
  191. SecretKey desedeKey = KeyGenerator.getInstance( "DESede" ).generateKey();
  192. // Create encrypter/decrypter class
  193. StringEncrypter desEncrypter = new StringEncrypter( desKey, desKey.getAlgorithm() );
  194. StringEncrypter blowfishEncrypter = new StringEncrypter( blowfishKey, blowfishKey.getAlgorithm() );
  195. StringEncrypter desedeEncrypter = new StringEncrypter( desedeKey, desedeKey.getAlgorithm() );
  196. // Encrypt the string
  197. String desEncrypted = desEncrypter.encrypt( secretString );
  198. String blowfishEncrypted = blowfishEncrypter.encrypt( secretString );
  199. String desedeEncrypted = desedeEncrypter.encrypt( secretString );
  200. // Decrypt the string
  201. String desDecrypted = desEncrypter.decrypt( desEncrypted );
  202. String blowfishDecrypted = blowfishEncrypter.decrypt( blowfishEncrypted );
  203. String desedeDecrypted = desedeEncrypter.decrypt( desedeEncrypted );
  204. // Print out values
  205. System.out.println( desKey.getAlgorithm() + " Encryption algorithm" );
  206. System.out.println( " Original String : " + secretString );
  207. System.out.println( " Encrypted String : " + desEncrypted );
  208. System.out.println( " Decrypted String : " + desDecrypted );
  209. System.out.println();
  210. System.out.println( blowfishKey.getAlgorithm() + " Encryption algorithm" );
  211. System.out.println( " Original String : " + secretString );
  212. System.out.println( " Encrypted String : " + blowfishEncrypted );
  213. System.out.println( " Decrypted String : " + blowfishDecrypted );
  214. System.out.println();
  215. System.out.println( desedeKey.getAlgorithm() + " Encryption algorithm" );
  216. System.out.println( " Original String : " + secretString );
  217. System.out.println( " Encrypted String : " + desedeEncrypted );
  218. System.out.println( " Decrypted String : " + desedeDecrypted );
  219. System.out.println();
  220. }
  221. catch ( NoSuchAlgorithmException e )
  222. {
  223. }
  224. }
  225. /**
  226. * The following method is used for testing the String Encrypter class.
  227. * This method is responsible for encrypting and decrypting a sample String
  228. * using using a Pass Phrase.
  229. */
  230. public static void testUsingPassPhrase()
  231. {
  232. System.out.println();
  233. System.out.println( "+----------------------------------------+" );
  234. System.out.println( "| -- Test Using Pass Phrase Method -- |" );
  235. System.out.println( "+----------------------------------------+" );
  236. System.out.println();
  237. String secretString = "Attack at dawn!";
  238. String passPhrase = "My Pass Phrase";
  239. // Create encrypter/decrypter class
  240. StringEncrypter desEncrypter = new StringEncrypter( passPhrase );
  241. // Encrypt the string
  242. String desEncrypted = desEncrypter.encrypt( secretString );
  243. // Decrypt the string
  244. String desDecrypted = desEncrypter.decrypt( desEncrypted );
  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. * public static void main( String[] args )
  254. * {
  255. * testUsingSecretKey();
  256. * testUsingPassPhrase();
  257. * }
  258. */
  259. }