Rev 156 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package jomm.utils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
/**
* @author Jorge Machado
* @date 20/Mai/2008
* @see jomm.utils
*/
public class DesUtils
{
static 1.5.0/docs/api/javax/crypto/Cipher.html">Cipher ecipher
;
static 1.5.0/docs/api/javax/crypto/Cipher.html">Cipher dcipher
;
static 1.5.0/docs/api/javax/crypto/SecretKey.html">SecretKey key
;
public static byte[] addParity
(byte[] in
) {
byte[] result =
new byte[8
];
// Keeps track of the bit position in the result
int resultIx =
1;
// Used to keep track of the number of 1 bits in each 7-bit chunk
int bitCount =
0;
// Process each of the 56 bits
for (int i=
0; i
<56; i++
) {
// Get the bit at bit position i
boolean bit =
(in
[6-i/8
]&(1
<<(i
%8
))) > 0;
// If set, set the corresponding bit in the result
if (bit
) {
result
[7-resultIx/8
] |=
(1
<<(resultIx
%8
))&0xFF
;
bitCount++
;
}
// Set the parity bit after every 7 bits
if ((i+1
) % 7 == 0
) {
if (bitCount
% 2 ==
0) {
// Set low-order bit (parity bit) if bit count is even
result
[7-resultIx/8
] |=
1;
}
resultIx++
;
bitCount =
0;
}
resultIx++
;
}
return result
;
}
// Get the 56-bit value
private static DesUtils desUtils
;
private DesUtils
()
{
try {
byte[] raw =
new byte[]{0x01, 0x72, 0x43, 0x3E, 0x1C, 0x7A, 0x55
};
byte[] keyBytes = addParity
(raw
);
key =
new 1.5.0/docs/api/javax/crypto/spec/SecretKeySpec.html">SecretKeySpec(keyBytes,
"DES");
ecipher =
1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.
getInstance("DES");
dcipher =
1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.
getInstance("DES");
ecipher.
init(1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.
ENCRYPT_MODE, key
);
dcipher.
init(1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.
DECRYPT_MODE, key
);
} catch (javax.
crypto.
1.5.0/docs/api/javax/crypto/NoSuchPaddingException.html">NoSuchPaddingException e
) {
e.
printStackTrace();
} catch (java.
security.
1.5.0/docs/api/java/security/NoSuchAlgorithmException.html">NoSuchAlgorithmException e
) {
e.
printStackTrace();
} catch (java.
security.
5+0%2Fdocs%2Fapi+InvalidKeyException">InvalidKeyException e
) {
e.
printStackTrace();
}
}
public static DesUtils getInstance
()
{
if(desUtils ==
null)
desUtils =
new DesUtils
();
return desUtils
;
}
public 1.5.0/docs/api/java/lang/String.html">String encrypt
(1.5.0/docs/api/java/lang/String.html">String str
) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.
getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.
doFinal(utf8
);
// Encode bytes to base64 to get a string
return new sun.
misc.
BASE64Encoder().
encode(enc
);
} catch (javax.
crypto.
1.5.0/docs/api/javax/crypto/BadPaddingException.html">BadPaddingException e
) {
} catch (1.5.0/docs/api/javax/crypto/IllegalBlockSizeException.html">IllegalBlockSizeException e
) {
} catch (1.5.0/docs/api/java/io/UnsupportedEncodingException.html">UnsupportedEncodingException e
) {
} catch (java.
io.
1.5.0/docs/api/java/io/IOException.html">IOException e
) {
}
return null;
}
public 1.5.0/docs/api/java/lang/String.html">String decrypt
(1.5.0/docs/api/java/lang/String.html">String str
) {
try {
// Decode base64 to get bytes
byte[] dec =
new sun.
misc.
BASE64Decoder().
decodeBuffer(str
);
// Decrypt
byte[] utf8 = dcipher.
doFinal(dec
);
// Decode using utf-8
return new 1.5.0/docs/api/java/lang/String.html">String(utf8,
"UTF8");
} catch (javax.
crypto.
1.5.0/docs/api/javax/crypto/BadPaddingException.html">BadPaddingException e
) {
} catch (1.5.0/docs/api/javax/crypto/IllegalBlockSizeException.html">IllegalBlockSizeException e
) {
} catch (1.5.0/docs/api/java/io/UnsupportedEncodingException.html">UnsupportedEncodingException e
) {
} catch (java.
io.
1.5.0/docs/api/java/io/IOException.html">IOException e
) {
}
return null;
}
public static void main
(1.5.0/docs/api/java/lang/String.html">String[] args
)
{
1.5.0/docs/api/java/lang/String.html">String code = DesUtils.
getInstance().
encrypt("jorge machado");
1.5.0/docs/api/java/lang/System.html">System.
out.
println(code
);
1.5.0/docs/api/java/lang/System.html">System.
out.
println(DesUtils.
getInstance().
decrypt(code
));
}
}