Andriod's DES encryption

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.