Copy Code code as follows:
Import java.io.UnsupportedEncodingException;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
public class MD5 {
/*
* MD5 Encryption
*/
public static string Getdigest (String str) {
MessageDigest messagedigest = null;
try {
MessageDigest = Messagedigest.getinstance ("MD5");
Messagedigest.reset ();
Messagedigest.update (Str.getbytes ("UTF-8"));
catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
byte[] ByteArray = Messagedigest.digest ();
StringBuffer Md5strbuff = new StringBuffer ();
for (int i = 0; i < bytearray.length; i++) {
if (integer.tohexstring (0xFF & Bytearray[i]). Length () = 1)
Md5strbuff.append ("0"). Append (integer.tohexstring (0xFF & Bytearray[i));
Else
Md5strbuff.append (integer.tohexstring (0xFF & Bytearray[i]));
}
Return md5strbuff.tostring (). toUpperCase ();
}
}
Copy Code code as follows:
Import Java.math.BigInteger;
Import Java.security.Key;
Import Java.security.KeyFactory;
Import Java.security.PublicKey;
Import Java.security.spec.RSAPublicKeySpec;
Import Javax.crypto.Cipher;
Import Org.bouncycastle.jce.provider.BouncyCastleProvider;
public class Rsautil {
/**
* Encryption
*
* @param message
* @return
*/
public static string encrypt (String message) {
byte[] result = NULL;
try {
result = Encrypt (message, Getpublickey ());
catch (Exception e) {
E.printstacktrace ();
}
return tohexstring (Result);
}
/**
* Decryption
*
* @param message
* @return
*/
public static string decrypt (String message) {
byte[] result = NULL;
try {
result = Decrypt (message, Getpublickey ());
catch (Exception e) {
E.printstacktrace ();
}
Return a new String (result);
}
/**
* Encryption (public key encryption, private key encryption)
*
* @param message to be encrypted
* @param key public or private key
* @return
* @throws Exception
*/
private static byte[] Encrypt (String message, key key) throws Exception {
Cipher Cipher = cipher.getinstance ("RSA", New Bouncycastleprovider ());
Cipher.init (Cipher.encrypt_mode, key);
Pay attention to the processing of Chinese
Return cipher.dofinal (Message.getbytes ("gb2312"));
}
/**
* Decrypt (if the public key is encrypted, the private key is decrypted; If the private key is encrypted, it is decrypted with the public key)
*
* @param message to be decrypted
* @param key public or private key
* @return
* @throws Exception
*/
private static byte[] Decrypt (String message, key key) throws Exception {
Cipher Cipher = cipher.getinstance ("RSA", New Bouncycastleprovider ());
Cipher.init (Cipher.decrypt_mode, key);
Return cipher.dofinal (tobytes);
}
/**
* Get public key through modulus length and public key index
*
* @param modulus Mould length
* @param publicexponent Public key index
* @return
* @throws Exception
*/
public static PublicKey Getpublickey () {
PublicKey publickey = null;
String modulus = " 1408656652375443985776387919933212011439917910993702529346999639638870580269795312759176454518936853460136543339317576035 9319373977698652594369746999669370499575326633159323339503808869829930818061221571354457746261342679351982419722639305968 3065343801412208205295045502348474411031999124137863144916358656019 ";
String publicexponent = "65537";
BigInteger m = new BigInteger (modulus);
BigInteger e = new BigInteger (publicexponent);
Rsapublickeyspec Keyspec = new Rsapublickeyspec (M, e);
try {
Keyfactory keyfactory = keyfactory.getinstance ("RSA", New Bouncycastleprovider ());
PublicKey = Keyfactory.generatepublic (Keyspec);
catch (Exception E1) {
E1.printstacktrace ();
}
return publickey;
}
private static final byte[] Tobytes (String s) {
byte[] bytes;
bytes = new Byte[s.length ()/2];
for (int i = 0; i < bytes.length; i++) {
Bytes[i] = (byte) integer.parseint (s.substring (2 * I, 2 * i + 2), 16);
}
return bytes;
}
public static String tohexstring (byte[] b) {
StringBuilder sb = new StringBuilder (B.length * 2);
for (int i = 0; i < b.length; i++) {
Sb.append (hexchar[(B[i] & 0xf0) >>> 4]);
Sb.append (Hexchar[b[i] & 0x0f]);
}
return sb.tostring ();
}
private static char[] Hexchar = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '};
}
Copy Code code as follows:
import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
public class Mainactivity extends activity {
@Override
public void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
String info = "Do not know when, start to like here, every night will come here to see you." ";
Log.d ("Zhangxy", Md5.getdigest (info));
//Public Key Cryptography
LOG.D ("Zhangxy", Rsautil.encrypt (info));
//Public key decryption (encrypted by private key)
Log.d ("Zhangxy", Rsautil.decrypt (" 94d5ffca913465785714348f10c57c8a0226aca2c8a5294d3a32f398c4791bee8bb37873e16a7b71ed64e40ac121ec4f4bf375b881421a17a3f10789d c543ab41c26c11ba1184b2e0328ef6d354e191f7d978bd9b984e76d310e028b3412093f7296d58d9adb7f9e4b5eb6427c369ae5e919f848c7a21b7794 D5985E4D3AD10A "));
}
}