Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21478 rajender 1
package com.saholic.profittill.Utils;
2
 
3
import android.util.Base64;
4
 
5
import java.io.UnsupportedEncodingException;
6
import java.security.spec.AlgorithmParameterSpec;
7
import java.security.spec.KeySpec;
8
 
9
import javax.crypto.Cipher;
10
import javax.crypto.IllegalBlockSizeException;
11
import javax.crypto.SecretKey;
12
import javax.crypto.SecretKeyFactory;
13
import javax.crypto.spec.PBEKeySpec;
14
import javax.crypto.spec.PBEParameterSpec;
15
 
16
public class DESEncrypter {
17
 
18
    Cipher ecipher;
19
    Cipher dcipher;
20
 
21
    byte[] salt = {(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
22
            (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03};
23
 
24
    int iterationCount = 19;
25
 
26
    public DESEncrypter(String passPhrase) {
27
        try {
28
            KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt,
29
                    iterationCount);
30
            SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES")
31
                    .generateSecret(keySpec);
32
            ecipher = Cipher.getInstance(key.getAlgorithm());
33
            dcipher = Cipher.getInstance(key.getAlgorithm());
34
 
35
            // Prepare the parameter to the ciphers
36
            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
37
 
38
            // Create the ciphers
39
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
40
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
41
        } catch (java.security.InvalidAlgorithmParameterException e) {
42
            e.printStackTrace();
43
        } catch (java.security.spec.InvalidKeySpecException e) {
44
            e.printStackTrace();
45
        } catch (javax.crypto.NoSuchPaddingException e) {
46
            e.printStackTrace();
47
        } catch (java.security.NoSuchAlgorithmException e) {
48
            e.printStackTrace();
49
        } catch (java.security.InvalidKeyException e) {
50
            e.printStackTrace();
51
        }
52
    }
53
 
54
    public String encrypt(String str) {
55
        try {
56
            byte[] utf8 = str.getBytes("UTF8");
57
            byte[] enc = ecipher.doFinal(utf8);
58
            return Base64.encodeToString(enc, Base64.URL_SAFE);
59
        } catch (javax.crypto.BadPaddingException e) {
60
            e.printStackTrace();
61
        } catch (IllegalBlockSizeException e) {
62
            e.printStackTrace();
63
        } catch (UnsupportedEncodingException e) {
64
            e.printStackTrace();
65
        }
66
        return null;
67
    }
68
 
69
    public String decrypt(String str) {
70
        try {
71
            byte[] dec = Base64.decode(str, android.util.Base64.DEFAULT);
72
            byte[] utf8 = dcipher.doFinal(dec);
73
            return new String(utf8, "UTF8");
74
        } catch (javax.crypto.BadPaddingException e) {
75
            e.printStackTrace();
76
        } catch (IllegalBlockSizeException e) {
77
            e.printStackTrace();
78
        } catch (UnsupportedEncodingException e) {
79
            e.printStackTrace();
80
        }
81
        return null;
82
    }
83
}
84