Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11890 kshitij.so 1
package in.shop2020.util;
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
import org.apache.log4j.Logger;
16
 
17
public class DesEncrypter {
18
 
19
    private static Logger logger = Logger.getLogger(DesEncrypter.class);
20
 
21
    Cipher ecipher;
22
    Cipher dcipher;
23
 
24
    // 8-byte Salt
25
    byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
26
            (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03 };
27
 
28
    // Iteration count
29
    int iterationCount = 19;
30
 
31
    public DesEncrypter(String passPhrase) {
32
        try {
33
            // Create the key
34
            KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt,
35
                    iterationCount);
36
            SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES")
37
                    .generateSecret(keySpec);
38
            ecipher = Cipher.getInstance(key.getAlgorithm());
39
            dcipher = Cipher.getInstance(key.getAlgorithm());
40
 
41
            // Prepare the parameter to the ciphers
42
            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
43
 
44
            // Create the ciphers
45
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
46
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
47
        } catch (java.security.InvalidAlgorithmParameterException e) {
48
            logger.error("Unable to initialize the cipher", e);
49
        } catch (java.security.spec.InvalidKeySpecException e) {
50
            logger.error("Unable to initialize the cipher", e);
51
        } catch (javax.crypto.NoSuchPaddingException e) {
52
            logger.error("Unable to initialize the cipher", e);
53
        } catch (java.security.NoSuchAlgorithmException e) {
54
            logger.error("Unable to initialize the cipher", e);
55
        } catch (java.security.InvalidKeyException e) {
56
            logger.error("Unable to initialize the cipher", e);
57
        }
58
    }
59
 
60
    public String encrypt(String str) {
61
        try {
62
            // Encode the string into bytes using utf-8
63
            byte[] utf8 = str.getBytes("UTF8");
64
 
65
            // Encrypt
66
            byte[] enc = ecipher.doFinal(utf8);
67
 
68
            // Encode bytes to base64 to get a string
69
            return Base64.encodeBase64URLSafeString(enc);
70
        } catch (javax.crypto.BadPaddingException e) {
71
            logger.error("Encryption failed because of bad padding", e);
72
        } catch (IllegalBlockSizeException e) {
73
            logger.error("Encryption failed because of illegal block size", e);
74
        } catch (UnsupportedEncodingException e) {
75
            logger.error("Encryption failed because the underlying encoding is unsupported", e);
76
        }
77
        return null;
78
    }
79
 
80
    public String decrypt(String str) {
81
        try {
82
            // Decode base64 encoded string to get bytes
83
            byte[] dec = Base64.decodeBase64(str);
84
 
85
            // Decrypt
86
            byte[] utf8 = dcipher.doFinal(dec);
87
 
88
            // Decode using utf-8
89
            return new String(utf8, "UTF8");
90
        } catch (javax.crypto.BadPaddingException e) {
91
            logger.error("Encryption failed because of bad padding", e);
92
        } catch (IllegalBlockSizeException e) {
93
            logger.error("Encryption failed because of illegal block size", e);
94
        } catch (UnsupportedEncodingException e) {
95
            logger.error("Encryption failed because the underlying encoding is unsupported", e);
96
        }
97
        return null;
98
    }
99
 
100
    public static void main(String[] args){
101
        DesEncrypter desEncrypter = new DesEncrypter("saholic");
102
        System.out.println(desEncrypter.encrypt("pud8SQuI6p"));
103
    }
104
}