zoukankan      html  css  js  c++  java
  • 加密

    1.MD5加密

    //采用重载public byte[] ComputeHash(byte[] buffer)
    //其他重载public byte[] ComputeHash(Stream inputStream),
    //其他重载public byte[] ComputeHash(byte[] buffer,int offset,int count)
    public static string GetMD5Hash(string str)
    {
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();//MD5 md5 =
        byte[] bytes = ASCIIEncoding.Default.GetBytes(str);//System.Text.Encoding.Default.GetBytes(str)
        byte[] encoded = md5.ComputeHash(bytes);//计算 bytes字节数组的哈希值
    
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < encoded.Length; i++)
            sb.Append(encoded[i].ToString("x2"));//ToString("x").PadLeft(2, '0'); //对遍历到的字节进行加密
    
        return sb.ToString();
    }
    
    public static string GetMD5Hash2(string str)
    {
        System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
        byte[] bytes = Encoding.Default.GetBytes(str);
        byte[] encoded = md5.ComputeHash(bytes);
    
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < encoded.Length; i++)
            sb.Append(encoded[i].ToString("x2"));
    
        return sb.ToString();
    }

    System.Security.Cryptography.MD5.Create() is actually creating a MD5CryptoServiceProvider.
    MD5 is the base class and it's abstract. I'm guessing they added the public create function for ease of use.

    public sealed class MD5CryptoServiceProvider : MD5
    public abstract class MD5 : HashAlgorithm

    MD5 Represents the abstract class from which all implementations of the MD5 hash algorithm inherit.
    MD5CryptoServiceProvider Computes the MD5 hash value for the input data using the implementation provided by the cryptographic service provider (CSP). This class cannot be inherited.

    2.ROT13

    private string ROT13Encode(string input)
    {
        char tem_char;
        int uniChar;
        StringBuilder encodeText=new StringBuilder();
        for (int i = 0; i < input.Length; i++)
        {
            //https://msdn.microsoft.com/en-us/library/362314fe.aspx
            tem_char = input[i];//System.Convert.ToChar(input.Substring(i,1));
            uniChar = (int) tem_char;
            if (uniChar >= 97 && uniChar <= 109)//97...109 a...m
            {
                uniChar = uniChar + 13;//110...122 n...z
            }
            else if (uniChar >= 110 && uniChar <= 122)
                {
                    uniChar = uniChar - 13;
                }
            else if (uniChar >= 65 && uniChar <= 77)
            {
                uniChar = uniChar + 13;
            }
            else if (uniChar >= 78 && uniChar <= 909)
            {
                uniChar = uniChar - 13;
            }
            encodeText.Append((char) uniChar);
        }
        return encodeText.ToString();
    }
  • 相关阅读:
    洛谷 P4114 Qtree1
    洛谷 P2486 [SDOI2011]染色
    洛谷 P1505 [国家集训队]旅游
    洛谷 P4281 [AHOI2008]紧急集合 / 聚会
    C++中main函数的返回值一定要是int
    局部变量作为函数返回值
    sizeof和strlen在string类中的使用
    strlen 和 sizeof 的区别
    数据结构中的堆栈和内存中的堆栈问题
    数据类型的字长,字节问题
  • 原文地址:https://www.cnblogs.com/dennysong/p/5628213.html
Copyright © 2011-2022 走看看