Subversion Repositories bacoAlunos

Rev

Rev 1306 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
156 jmachado 1
package jomm.utils;
2
 
3
import javax.crypto.Cipher;
4
import javax.crypto.SecretKey;
5
import javax.crypto.IllegalBlockSizeException;
6
import javax.crypto.KeyGenerator;
7
import javax.crypto.spec.SecretKeySpec;
8
import java.io.UnsupportedEncodingException;
9
import java.security.NoSuchAlgorithmException;
10
 
11
/**
12
 * @author Jorge Machado
13
 * @date 20/Mai/2008
14
 * @see jomm.utils
15
 */
16
public class DesUtils
17
{
18
    static 1.5.0/docs/api/javax/crypto/Cipher.html">Cipher ecipher;
19
    static 1.5.0/docs/api/javax/crypto/Cipher.html">Cipher dcipher;
20
 
21
    static 1.5.0/docs/api/javax/crypto/SecretKey.html">SecretKey key;
22
 
23
 
24
    public static byte[] addParity(byte[] in) {
25
        byte[] result = new byte[8];
26
 
27
        // Keeps track of the bit position in the result
28
        int resultIx = 1;
29
 
30
        // Used to keep track of the number of 1 bits in each 7-bit chunk
31
        int bitCount = 0;
32
 
33
        // Process each of the 56 bits
34
        for (int i=0; i<56; i++) {
35
            // Get the bit at bit position i
36
            boolean bit = (in[6-i/8]&(1<<(i%8))) > 0;
37
 
38
            // If set, set the corresponding bit in the result
39
            if (bit) {
40
                result[7-resultIx/8] |= (1<<(resultIx%8))&0xFF;
41
                bitCount++;
42
            }
43
 
44
            // Set the parity bit after every 7 bits
45
            if ((i+1) % 7 == 0) {
46
                if (bitCount % 2 == 0) {
47
                    // Set low-order bit (parity bit) if bit count is even
48
                    result[7-resultIx/8] |= 1;
49
                }
50
                resultIx++;
51
                bitCount = 0;
52
            }
53
            resultIx++;
54
        }
55
        return result;
56
    }
57
 
58
    // Get the 56-bit value
59
 
60
 
61
    private static DesUtils desUtils;
62
 
63
    private DesUtils()
64
    {
65
        try {
66
            byte[] raw = new byte[]{0x01, 0x72, 0x43, 0x3E, 0x1C, 0x7A, 0x55};
67
            byte[] keyBytes = addParity(raw);
68
            key = new 1.5.0/docs/api/javax/crypto/spec/SecretKeySpec.html">SecretKeySpec(keyBytes, "DES");
69
            ecipher = 1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.getInstance("DES");
70
            dcipher = 1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.getInstance("DES");
71
            ecipher.init(1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.ENCRYPT_MODE, key);
72
            dcipher.init(1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.DECRYPT_MODE, key);
73
 
74
        } catch (javax.crypto.1.5.0/docs/api/javax/crypto/NoSuchPaddingException.html">NoSuchPaddingException e) {
75
            e.printStackTrace();
76
        } catch (java.security.1.5.0/docs/api/java/security/NoSuchAlgorithmException.html">NoSuchAlgorithmException e) {
77
            e.printStackTrace();
78
        } catch (java.security.5+0%2Fdocs%2Fapi+InvalidKeyException">InvalidKeyException e) {
79
            e.printStackTrace();
80
        }
81
    }
82
 
83
    public static DesUtils getInstance()
84
    {
85
        if(desUtils == null)
86
            desUtils = new DesUtils();
87
        return desUtils;
88
    }
89
 
90
    public 1.5.0/docs/api/java/lang/String.html">String encrypt(1.5.0/docs/api/java/lang/String.html">String str) {
91
        try {
92
            // Encode the string into bytes using utf-8
93
            byte[] utf8 = str.getBytes("UTF8");
94
 
95
            // Encrypt
96
            byte[] enc = ecipher.doFinal(utf8);
97
 
98
            // Encode bytes to base64 to get a string
99
            return new sun.misc.BASE64Encoder().encode(enc);
100
        } catch (javax.crypto.1.5.0/docs/api/javax/crypto/BadPaddingException.html">BadPaddingException e) {
101
        } catch (1.5.0/docs/api/javax/crypto/IllegalBlockSizeException.html">IllegalBlockSizeException e) {
102
        } catch (1.5.0/docs/api/java/io/UnsupportedEncodingException.html">UnsupportedEncodingException e) {
103
        } catch (java.io.1.5.0/docs/api/java/io/IOException.html">IOException e) {
104
        }
105
        return null;
106
    }
107
 
108
    public 1.5.0/docs/api/java/lang/String.html">String decrypt(1.5.0/docs/api/java/lang/String.html">String str) {
109
        try {
110
            // Decode base64 to get bytes
111
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
112
 
113
            // Decrypt
114
            byte[] utf8 = dcipher.doFinal(dec);
115
 
116
            // Decode using utf-8
117
            return new 1.5.0/docs/api/java/lang/String.html">String(utf8, "UTF8");
118
        } catch (javax.crypto.1.5.0/docs/api/javax/crypto/BadPaddingException.html">BadPaddingException e) {
119
        } catch (1.5.0/docs/api/javax/crypto/IllegalBlockSizeException.html">IllegalBlockSizeException e) {
120
        } catch (1.5.0/docs/api/java/io/UnsupportedEncodingException.html">UnsupportedEncodingException e) {
121
        } catch (java.io.1.5.0/docs/api/java/io/IOException.html">IOException e) {
122
        }
123
        return null;
124
    }
125
 
126
    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args)
127
    {
128
        1.5.0/docs/api/java/lang/String.html">String code = DesUtils.getInstance().encrypt("jorge machado");
129
        1.5.0/docs/api/java/lang/System.html">System.out.println(code);
130
        1.5.0/docs/api/java/lang/System.html">System.out.println(DesUtils.getInstance().decrypt(code));
131
    }
132
}