java加密算法分享(rsa解密、对称加密、md5加密)
import java.io.unsupportedencodingexception;
import java.security.invalidkeyexception;
import java.security.messagedigest;
import java.security.nosuchalgorithmexception;
import java.security.privatekey;
import java.security.publickey;
import java.security.securerandom;
import javax.crypto.badpaddingexception;
import javax.crypto.cipher;
import javax.crypto.illegalblocksizeexception;
import javax.crypto.keygenerator;
import javax.crypto.nosuchpaddingexception;
import javax.crypto.secretkey;
import com.sun.mail.util.base64decoderstream;
import com.sun.mail.util.base64encoderstream;
public class util {
/**
* 传入名文和公钥钥对数据进行rsa解密
* <br>返回值:string
* <br>@param src
* <br>@param pubkey
* <br>@return
*/
public static string rsaencoding(string src,publickey pubkey){
try {
cipher cip = cipher.getinstance("rsa");
cip.init(cip.encrypt_mode, pubkey);
byte[] by = cip.dofinal(src.getbytes());
return new string(base64encoderstream.encode(by));
} catch (nosuchalgorithmexception e) {
throw new runtimeexception(e);
} catch (nosuchpaddingexception e) {
throw new runtimeexception(e);
} catch (invalidkeyexception e) {
throw new runtimeexception(e);
} catch (illegalblocksizeexception e) {
throw new runtimeexception(e);
} catch (badpaddingexception e) {
throw new runtimeexception(e);
}
}
/**
* 传入rsa密文和私钥对数据进行解密
* <br>返回值:string
* <br>@param sec
* <br>@param privkey
* <br>@return
*/
public static string rsadeencoding(string sec,privatekey privkey){
try {
cipher cip = cipher.getinstance("rsa");
cip.init(cip.decrypt_mode, privkey);
byte[] by = base64decoderstream.decode(sec.getbytes());
return new string(cip.dofinal(by));
} catch (nosuchalgorithmexception e) {
throw new runtimeexception(e);
} catch (nosuchpaddingexception e) {
throw new runtimeexception(e);
} catch (invalidkeyexception e) {
throw new runtimeexception(e);
} catch (illegalblocksizeexception e) {
throw new runtimeexception(e);
} catch (badpaddingexception e) {
throw new runtimeexception(e);
}
}
/**
* 传入字符串、密钥,并加密字符串(对称加密加密),支持:des、aes、desede(3des)
* <br>返回值:string 密文
* <br>@param src
* <br>@param key
* <br>@param method(des、aes、desede)
* <br>@return
*/
//对称加密加密
public static string doubkeyencoding(string src,string keysrc,string method) {
secretkey key;
try {
//生成密钥
keygenerator kg = keygenerator.getinstance(method);
//初始化此密钥生成器。
kg.init(new securerandom(keysrc.getbytes("utf-8")));
key = kg.generatekey();
//加密
cipher ciph = cipher.getinstance(method);
ciph.init(cipher.encrypt_mode, key);
ciph.update(src.getbytes("utf-8"));
//使用64进行编码,一避免出现丢数据情景
byte[] by = base64encoderstream.encode(ciph.dofinal());
return new string(by);
} catch (nosuchalgorithmexception e) {
throw new runtimeexception(e);
} catch (nosuchpaddingexception e) {
throw new runtimeexception(e);
} catch (invalidkeyexception e) {
throw new runtimeexception(e);
} catch (illegalblocksizeexception e) {
throw new runtimeexception(e);
} catch (badpaddingexception e) {
throw new runtimeexception(e);
} catch (unsupportedencodingexception e) {
throw new runtimeexception(e);
}
}
/**
* 传入字符串、密钥、加密方式,并解密字符串(对称加密解密密),支持:des、aes、desede(3des)
* <br>生成时间:2014年5月2日 下午1:12:13
* <br>返回值:string 密钥原文
* <br>@param sec
* <br>@param key
* <br>@param method(des、aes、desede)
* <br>@return
*/
public static string doubkeydencoding(string sec,string keysrc,string method) {
secretkey key;
try {
//生成密钥
keygenerator kg = keygenerator.getinstance(method);
//初始化此密钥生成器。
kg.init(new securerandom(keysrc.getbytes("utf-8")));
key = kg.generatekey();
//加密
cipher ciph = cipher.getinstance(method);
ciph.init(ciph.decrypt_mode, key);
//使用64进行解码,一避免出现丢数据情景
byte[] by = base64decoderstream.decode(sec.getbytes());
ciph.update(by);
return new string(ciph.dofinal());
} catch (nosuchalgorithmexception e) {
throw new runtimeexception(e);
} catch (nosuchpaddingexception e) {
throw new runtimeexception(e);
} catch (invalidkeyexception e) {
throw new runtimeexception(e);
} catch (illegalblocksizeexception e) {
throw new runtimeexception(e);
} catch (badpaddingexception e) {
throw new runtimeexception(e);
} catch (unsupportedencodingexception e) {
throw new runtimeexception(e);
}
}
/**
* 单向信息加密(信息摘要),支持:md5、md2、sha(sha-1,sha1)、sha-256、sha-384、sha-512,
* <br>返回值:string 加密后的密文
* <br>@param src 传入加密字符串(明文)
* <br>@param method 指定算法(md5、md2、sha(sha-1,sha1)、sha-256、sha-384、sha-512)
* <br>@return
*/
public static string ecodingpasswd(string src,string method){
try {
//信息摘要算法
messagedigest md5 = messagedigest.getinstance(method);
md5.update(src.getbytes());
byte[] encoding = md5.digest();
//使用64进行编码,一避免出现丢数据情景
return new string(base64encoderstream.encode(encoding));
} catch (nosuchalgorithmexception e) {
throw new runtimeexception(e+"加密失败!!");
}
}
}
上一篇: java模仿windows计算器示例