加密与解密、加签与验签

参考博客:
--SHA-1算法、SHA-2算法
[url]https://www.sohu.com/a/192953382_604699[/url]
--SSH 密钥类型的的选择(RSA, DSA or Other)
[url]http://blog.sina.com.cn/s/blog_6f31085901015agu.html[/url]
--DSA和RSA的区别
[url]http://blog.sina.com.cn/s/blog_60cf05130101ew6r.html[/url]
--RSA、DSA和ECDSA三者的签名
[url]https://blog.csdn.net/sszgg2006/article/details/25478269[/url]

----一张图了解RSA加解密与加验签
[url]http://blog.csdn.net/zhshulin/article/details/71573542[/url]
图:
[img]http://dl2.iteye.com/upload/attachment/0126/7457/03da7612-1147-30a1-97b4-3199706bf05f.png[/img]

----Java RSA 加密 解密 签名 验签
[url]http://gaofulai1988.iteye.com/blog/2262802[/url]

----使用RSA、MD5对参数生成签名与验签
[url]http://blog.csdn.net/mr_smile2014/article/details/52130029[/url]

--java加签与解签
[url]https://blog.csdn.net/john2522/article/details/53365358[/url]

--MD5加密

public class CommonMd5Utils {
private static final Logger logger = LoggerFactory.getLogger(CommonMd5Utils.class);
private static final String HEXADECIMAL_NUMBER_ARRAY[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

/**
* MD5加密方法
* @param sourceStr 待加密串
* @param charsetname 字符编码如"UTF-8"
* @param lengthType 32位或者16位
* @return
*/
public static String encrypt(String sourceStr, String charsetname, String lengthType) {
if(null==sourceStr){
throw new BusinessException("fail", "待加密字段不能为NULL");
}
if(!(CommonConstants.STRING_16.equals(lengthType)||CommonConstants.STRING_32.equals(lengthType))){
throw new BusinessException("fail", "加密设置的生成长度不合法");
}
String resultString = "";
try {
resultString = new String(sourceStr);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname)){
resultString = byteArrayToHexStringMethod(md.digest(resultString.getBytes()));
}else{
resultString = byteArrayToHexStringMethod(md.digest(resultString.getBytes(charsetname)));
}
} catch (Exception e) {
logger.info("MD5加密异常");
e.printStackTrace();
}
logger.info("resultString="+resultString);
if(StringUtils.isNotBlank(resultString)){
if("16".equals(lengthType)){
resultString=resultString.substring(8, 24);
}
return resultString;
}
return "";
}

/**
* 字节数组转十六进制字符串
* @param b
* @return
*/
private static String byteArrayToHexStringMethod(byte byteArray[]) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < byteArray.length; i++){
stringBuffer.append(byteToHexStringMethod(byteArray[i]));
}
return stringBuffer.toString();
}

/**
* 字节转十六进制字符串
* @param b
* @return
*/
private static String byteToHexStringMethod(byte byteVar) {
int n = byteVar;
if (n < 0){
n += 256;
}
int d1 = n / 16;
int d2 = n % 16;
return HEXADECIMAL_NUMBER_ARRAY[d1] + HEXADECIMAL_NUMBER_ARRAY[d2];
}
}

--Java 编程下字符串的 16 位、32位 MD5 加密
https://www.cnblogs.com/sunzn/p/3455135.html


为什么MD5不可以解密?
[url]https://zhidao.baidu.com/question/579202465.html[/url]

MD5加密原理是散列算法,散列算法也称哈希算法。
计算机专业学的数据结构就有哈希表这一知识点。
比如10除以3余数为一,4除以3余数也为一,但余数为一的就不知道这个数是哪个了。
所以md5不能解密。
就算是设计这个加密算法的人都不知道。
但是你的密码是怎么验证的呢? 就是因为同一密码加密后一定相同。
你输入密码加密后才能知道你的密码是否正确。
也就是说,你的密码只有你自己知道。
也是为什么扣扣密码只能重置,不能找回的原因。
over
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值