Andriod's DES encryption
1 Main Code
About the des package, you need to export the 64-bit package of Android. If you find com. sun..., a foundclass exception may occur.
String key;
Public DES (String key)
{
This. key = key;
}
Public String encrypt (String str ){
Byte [] enc = null;
Try {
Enc = desEncrypt (str, key );
}
Catch (Exception ex ){
}
// Return new com. e. text. BASE64Encoder (). encode (enc );
// Return new BASE64Encoder (). encode (enc );
Return new String (Base64.encode (enc, Base64.DEFAULT ));
}
Public static byte [] desEncrypt (String message, String key) throws Exception {
Cipher cipher = Cipher. getInstance ("DES/CBC/PKCS5Padding ");
DESKeySpec desKeySpec = new DESKeySpec (key. getBytes ("UTF-8 "));
SecretKeyFactory keyFactory = SecretKeyFactory. getInstance ("DES ");
SecretKey secretKey = keyFactory. generateSecret (desKeySpec );
IvParameterSpec iv = new IvParameterSpec (key. getBytes ("UTF-8 "));
Cipher. init (Cipher. ENCRYPT_MODE, secretKey, iv );
Return cipher. doFinal (message. getBytes ("UTF-8 "));
}
Demo attached
Http://download.csdn.net/detail/wangchunshun/8180345