Guest User

Untitled

a guest
Apr 1st, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. import javax.crypto.Cipher;
  2. import javax.crypto.SecretKeyFactory;
  3. import javax.crypto.SecretKey;
  4. import javax.crypto.spec.IvParameterSpec;
  5. import javax.crypto.spec.PBEParameterSpec;
  6. import javax.crypto.spec.SecretKeySpec;
  7. import javax.crypto.spec.PBEKeySpec;
  8. import java.security.spec.KeySpec;
  9. import java.security.AlgorithmParameters;
  10. import java.security.InvalidAlgorithmParameterException;
  11. import java.security.InvalidKeyException;
  12. import java.security.NoSuchAlgorithmException;
  13. import java.security.spec.AlgorithmParameterSpec;
  14. import java.security.spec.InvalidKeySpecException;
  15. import java.security.spec.InvalidParameterSpecException;
  16. import javax.crypto.NoSuchPaddingException;
  17.  
  18. import java.io.ByteArrayInputStream;
  19. import java.io.IOException;
  20. import java.io.ObjectInputStream;
  21. import java.io.UnsupportedEncodingException;
  22. import javax.crypto.IllegalBlockSizeException;
  23. import javax.crypto.BadPaddingException;
  24. /**
  25. * This Crypt class constructs javax.crypto.Cipher objects with password-based data encryption, PBEWithSHA1AndDESEDE.
  26. * The salt is currently constant in this class, but it could be modified to take a salt as part of a constructor.
  27. * This class performs no exception handling, all potential exceptions are thrown.
  28. *
  29. * <h6>This code is released for educational purposes only and does not come with any
  30. * warranty for the merchantability or fitness for any particular purpose.</h6>
  31. * @author Geoffery Miller
  32. * @version 0.1
  33. */
  34. public class Crypt {
  35. private Cipher encrypt, decrypt;
  36. /**
  37. * Crypt objects require a password secret.
  38. * To create a Crypt object, you will need to provide the password that it will use to
  39. * encrypt and decrypt data with. The salt used by Crypt is constant.
  40. *
  41. * @param password The password to construct the encryption and decryption ciphers
  42. * @throws InvalidKeySpecException
  43. * @throws NoSuchAlgorithmException
  44. * @throws NoSuchPaddingException
  45. * @throws UnsupportedEncodingException This will only occur if UTF-8 is not supported on your system
  46. * @throws InvalidKeyException
  47. * @throws InvalidParameterSpecException
  48. * @throws InvalidAlgorithmParameterException
  49. */
  50. public Crypt(String password) throws InvalidKeySpecException,
  51. NoSuchAlgorithmException,
  52. NoSuchPaddingException,
  53. UnsupportedEncodingException,
  54. InvalidKeyException,
  55. InvalidParameterSpecException,
  56. InvalidAlgorithmParameterException {
  57. String algorithm = "PBEWithSHA1AndDESede";
  58. int iterations = 1000;
  59. char[] pass_c = password.toCharArray();
  60. byte[] salt_b = getSalt();
  61. SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
  62. KeySpec spec = new PBEKeySpec(pass_c, salt_b, iterations, 128);
  63. SecretKey secret = factory.generateSecret(spec);
  64.  
  65. AlgorithmParameterSpec params = new PBEParameterSpec(salt_b,iterations);
  66.  
  67. encrypt = Cipher.getInstance(secret.getAlgorithm());
  68. encrypt.init(Cipher.ENCRYPT_MODE, secret, params);
  69.  
  70. decrypt = Cipher.getInstance(secret.getAlgorithm());
  71. decrypt.init(Cipher.DECRYPT_MODE, secret, params);
  72. }
  73.  
  74. /**
  75. * This method will take an array of unencrypted bytes and encrypt them
  76. * using the password provided to create this Crypt object. The returned
  77. * byte array will be encrypted.
  78. * @param data The data to encrypt using this Crypt object
  79. * @return The encrypted data
  80. * @throws IllegalBlockSizeException
  81. * @throws BadPaddingException
  82. */
  83. public byte[] encrypt(byte[] data) throws IllegalBlockSizeException,
  84. BadPaddingException {
  85. byte[] enc = encrypt.doFinal(data);
  86. return enc;
  87. }
  88.  
  89. /**
  90. * This method takes a byte array of encrypted bytes and returns a
  91. * byte array of decrypted bytes, using the password provided to create
  92. * this Crypt object.
  93. * @param data The data to decrypt using this Cypher object
  94. * @return The decrypted data
  95. * @throws IllegalBlockSizeException
  96. * @throws BadPaddingException
  97. */
  98. public byte[] decrypt(byte[] data) throws IllegalBlockSizeException,
  99. BadPaddingException {
  100. byte[] dec = decrypt.doFinal(data);
  101. return dec;
  102. }
  103.  
  104. private byte[] getSalt() throws UnsupportedEncodingException {
  105. byte[] salt = { (byte)0x0,(byte)0x0,(byte)0x0,(byte)0x0,
  106. (byte)0x0,(byte)0x0,(byte)0x0,(byte)0x0 };
  107. return salt;
  108. }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment