Android des aes MD5 Encryption

Source: Internet
Author: User

Android des aes MD5 Encryption

AES encryption:

 

Package com. example. encrypdate. util; import java. io. unsupportedEncodingException; import java. security. invalidKeyException; import java. security. noSuchAlgorithmException; import java. security. secureRandom; import javax. crypto. badPaddingException; import javax. crypto. cipher; import javax. crypto. illegalBlockSizeException; import javax. crypto. keyGenerator; import javax. crypto. noSuchPaddingException; import javax. crypto. secretKey; import javax. crypto. spec. secretKeySpec; public class AES {/*** encryption ** @ param content * content to be encrypted * @ param password * encryption password * @ return */public static byte [] encrypt (String content, string password) {try {KeyGenerator kgen = KeyGenerator. getInstance (AES); kgen. init (128, new SecureRandom (password. getBytes (); SecretKey secretKey = kgen. generateKey (); byte [] enCodeFormat = secretKey. getEncoded (); SecretKeySpec key = new SecretKeySpec (enCodeFormat, AES); Cipher cipher = Cipher. getInstance (AES); // create a secret byte [] byteContent = content. getBytes (UTF-8); cipher. init (Cipher. ENCRYPT_MODE, key); // initialize byte [] result = cipher. doFinal (byteContent); return result; // encryption} catch (NoSuchAlgorithmException e) {e. printStackTrace ();} catch (NoSuchPaddingException e) {e. printStackTrace ();} catch (InvalidKeyException e) {e. printStackTrace ();} catch (UnsupportedEncodingException e) {e. printStackTrace ();} catch (IllegalBlockSizeException e) {e. printStackTrace ();} catch (BadPaddingException e) {e. printStackTrace ();} return null ;} /*** decrypt ** @ param content * content to be decrypted * @ param password * decryption key * @ return */public static byte [] decrypt (byte [] content, string password) {try {KeyGenerator kgen = KeyGenerator. getInstance (AES); kgen. init (128, new SecureRandom (password. getBytes (); SecretKey secretKey = kgen. generateKey (); byte [] enCodeFormat = secretKey. getEncoded (); SecretKeySpec key = new SecretKeySpec (enCodeFormat, AES); Cipher cipher = Cipher. getInstance (AES); // create a cipher. init (Cipher. DECRYPT_MODE, key); // initialize byte [] result = cipher. doFinal (content); return result; // encryption} catch (NoSuchAlgorithmException e) {e. printStackTrace ();} catch (NoSuchPaddingException e) {e. printStackTrace ();} catch (InvalidKeyException e) {e. printStackTrace ();} catch (IllegalBlockSizeException e) {e. printStackTrace ();} catch (BadPaddingException e) {e. printStackTrace ();} return null;}/*** convert decimal to hexadecimal ** @ param buf * @ return */public static String parseByte2HexStr (byte buf []) {StringBuffer sb = new StringBuffer (); for (int I = 0; I <buf. length; I ++) {String hex = Integer. toHexString (buf [I] & 0xFF); if (hex. length () = 1) {hex = '0' + hex;} sb. append (hex. toUpperCase ();} return sb. toString ();}/*** convert the hexadecimal format to the decimal format ** @ param hexStr * @ return */public static byte [] parseHexStr2Byte (String hexStr) {if (hexStr. length () <1) return null; byte [] result = new byte [hexStr. length ()/2]; for (int I = 0; I DES encryption:

 

 

Package com. example. encrypdate. util; import javax. crypto. cipher; import javax. crypto. spec. ivParameterSpec; import javax. crypto. spec. secretKeySpec; import android. util. base64; public class DES {// initialization vector, randomly filled with private static byte [] iv = {1, 2, 3, 4, 5, 6, 7, 8 }; /*** @ param encryptString * plaintext to be encrypted * @ param encryptKey * Key * @ return encrypted ciphertext * @ throws Exception */public static String encryptDES (String encryptString, string encryptKey) throws Exception {// instantiate the IvParameterSpec object and use the specified initialization vector IvParameterSpec zeroIv = new IvParameterSpec (iv); // instantiate the SecretKeySpec class, construct SecretKeySecretKeySpec key = new SecretKeySpec (encryptKey. getBytes (), DES); // create a Cipher cipher Cipher = Cipher. getInstance (DES/CBC/PKCS5Padding); // use the key to initialize the Cipher object cipher. init (Cipher. ENCRYPT_MODE, key, zeroIv); // perform the encryption operation byte [] encryptedData = cipher. doFinal (encryptString. getBytes (); return Base64.encodeToString (encryptedData, Base64.DEFAULT );} /***** @ param decrypString * ciphertext * @ param decryptKey * decryption key * @ return * @ throws Exception */public static String decryptDES (String decrypString, String decryptKey) throws Exception {byte [] byteMi = Base64.decode (decrypString, Base64.DEFAULT); // instantiate the IvParameterSpec object and use the specified initialization vector IvParameterSpec zeroIv = new IvParameterSpec (iv ); // instantiate the SecretKeySpec class and construct SecretKeySecretKeySpec key = new SecretKeySpec (decryptKey. getBytes (), DES); // create a Cipher cipher Cipher = Cipher. getInstance (DES/CBC/PKCS5Padding); // use the key to initialize the Cipher object cipher. init (Cipher. DECRYPT_MODE, key, zeroIv); // execute the decryption operation byte [] decryptedData = cipher. doFinal (byteMi); return new String (decryptedData );}}
MD5 encryption:

 

 

Package com. example. encrypdate. util; import java. security. messageDigest; import java. security. noSuchAlgorithmException; public class MD5 {/***** @ param data * @ return */public static String toMD5 (String data) {try {// instantiate a MessageDigest object MessageDigest algorithm = MessageDigest whose Digest algorithm is MD5. getInstance (MD5); // reset the Digest to use algorithm again. reset (); // use bytes to update the abstract algorithm. update (data. getBytes (); // use the specified byte array to update the abstract. Then, the return toHexString (algorithm. digest (),);} catch (NoSuchAlgorithmException e) {// catch Block e automatically generated by TODO. printStackTrace () ;}return ;}// convert each character in the String to a hexadecimal private static String toHexString (byte [] bytes, String separator) {StringBuilder hexstring = new StringBuilder (); for (byte B: bytes) {String hex = Integer. toHexString (0xFF & B); if (hex. length () = 1) {hexstring. append ('0');} hexstring. append (hex);} return hexstring. toString ();}}

Test file:

 

 

Package com. example. encrypdate; import com. example. encrypdate. util. AES; import com. example. encrypdate. util. DES; import android. OS. bundle; import android. app. activity; import android. util. log; import android. widget. textView; import com. example. encrypdate. util. MD5; public class MainActivity extends Activity {private TextView TV; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TV = (TextView) this. findViewById (R. id. TV); // DESTest (); // MD5Test (); AESTest ();} public void DESTest () {// The specified key can only be 8-Bit String key = 12345678; // specify the number of plaintext String text to be encrypted; Log. I (DES, DES encryption plaintext: + text + encryption key: + key); // call DES encryption try {String encryptResult = DES. encryptDES (text, key); String decryptResult = DES. decryptDES (encryptResult, key); TV. setText (encryption result: + encryptResult + decryption result: + decryptResult); Log. I (DES, encryption result: + encryptResult + decryption result: + decryptResult);} catch (Exception e) {// catch Block e automatically generated by TODO. printStackTrace () ;}} private void AESTest () {String content = test3444; String password = 11; // encrypt byte [] encryptResult = AES. encrypt (content, password); String encryptResultStr = AES. parseByte2HexStr (encryptResult); // decrypt byte [] decryptFrom = AES. parseHexStr2Byte (encryptResultStr); String decryptResult = new String (AES. decrypt (decryptFrom, password); Log. I (AES, password: + password + before encryption: + content + after encryption: + encryptResultStr + after decryption: + decryptResult); TV. setText (password: + password + pre-encryption: + content + after encryption: + encryptResultStr + after decryption: + decryptResult);} private void MD5Test () {String str = 1nakjnvjanbdvjk, female guest, Region v pride cannot be wasted. v pride cannot be wasted. v convenience cannot be wasted. vu Bagua v Arabic Style v; string MD5Result = MD5.toMD5 (str); Log. I (MD5, str + MD5 encryption result: + MD5Result); TV. setText (str + MD5 encryption result: + MD5Result );}}

 
 

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.