Subversion Repositories SmartDukaan

Rev

Rev 2146 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2146 Rev 2942
Line 10... Line 10...
10
import javax.crypto.SecretKeyFactory;
10
import javax.crypto.SecretKeyFactory;
11
import javax.crypto.spec.PBEKeySpec;
11
import javax.crypto.spec.PBEKeySpec;
12
import javax.crypto.spec.PBEParameterSpec;
12
import javax.crypto.spec.PBEParameterSpec;
13
 
13
 
14
import org.apache.commons.codec.binary.Base64;
14
import org.apache.commons.codec.binary.Base64;
-
 
15
import org.apache.log4j.Logger;
15
 
16
 
16
public class DesEncrypter {
17
public class DesEncrypter {
-
 
18
    
-
 
19
    private static Logger logger = Logger.getLogger(DesEncrypter.class);
-
 
20
    
17
	Cipher ecipher;
21
	Cipher ecipher;
18
	Cipher dcipher;
22
	Cipher dcipher;
19
 
23
 
20
	// 8-byte Salt
24
	// 8-byte Salt
21
	byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
25
	byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
Line 39... Line 43...
39
 
43
 
40
			// Create the ciphers
44
			// Create the ciphers
41
			ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
45
			ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
42
			dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
46
			dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
43
		} catch (java.security.InvalidAlgorithmParameterException e) {
47
		} catch (java.security.InvalidAlgorithmParameterException e) {
44
			e.printStackTrace();
48
			logger.error("Unable to initialize the cipher", e);
45
		} catch (java.security.spec.InvalidKeySpecException e) {
49
		} catch (java.security.spec.InvalidKeySpecException e) {
46
			e.printStackTrace();
50
		    logger.error("Unable to initialize the cipher", e);
47
		} catch (javax.crypto.NoSuchPaddingException e) {
51
		} catch (javax.crypto.NoSuchPaddingException e) {
48
			e.printStackTrace();
52
		    logger.error("Unable to initialize the cipher", e);
49
		} catch (java.security.NoSuchAlgorithmException e) {
53
		} catch (java.security.NoSuchAlgorithmException e) {
50
			e.printStackTrace();
54
		    logger.error("Unable to initialize the cipher", e);
51
		} catch (java.security.InvalidKeyException e) {
55
		} catch (java.security.InvalidKeyException e) {
52
			e.printStackTrace();
56
		    logger.error("Unable to initialize the cipher", e);
53
		}
57
		}
54
	}
58
	}
55
 
59
 
56
	public String encrypt(String str) {
60
	public String encrypt(String str) {
57
		try {
61
		try {
Line 62... Line 66...
62
			byte[] enc = ecipher.doFinal(utf8);
66
			byte[] enc = ecipher.doFinal(utf8);
63
 
67
 
64
			// Encode bytes to base64 to get a string
68
			// Encode bytes to base64 to get a string
65
			return Base64.encodeBase64URLSafeString(enc);
69
			return Base64.encodeBase64URLSafeString(enc);
66
		} catch (javax.crypto.BadPaddingException e) {
70
		} catch (javax.crypto.BadPaddingException e) {
67
			e.printStackTrace();
71
		    logger.error("Encryption failed because of bad padding", e);
68
		} catch (IllegalBlockSizeException e) {
72
		} catch (IllegalBlockSizeException e) {
69
			e.printStackTrace();
73
		    logger.error("Encryption failed because of illegal block size", e);
70
		} catch (UnsupportedEncodingException e) {
74
		} catch (UnsupportedEncodingException e) {
71
			e.printStackTrace();
75
		    logger.error("Encryption failed because the underlying encoding is unsupported", e);
72
		}
76
		}
73
		return null;
77
		return null;
74
	}
78
	}
75
 
79
 
76
	public String decrypt(String str) {
80
	public String decrypt(String str) {
Line 82... Line 86...
82
			byte[] utf8 = dcipher.doFinal(dec);
86
			byte[] utf8 = dcipher.doFinal(dec);
83
 
87
 
84
			// Decode using utf-8
88
			// Decode using utf-8
85
			return new String(utf8, "UTF8");
89
			return new String(utf8, "UTF8");
86
		} catch (javax.crypto.BadPaddingException e) {
90
		} catch (javax.crypto.BadPaddingException e) {
87
			e.printStackTrace();
91
		    logger.error("Encryption failed because of bad padding", e);
88
		} catch (IllegalBlockSizeException e) {
92
		} catch (IllegalBlockSizeException e) {
89
			e.printStackTrace();
93
		    logger.error("Encryption failed because of illegal block size", e);
90
		} catch (UnsupportedEncodingException e) {
94
		} catch (UnsupportedEncodingException e) {
91
			e.printStackTrace();
95
		    logger.error("Encryption failed because the underlying encoding is unsupported", e);
92
		}
96
		}
93
		return null;
97
		return null;
94
	}
98
	}
95
	
99
	
96
	public static void main(String[] args){
100
	public static void main(String[] args){