当前位置: 移动技术网 > IT编程>移动开发>Android > android md5加密与rsa加解密实现代码

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

一条船上有75头牛,威尼斯人高尔夫赌场,兖州天气预报

复制代码 代码如下:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网