常用的加密算法---数字摘要

发布时间:2020-07-10 12:04:07 作者:诗情碧霄
来源:网络 阅读:1128

数字摘要:

数字摘要也称为消息摘要,它是一个唯一对应一个消息或文本的固定长度的值,它是一个单向


Hash函数对消息进行计算产生的。



摘要生成的过程:待摘要串-----> Hash函数-----> 摘要


消息摘要的特点:


1.无论输入的消息多长,计算出来的消息摘要的长度是固定的。例如:MD5 的为128个比特位,SHA-1的为


160个比特位;


2.一般只要输入的消息不同,对其产生的摘要消息也是不相同的,相同的输入必然会产生相同的摘要消息;


3.由于消息摘要并不包含原文的完整信息,因此只能进行正向的消息摘要,而无法从摘要中恢复出原来的消息


甚至根本不可能找到任何与原信息相关的消息。


1.MD5:

基于java的MD5算法的使用:

常用的加密算法---数字摘要

/**

 * 实现MD5的加密

 * 

 * @param con

 *   需要加密的字符串

 * @return

 * @throws Exception

 */

 private static byte[] testMD5(String con) throws Exception {

     MessageDigest md5 = MessageDigest.getInstance("MD5");

     byte[] bytes = md5.digest(con.getBytes("utf-8"));

     return bytes;

 }



2.SHA(摘要的信息长度160位  最安全的散列算法之一):

常用的加密算法---数字摘要

 /**

  *SHA 散列安全算法

  * 

  * @param con 

  *             待加密的字符串

  * @return

  * @throws Exception

  */

  private static byte[] tstSHA1(String con) throws Exception {

     MessageDigest sha = MessageDigest.getInstance("SHA-1");

     byte[] bytes = sha.digest(con.getBytes("utf-8"));

     return bytes;

 }



3.十六进制编码:

 /**

  * 16进制加密

  * 

  * @param bytes

  * @return

  */

 private static String bytes2hex(byte[] bytes) {

   StringBuilder hex = new StringBuilder();

   for (int i = 0; i < bytes.length; i++) {

      byte b = bytes[i];

      boolean negtive = false;

      if (b < 0) {

        negtive = true;

      }

      int inte = Math.abs(b);

      if (negtive)

      inte = inte | 0x80;

      String temp = Integer.toHexString(inte & 0xFF);

      if (temp.length() == 1) {

        hex.append("0");

      }

      hex.append(temp.toLowerCase());

      }

      return hex.toString();

   }

  /**

   * 16进制解密

   * 

   * @param hex

   * @return

   */

 private static byte[] hex2bytes(String hex) {

   byte[] bytes = new byte[hex.length() / 2];

   for (int i = 0; i < hex.length(); i = i + 2) {

      String subStr = hex.substring(i, i + 2);

      boolean negative = false;

      int inte = Integer.parseInt(subStr, 16);

      if (inte > 127) {

        negative = true;

      }

      if (inte == 128) {

        inte = -128;

      } else if (negative) {

        inte = 0 - (inte & 0x7F);

      }

      byte b = (byte) inte;

      bytes[i / 2] = b;

      }

      return bytes;

  }

4.Base64编码:

 /**

  * base64 編碼

  * @param base64

  * @return

  * @throws IOException

  */

 private static byte[] base642byte(String base64) throws IOException {

    BASE64Decoder bs = new BASE64Decoder();

    return bs.decodeBuffer(base64); 

  }

 /**

  * base64 解码

  * @param bytes

  * @return

  */

 private static String byte2base64(byte[] bytes) {

    BASE64Encoder bse = new BASE64Encoder();

    return bse.encode(bytes);

 }


  随着数据化时代的到来,信息的安全性越来越成为我们关注的问题,本期博客写了一些基础的加密算法,希望对大家有用。其他的加密算法我也会在后期的书写中逐渐补上。

推荐阅读:
  1. MySQL分区摘要
  2. python 摘要:hmac

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

基础 md5 base

上一篇:html基础课(2)

下一篇:nagios监控redis端口、监控url接口告警脚本

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》