android md5加密与rsa加解密实现代码

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

时间: 2024-08-24 18:09:30

android md5加密与rsa加解密实现代码的相关文章

Android 与 SUN JAVA RSA 加解密不同之处

默认情况下无法通用因为默认的填充方式不同. 做以下处理能够到达一致: android: Cipher cipher = Cipher.getInstance("RSA/None/NoPadding"); sun java: Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider()); 其中 org.bouncycastle.jc

Delphi RSA加解密【 (RSA公钥加密,私钥解密)、(RSA私钥加密,公钥解密)、MD5加密、SHA加密】

作者QQ:(648437169) delphi RSA加解密 [Delphi RSA加解密]支持 (RSA公钥加密,私钥解密).(RSA私钥加密,公钥解密).MD5加密.SHA1加密.SHA224加密.SHA256加密.SHA384加密.SHA512加密 原文地址:https://www.cnblogs.com/zhimamaigua/p/11003504.html

java 使用pem密钥进行RSA加解密

1.使用openssl生成私钥和公钥 openssl下载地址:http://www.openssl.org/source openssl生成私钥命令:  genrsa -out rsa_private_key.pem 1024 openssl生成公钥命令:  rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem 2.此时在openssl安装目录下的bin文件夹可以看到 rsa_private_key.pem 和 rsa_publi

【转】 Java 进行 RSA 加解密时不得不考虑到的那些事儿

[转] Java 进行 RSA 加解密时不得不考虑到的那些事儿 1. 加密的系统不要具备解密的功能,否则 RSA 可能不太合适 公钥加密,私钥解密.加密的系统和解密的系统分开部署,加密的系统不应该同时具备解密的功能,这样即使黑客攻破了加密系统,他拿到的也只是一堆无法破解的密文数据.否则的话,你就要考虑你的场景是否有必要用 RSA 了. 2. 可以通过修改生成密钥的长度来调整密文长度 生成密文的长度等于密钥长度.密钥长度越大,生成密文的长度也就越大,加密的速度也就越慢,而密文也就越难被破解掉.著名

java rsa加解密算法的实现

RSAUtils:RSA加解密的实现 package com.rsa.test; import java.io.ByteArrayOutputStream; import java.nio.charset.Charset; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import jav

PHP RSA加解密示例(转)

1.生成密钥和公钥 开始前需要准备openssl环境 linux 需要安装openssl工具包,传送门http://www.openssl.org/source/ window 下需要安装openssl的程序,传送门http://slproweb.com/products/Win32OpenSSL.html 如果不想安装,可以用本例提供的密钥和公钥进行测试. 密钥生成 openssl genrsa 用于生成rsa私钥文件,生成是可以指定私钥长度和密码保护,具体参数请参考文档. openssl g

java RSA加解密以及用途

在公司当前版本的中间件通信框架中,为了防止非授权第三方和到期客户端的连接,我们通过AES和RSA两种方式的加解密策略进行认证.对于非对称RSA加解密,因为其性能耗费较大,一般仅用于认证连接,不会用于每次报文本身的加解密(这一般使用AES/DES加密),对于较为安全的支付通道,则一般是约定定期交换加解密密钥,交换过程本身的报文则是通过RSA进行加解密的.这样就在单纯的对称加密的基础上提供了更好的保障,只要签名复杂,定期的更新足以使得破坏的成本高昂到超过破解的成本. 一般来说,公钥会发布给客户端,客

【go语言】RSA加解密

关于go语言的RSA加解密的介绍,这里有一篇文章,已经介绍的很完整了. 对应的go语言的加解密代码,参考git. 因为原文跨语言是跟php,我这里要跟c语言进行交互,所以,这里贴上c语言的例子. 参考原文:http://hayageek.com/rsa-encryption-decryption-openssl-c/ #include <openssl/pem.h> #include <openssl/ssl.h> #include <openssl/rsa.h> #i

[转]PHP,Android,IOS通信之AES128加解密

转自:http://s00s10.blog.163.com/blog/static/43988552201411913011459/ android上使用: mcrypt = new MCrypt(); /* 加密*/ String encrypted = MCrypt.bytesToHex( mcrypt.encrypt("需加密的字符") ); /* 解密*/ String decrypted = new String( mcrypt.decrypt( encrypted ) );