Encrypted transmission in Android

Source: Internet
Author: User
Tags array to string

Find two encryption algorithms, one MD5 encryption, which is applicable to password encryption, because this algorithm is irreversible; the other is RSA encryption, Which is reversible:

1. MD5 Encryption

 

 

Import Java. security. messagedigest; public class MD5 {private final static string [] hexdigits = {"0", "1", "2", "3", "4", "5 ", "6", "7", "8", "9", "A", "B", "C", "D", "E ", "F "}; /*** convert the byte array to a hexadecimal string ** @ Param B * byte array * @ return hexadecimal string */public static string bytearraytohexstring (byte [] B) {stringbuffer resultsb = new stringbuffer (); For (INT I = 0; I <B. length; I ++) {resultsb. append (bytetohexstring (B [I]);} return resultsb. tostring ();} Private Static string bytetohexstring (byte B) {int n = B; If (n <0) n = 256 + N; int d1 = N/16; int D2 = n % 16; return hexdigits [D1] + hexdigits [D2];} public static string md5encode (string origin) {string resultstring = NULL; try {resultstring = new string (origin); messagedigest MD = messagedigest. getinstance ("MD5"); resultstring = bytearraytohexstring (MD. digest (resultstring. getbytes ();} catch (exception ex) {} return resultstring ;}}

 

Ii. RSA Encryption

 

import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import javax.crypto.Cipher; /** * RSAEncrypt * * @author chen * @see */ public class Rsaencrype {    /**      * Main method for RSAEncrypt.      * @param args      */     public static void main(String[] args) {         try {             Rsaencrype encrypt = new Rsaencrype();                          String encryptText = "encryptText";             KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");             keyPairGen.initialize(1024);             KeyPair keyPair = keyPairGen.generateKeyPair();             // Generate keys             RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();             RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();                          byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes());             byte[] de = encrypt.decrypt(privateKey,e);             System.out.println(encrypt.bytesToString(e));             System.out.println(encrypt.bytesToString(de));         } catch (Exception e) {             e.printStackTrace();         }     }         /**      * Change byte array to String.      * @return byte[]      */     protected String bytesToString(byte[] encrytpByte) {         String result = "";         for (Byte bytes : encrytpByte) {             result += (char) bytes.intValue();         }         return result;     }         /**      * Encrypt String.      * @return byte[]      */     protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj)  {         if (publicKey != null) {             try {                 Cipher cipher = Cipher.getInstance("RSA");                 cipher.init(Cipher.ENCRYPT_MODE, publicKey);                 return cipher.doFinal(obj);             } catch (Exception e) {                 e.printStackTrace();             }         }         return null;     }     /**      * Basic decrypt method      * @return byte[]      */     protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {         if (privateKey != null) {                 try {                     Cipher cipher = Cipher.getInstance("RSA");                     cipher.init(Cipher.DECRYPT_MODE, privateKey);                     return cipher.doFinal(obj);                 } catch (Exception e) {                     e.printStackTrace();                 }             }             return null;     } }

 

 

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.