Android MD5 encryption and RSA plus decryption implementation code

Source: Internet
Author: User
Tags decrypt md5 encryption modulus

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 ();
}
}

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 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);
Attention to the processing of Chinese
Return cipher.dofinal (Message.getbytes ("gb2312"));
}
/**
* Decryption (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 Decrypt
* @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 (message));
}
/**
* Get public key via modulo length and public key index
*
* @param modulus die 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 '};
}

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 "));
}
}

Android MD5 encryption and RSA plus decryption implementation code

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.