import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class CryptUtil3DES {
private static final String CRYPT_KEY =
"v3VC7LfCq6IL5KgIglqZrQ1b";
public static final String KEY_ALGORITHM =
"DESede";
public static final String CIPHER_ALGORITHM =
"DESede/ECB/PKCS5Padding";
public static String decrypt(String value)
{
try {
SecretKeySpec
keySpec = new SecretKeySpec(CRYPT_KEY.getBytes(),
KEY_ALGORITHM);
Cipher cipher
= Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE,
keySpec);
byte[]
decodedByte = Base64.decodeBase64(value.getBytes());
byte[]
decryptedByte = cipher.doFinal(decodedByte);
return new
String(decryptedByte);
} catch (Exception e) {
return
null;
}
}
public static String encrypt(String value)
{
try {
SecretKeySpec
keySpec = new SecretKeySpec(CRYPT_KEY.getBytes(),
KEY_ALGORITHM);
Cipher cipher
= Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE,
keySpec);
byte[]
encryptedByte = cipher.doFinal(value.getBytes());
byte[]
encodedByte = Base64.encodeBase64(encryptedByte);
return new
String(encodedByte);
} catch (Exception e) {
return
null;
}
}
public static void main(String[] arg) {
String dec =
encrypt("24000JAD9");
System.out.println(dec);
dec =
decrypt("2WQxm/1RLz46e8oKp9QR4w==");
System.out.println(dec);
}
}