Subversion Repositories SmartDukaan

Rev

Rev 1034 | Rev 2146 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
555 chandransh 1
package in.shop2020.serving.utils;
2
 
3
import java.io.UnsupportedEncodingException;
4
import java.security.spec.AlgorithmParameterSpec;
5
import java.security.spec.KeySpec;
6
 
7
import javax.crypto.Cipher;
8
import javax.crypto.IllegalBlockSizeException;
9
import javax.crypto.SecretKey;
10
import javax.crypto.SecretKeyFactory;
11
import javax.crypto.spec.PBEKeySpec;
12
import javax.crypto.spec.PBEParameterSpec;
13
 
14
import org.apache.commons.codec.binary.Base64;
15
 
16
public class DesEncrypter {
17
	Cipher ecipher;
18
	Cipher dcipher;
19
 
20
	// 8-byte Salt
21
	byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
22
			(byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03 };
23
 
24
	// Iteration count
25
	int iterationCount = 19;
26
 
27
	public DesEncrypter(String passPhrase) {
28
		try {
29
			// Create the key
30
			KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt,
31
					iterationCount);
32
			SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES")
33
					.generateSecret(keySpec);
34
			ecipher = Cipher.getInstance(key.getAlgorithm());
35
			dcipher = Cipher.getInstance(key.getAlgorithm());
36
 
37
			// Prepare the parameter to the ciphers
38
			AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
39
 
40
			// Create the ciphers
41
			ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
42
			dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
43
		} catch (java.security.InvalidAlgorithmParameterException e) {
44
			e.printStackTrace();
45
		} catch (java.security.spec.InvalidKeySpecException e) {
46
			e.printStackTrace();
47
		} catch (javax.crypto.NoSuchPaddingException e) {
48
			e.printStackTrace();
49
		} catch (java.security.NoSuchAlgorithmException e) {
50
			e.printStackTrace();
51
		} catch (java.security.InvalidKeyException e) {
52
			e.printStackTrace();
53
		}
54
	}
55
 
56
	public String encrypt(String str) {
57
		try {
58
			// Encode the string into bytes using utf-8
59
			byte[] utf8 = str.getBytes("UTF8");
60
 
61
			// Encrypt
62
			byte[] enc = ecipher.doFinal(utf8);
63
 
64
			// Encode bytes to base64 to get a string
65
			return Base64.encodeBase64URLSafeString(enc);
66
		} catch (javax.crypto.BadPaddingException e) {
67
			e.printStackTrace();
68
		} catch (IllegalBlockSizeException e) {
69
			e.printStackTrace();
70
		} catch (UnsupportedEncodingException e) {
71
			e.printStackTrace();
72
		}
73
		return null;
74
	}
75
 
76
	public String decrypt(String str) {
77
		try {
78
			// Decode base64 encoded string to get bytes
79
			byte[] dec = Base64.decodeBase64(str);
80
 
81
			// Decrypt
82
			byte[] utf8 = dcipher.doFinal(dec);
83
 
84
			// Decode using utf-8
85
			return new String(utf8, "UTF8");
86
		} catch (javax.crypto.BadPaddingException e) {
87
			e.printStackTrace();
88
		} catch (IllegalBlockSizeException e) {
89
			e.printStackTrace();
90
		} catch (UnsupportedEncodingException e) {
91
			e.printStackTrace();
92
		} catch (java.io.IOException e) {
93
			e.printStackTrace();
94
		}
95
		return null;
96
	}
2047 chandransh 97
 
98
	public static void main(String[] args){
99
		DesEncrypter desEncrypter = new DesEncrypter("shop2020");
100
		System.out.println(desEncrypter.decrypt("BVl-z9SkaLo"));
101
	}
555 chandransh 102
}