13种加密与解密算法【四】(加密算法和解密算法都需要保密)

网友投稿 475 2022-10-08


13种加密与解密算法【四】(加密算法和解密算法都需要保密)

【10、散列哈希之SHA1加密】

SHA1(英语:Secure Hash Algorithm 1,中文名:安全散列算法1)是一种密码散列函数,美国国家安全局设计,并由美国国家标准技术研究所(NIST)发布为联邦数据处理标准(FIPS)。SHA-1可以生成一个被称为消息摘要的160位(20字节)散列值,散列值通常的呈现形式为40个十六进制数。**【SHA-1和SHA-0】SHA-1和SHA-0的算法只在压缩函数的消息转换部分差了一个比特的循环位移。

【散列之SHA1】

/** * SHA1 * @param inStr 需要摘要的内容 * @return */ public static String sha1Encode(String inStr) { MessageDigest sha = null; try { sha = MessageDigest.getInstance("SHA"); byte[] byteArray = inStr.getBytes("UTF-8"); byte[] md5Bytes = sha.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); return ""; } }

【散列之SHA1小 demo】System.out.println("散列之SHA1摘要:"+sha1Encode(str));

【PS:SHA1同MD5一样,是不可逆的,可以用作文章摘要,作为判别标识】

【11、散列之CRC32加密】

【散列之CRC32 小demo】【PS:散列加密过程不可逆,拿一段文字作为加密内容没有意义,这里使用文件作为内容】

【散列之 CRC32 加密】

/** * 使用CheckedInputStream计算CRC */ public static Long getCRC32(String filepath) { try { CRC32 crc32 = new CRC32(); FileInputStream fileinputstream = new FileInputStream(new File(filepath)); CheckedInputStream checkedinputstream = new CheckedInputStream(fileinputstream, crc32); while (checkedinputstream.read() != -1) { } checkedinputstream.close(); return crc32.getValue(); }catch (Exception e){ e.printStackTrace(); return null; } } /** * 采用BufferedInputStream的方式加载文件 */ public static long bufferedInputStream(String filepath) { try { InputStream inputStream = new BufferedInputStream(new FileInputStream(filepath)); CRC32 crc = new CRC32(); byte[] bytes = new byte[1024]; int cnt; while ((cnt = inputStream.read(bytes)) != -1) { crc.update(bytes, 0, cnt); } inputStream.close(); return crc.getValue(); }catch (Exception e){ e.printStackTrace(); return 0; } }

【测试小 demo】

String path = "D:\\huatu.eapx"; Long ll = getCRC32(path); System.out.println("使用CheckedInputStream计算CRC得到:" + ll); Long ll1 = bufferedInputStream(path); System.out.println("采用BufferedInputStream计算CRC得到:"+ll1); ![](Bytes,即16 TB,若消息超过该长度,则需要更换密钥对剩下的消息进行处理。它是目前安全性较高,加/解密速度比较高效的流密码之一,在各种处理器平台上都有不凡的表现。【测试小 demo】略1

【13、Escape】

在很多脚本语言的应用当中,escape函数是一个可转换编码的函数。比如javascript 中的传递参数?deptName=测试,可先将"测试"用escape重新编码,再进行传递,在服务器端接收后再解码才不会出现乱码。escape一般用于传递URL参数和类似urlencode base64_encode函数是类似的。详情参见《Web项目中的常用编码》


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:idea 在springboot中使用lombok插件的方法
下一篇:13种加密与解密算法之RC4篇【三】(rc5算法加密)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~