zl程序教程

您现在的位置是:首页 >  其他

当前栏目

androidmd5加密与rsa加解密实现代码

加密代码 实现 rsa 加解密
2023-06-13 09:14:42 时间
复制代码代码如下:

importjava.io.UnsupportedEncodingException;
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
publicclassMD5{
/*
*MD5加密
*/
publicstaticStringgetDigest(Stringstr){
MessageDigestmessageDigest=null;
try{
messageDigest=MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
}catch(NoSuchAlgorithmExceptione){
e.printStackTrace();
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}
byte[]byteArray=messageDigest.digest();
StringBuffermd5StrBuff=newStringBuffer();
for(inti=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]));
}
returnmd5StrBuff.toString().toUpperCase();
}
}

复制代码代码如下:

importjava.math.BigInteger;
importjava.security.Key;
importjava.security.KeyFactory;
importjava.security.PublicKey;
importjava.security.spec.RSAPublicKeySpec;
importjavax.crypto.Cipher;
importorg.bouncycastle.jce.provider.BouncyCastleProvider;
publicclassRSAUtil{
/**
*加密
*
*@parammessage
*@return
*/
publicstaticStringencrypt(Stringmessage){
byte[]result=null;
try{
result=encrypt(message,getPublicKey());
}catch(Exceptione){
e.printStackTrace();
}
returntoHexString(result);
}
/**
*解密
*
*@parammessage
*@return
*/
publicstaticStringdecrypt(Stringmessage){
byte[]result=null;
try{
result=decrypt(message,getPublicKey());
}catch(Exceptione){
e.printStackTrace();
}
returnnewString(result);
}
/**
*加密(公钥加密、私钥加密)
*
*@parammessage待加密的消息
*@paramkey公钥或私钥
*@return
*@throwsException
*/
privatestaticbyte[]encrypt(Stringmessage,Keykey)throwsException{
Ciphercipher=Cipher.getInstance("RSA",newBouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE,key);
//注意中文的处理
returncipher.doFinal(message.getBytes("gb2312"));
}
/**
*解密(如果公钥加密,则用私钥解密;如果私钥加密,则用公钥解密)
*
*@parammessage待解密的消息
*@paramkey公钥或私钥
*@return
*@throwsException
*/
privatestaticbyte[]decrypt(Stringmessage,Keykey)throwsException{
Ciphercipher=Cipher.getInstance("RSA",newBouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE,key);
returncipher.doFinal(toBytes(message));
}
/**
*通过模长和公钥指数获取公钥
*
*@parammodulus模长
*@parampublicExponent公钥指数
*@return
*@throwsException
*/
publicstaticPublicKeygetPublicKey(){
PublicKeypublicKey=null;
Stringmodulus="140865665237544398577638791993321201143991791099370252934699963963887058026979531275917645451893685346013654333931757603593193739776986525943697469996693704995753266331593233395038088698299308180612215713544577462613426793519824197226393059683065343801412208205295045502348474411031999124137863144916358656019";
StringpublicExponent="65537";
BigIntegerm=newBigInteger(modulus);
BigIntegere=newBigInteger(publicExponent);
RSAPublicKeySpeckeySpec=newRSAPublicKeySpec(m,e);
try{
KeyFactorykeyFactory=KeyFactory.getInstance("RSA",newBouncyCastleProvider());
publicKey=keyFactory.generatePublic(keySpec);
}catch(Exceptione1){
e1.printStackTrace();
}
returnpublicKey;
}
privatestaticfinalbyte[]toBytes(Strings){
byte[]bytes;
bytes=newbyte[s.length()/2];
for(inti=0;i<bytes.length;i++){
bytes[i]=(byte)Integer.parseInt(s.substring(2*i,2*i+2),16);
}
returnbytes;
}
publicstaticStringtoHexString(byte[]b){
StringBuildersb=newStringBuilder(b.length*2);
for(inti=0;i<b.length;i++){
sb.append(HEXCHAR[(b[i]&0xf0)>>>4]);
sb.append(HEXCHAR[b[i]&0x0f]);
}
returnsb.toString();
}
privatestaticchar[]HEXCHAR={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
}

复制代码代码如下:
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
publicclassMainActivityextendsActivity{
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Stringinfo="不知道什么时候,开始喜欢这里,每个夜里都会来这里看你。";
Log.d("zhangxy",MD5.getDigest(info));
//公钥加密
Log.d("zhangxy",RSAUtil.encrypt(info));
//公钥解密(经私钥加密)
Log.d("zhangxy",RSAUtil.decrypt("94d5ffca913465785714348f10c57c8a0226aca2c8a5294d3a32f398c4791bee8bb37873e16a7b71ed64e40ac121ec4f4bf375b881421a17a3f10789dc543ab41c26c11ba1184b2e0328ef6d354e191f7d978bd9b984e76d310e028b3412093f7296d58d9adb7f9e4b5eb6427c369ae5e919f848c7a21b7794d5985e4d3ad10a"));
}
}