登录
原创

多语言MD5加密代码实现

发布于 2020-09-30 阅读 10428
  • Java
  • Python
  • PHP
  • Go
  • C#
原创

PHP

/**
 * PHP MD5 加密方法
 */
$string = '123456789'; //需要加密的字符串
 
$md5a = md5($string); //32位MD5
echo $md5a;
 
$md5b = substr(md5($string),8,16);  // 16位MD5加密
echo $md5b;

Java

import java.security.MessageDigest;
 
public class MD5Util {
    /**
     * 把字节数组转成16进位制数
     *
     * @param bytes
     * @return
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuffer md5str = new StringBuffer();
        //把数组每一字节换成16进制连成md5字符串
        int digital;
        for (int i = 0; i < bytes.length; i++) {
            digital = bytes[i];
            if (digital < 0) {
                digital += 256;
            }
            if (digital < 16) {
                md5str.append("0");
            }
            md5str.append(Integer.toHexString(digital));
        }
        return md5str.toString();
    }
  
    /**
     * 把字节数组转换成md5
     *
     * @param input
     * @return
     */
    public static String bytesToMD5(byte[] input) {
        String md5str = null;
        try {
            //创建一个提供信息摘要算法的对象,初始化为md5算法对象
            MessageDigest md = MessageDigest.getInstance("MD5");
            //计算后获得字节数组
            byte[] buff = md.digest(input);
            //把数组每一字节换成16进制连成md5字符串
            md5str = bytesToHex(buff);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md5str;
    }
  
    /**
     * 将字符串进行md5加密
     *
     * @param str 需要进行加密的字符串
     * @return  加密后的字符串
     */
    public static String strToMD5(String str) {
        byte[] input = str.getBytes();
        return bytesToMD5(input);
    }
      
    public static void main(String[] args) {
     System.out.println(strToMD5("feng"));
    }
}

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
  
  
namespace MD5_Project
{
    public class md5object
    {
        //static void Main(string[] args)
        //{
        //    md5object md5obj = new md5object();
        //    //16位md5加密(或32位)
        //    Console.WriteLine(md5obj.md5("123456", 16));
        //}
  
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string MD5(string text)
        {
            string str = "";
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] textBytes = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(text));
            foreach (int i in textBytes)
            {
                str += i.ToString("x2");
            }
            return str;
        }
        public string md5(string str, int code)
        {
            if (code == 16) //16位MD5加密(取32位加密的9~25字符) 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
            }
  
            if (code == 32) //32位加密 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
            }
  
            return "00000000000000000000000000000000";
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MD5_Project;
using System.Diagnostics;
  
namespace Project_md5
{
    class Program
    {
        static void Main(string[] args)
        {
            md5object md5obj = new md5object();
            //16位md5加密(或32位)
            Debug.WriteLine(md5obj.md5("123456", 16));
            Console.WriteLine(md5obj.md5("123456", 16));
            Console.ReadLine();
        }
    }
}

Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
import hashlib
import md5
 
def main():
    #需要加密的字符串
    str = '1234679'
 
    #方法1(推荐):
    m2 = hashlib.md5()
    m2.update(str)
    sign2 = m2.hexdigest()
    print sign2
 
    #方法2:
    m = md5.new()
    m.update(str)
    sign = m.hexdigest()
    print sign
 
if __name__ == '__main__':
    main()

Go

package main
import (
    "crypto/md5"
    "fmt"
    "encoding/hex"
)
 
func main(){
    md5Ctx := md5.New()
    md5Ctx.Write([]byte("1234679"))
    cipherStr := md5Ctx.Sum(nil)
//  fmt.Print(cipherStr)
//  fmt.Print("\n")
    fmt.Print(hex.EncodeToString(cipherStr))
}

评论区

DDwyane
22粉丝

我饿了,要睡觉了~

6

4

5

举报