import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
/* 
* MD5加密 
*/ 
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 {
/**
* 加密
*
* @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);
}
/**
* 解密
*
* @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);
}
/**
* 加密(公钥加密、私钥加密)
*
* @param message 待加密的消息
* @param 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);
// 注意中文的处理
return cipher.doFinal(message.getBytes("gb2312"));
}
/**
* 解密(如果公钥加密,则用私钥解密;如果私钥加密,则用公钥解密)
*
* @param message 待解密的消息
* @param 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));
}
/**
* 通过模长和公钥指数获取公钥
*
* @param modulus 模长
* @param publicExponent 公钥指数
* @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' };
}

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 = "不知道什么时候,开始喜欢这里,每个夜里都会来这里看你。";
Log.d("zhangxy",MD5.getDigest(info));
// 公钥加密
Log.d("zhangxy",RSAUtil.encrypt(info));
// 公钥解密(经私钥加密)
Log.d("zhangxy", RSAUtil.decrypt("94d5ffca913465785714348f10c57c8a0226aca2c8a5294d3a32f398c4791bee8bb37873e16a7b71ed64e40ac121ec4f4bf375b881421a17a3f10789dc543ab41c26c11ba1184b2e0328ef6d354e191f7d978bd9b984e76d310e028b3412093f7296d58d9adb7f9e4b5eb6427c369ae5e919f848c7a21b7794d5985e4d3ad10a"));
}
}

最新文章

  1. mssqlserver数据导出到另外一个数据库
  2. LocalDB在IIS中的运行失败
  3. Unity3D 5.x 交互功能 - 光线投射、碰撞设置
  4. Android Stduio 发生 Process &#39;command &#39;somePath:java.exe&#39;&#39; finished with non-zero exit value 2 异常的解决办法
  5. android中textview设置为多行文本时,如何让文字从最顶开始显示
  6. Git 一些错误的解决方法
  7. 关于npoi不能导入07以上版本的Excel
  8. div.2/C. They Are Everywhere&lt;two pointer&gt;
  9. MySQL所学所思所想
  10. node.js进阶话题
  11. Android的自动完成文本框-android学习之旅(二十六)
  12. 修复ubuntu引导
  13. [LeetCode&amp;Python] Problem 606. Construct String from Binary Tree
  14. 课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第三周(Hyperparameter tuning, Batch Normalization and Programming Frameworks) —— 2.Programming assignments
  15. golang 解码未知键的 json 字符串
  16. tomcat logs 目录下各日志文件的含义
  17. 20155213 2016-2017-2《Java程序设计》第四周学习总结
  18. 【BZOJ 2024】 2024: [SHOI2009] 舞会 (容斥原理+高精度)
  19. matlab从文件夹名中获得该文件夹下所图像文件名
  20. Algorithm4.子数组求和贪心

热门文章

  1. [AlwaysOn Availability Groups]排查:AG超过RTO
  2. 微软CodeDom模型学习笔记(全)
  3. SSH之免密码登录
  4. forward和redirect的区别(转)
  5. MYSQL的常用命令和增删改查语句和数据类型
  6. Oracle索引梳理系列(七)- Oracle唯一索引、普通索引及约束的关系
  7. MongoDB学习笔记~索引提高查询效率
  8. [Java入门笔记] Java语言基础(一):注释、标识符与关键字
  9. DB监控-Riak集群监控
  10. ELF Format 笔记(十四)—— 段内容