# 加密解密工具类使用说明
# MD5使用
md5并不是加密使用,只是对文件进行信息摘要提取,md5工具类提供了两个静态方法进行使用,详情如下:
相关说明:
MD5方法,对输入的内容进行md5摘要提取
实例demo:
String pass = MD5Util.hash("123"); System.out.println("md5="+pass);
verify方法,进行MD5验证,第一个参数是需要验证的内容,第二个是md5的字符串
实例demo:
boolean success = MD5Util.verify("123",pass); System.out.println("verify="+success);
# RSA使用
rsa是非对称加密,一般用作简单信息的非对称,例如网络通信协商使用哪种对称加密等等,由于其复杂度很高,所以产生也需要消耗部分时间,提供的方法如下:
相关说明:
loadKeyFromFile
,从当前类路径加载文件实例demo:
//从类路径加载文件 String pubKeyStr = TsRSAUtil.loadKeyFromFile("publicKey.pem",true);
getKeyPair
获取密钥对,并调用getPublicKey
和getPrivateKey
获取base64编码后的公钥和私钥字符串,可以调用文件的main
方法进行保存(默认公钥文件保存在/tmp/rsa.pub.pem
,私钥保存在/tmp/rsa.pri.pem
)或者新建相应的文件进行存储实例demo:
KeyPair keyPair = TARSAUtil.getKeyPair(); String publicKeyStr = TsRSAUtil.getPublicKey(keyPair); String privateKeyStr = TsRSAUtil.getPrivateKey(keyPair); System.out.println("RSA公钥Base64编码:" + publicKeyStr); System.out.println("RSA私钥Base64编码:" + privateKeyStr);
string2PublicKey
和string2PrivateKey
,将ba64编码的公钥字符串和私钥字符串转成对应的PublicKey
对象和PrivateKey
对象,实例demo:
//将Base64编码后的公钥转换成PublicKey对象 PublicKey publicKey = TsRSAUtil.string2PublicKey(publicKeyStr); //将Base64编码后的私钥转换成PrivateKey对象 PrivateKey privateKey = TsRSAUtil.string2PrivateKey(privateKeyStr);
公钥加密与私钥解密,使用
encrypt
方法进行公钥加密和decrypt
进行私钥解密,进行相应的操作即可实例demo:
//用公钥加密 String publicEncrypt = TARSAUtil.encrypt(message, publicKey); //用私钥解密 String privateDecrypt = TARSAUtil.decrypt(publicEncrypt, privateKey);