Copy codeThe Code is 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 codeThe Code is 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 new String (result );
}
/**
* Encryption (public key encryption and private key encryption)
*
* @ Param message refers to the message to be encrypted.
* @ Param key public key 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 );
// Handle Chinese Characters
Return cipher. doFinal (message. getBytes ("gb2312 "));
}
/**
* Decryption (if the public key is encrypted, the private key is used for decryption; if the private key is encrypted, the public key is used for decryption)
*
* @ Param message refers to the message to be decrypted.
* @ Param key public key 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 ));
}
/**
* Obtain the Public Key through the module length and Public Key Index
*
* @ Param modulus mode length
* @ Param publicExponent Public Key Index
* @ Return
* @ Throws Exception
*/
Public static PublicKey getPublicKey (){
PublicKey publicKey = null;
String modulus = "140865665237544398577638791993321201143991791099370252934699963963887058026979531275917645451893685346013654333931757603593193739776986525943697469996693704995753266331593233395038088698299308180612215713544577462613426793519824197226393059683065343801412208205295045502348474411031999124137863144916358656019 ";
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 codeThe Code is 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 = "I don't know when to like it. I will come to see you every night. ";
Log. d ("zhangxy", MD5.getDigest (info ));
// Public key encryption
Log. d ("zhangxy", RSAUtil. encrypt (info ));
// Public Key decryption (encrypted by private key)
Log. d ("zhangxy", RSAUtil. decrypt ("decrypt "));
}
}