View difference between Paste ID: 2G7xyada and vWPc8Qby
SHOW: | | - or go back to the newest paste.
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
31+
32
 */
33
public class Crypt {
34
	private Cipher encrypt, decrypt;
35
	/** 
36
	 * Crypt objects require a password secret.  
37
	 * To create a Crypt object, you will need to provide the password that it will use to 
38
	 * encrypt and decrypt data with. The salt used by Crypt is constant.
39
	 * 
40
	 * @param password  The password to construct the encryption and decryption ciphers
41
	 * @throws InvalidKeySpecException
42
	 * @throws NoSuchAlgorithmException
43
	 * @throws NoSuchPaddingException
44
	 * @throws UnsupportedEncodingException This will only occur if UTF-8 is not supported on your system
45
	 * @throws InvalidKeyException
46
	 * @throws InvalidParameterSpecException
47
	 * @throws InvalidAlgorithmParameterException
48
	 */
49
	public Crypt(String password) throws	InvalidKeySpecException, 
50
										NoSuchAlgorithmException, 
51
										NoSuchPaddingException, 
52
										UnsupportedEncodingException, 
53
										InvalidKeyException,
54
										InvalidParameterSpecException,
55
										InvalidAlgorithmParameterException	{
56
		String algorithm = "PBEWithSHA1AndDESede";
57
		int iterations = 1000;
58
		char[] pass_c = password.toCharArray();
59
		byte[] salt_b = getSalt();
60
		SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
61
		KeySpec spec = new PBEKeySpec(pass_c, salt_b, iterations, 128);
62
		SecretKey secret = factory.generateSecret(spec);
63
		
64
		AlgorithmParameterSpec params = new PBEParameterSpec(salt_b,iterations);
65
66
		encrypt = Cipher.getInstance(secret.getAlgorithm());
67
		encrypt.init(Cipher.ENCRYPT_MODE, secret, params);
68
69
		decrypt = Cipher.getInstance(secret.getAlgorithm());
70
		decrypt.init(Cipher.DECRYPT_MODE, secret, params);
71
	}
72
73
	/**
74
	 * This method will take an array of unencrypted bytes and encrypt them
75
	 * using the password provided to create this Crypt object.  The returned 
76
	 * byte array will be encrypted.
77
	 * @param data The data to encrypt using this Crypt object
78
	 * @return The encrypted data 
79
	 * @throws IllegalBlockSizeException
80
	 * @throws BadPaddingException
81
	 */
82
	public byte[] encrypt(byte[] data) throws	IllegalBlockSizeException, 
83
												BadPaddingException {
84
		byte[] enc = encrypt.doFinal(data);
85
		return enc;
86
	}
87
88
	/** 
89
	 * This method takes a byte array of encrypted bytes and returns a 
90
	 * byte array of decrypted bytes, using the password provided to create
91
	 * this Crypt object.
92
	 * @param data The data to decrypt using this Cypher object
93
	 * @return The decrypted data 
94
	 * @throws IllegalBlockSizeException
95
	 * @throws BadPaddingException
96
	 */
97
	public byte[] decrypt(byte[] data) throws	IllegalBlockSizeException,
98
												BadPaddingException {
99
		byte[] dec = decrypt.doFinal(data);
100
		return dec;
101
	}
102
103
	private byte[] getSalt() throws UnsupportedEncodingException {
104
		byte[] salt = {	(byte)0x0,(byte)0x0,(byte)0x0,(byte)0x0,
105
						(byte)0x0,(byte)0x0,(byte)0x0,(byte)0x0	};
106
		return salt;
107
	}
108
	
109
}