欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#多种加解密示例(md5加密解密)

程序员文章站 2024-02-24 08:41:28
复制代码 代码如下:using system;using system.collections.generic;using system.globalization;usi...

复制代码 代码如下:

using system;
using system.collections.generic;
using system.globalization;
using system.io;
using system.security.cryptography;
using system.text;

/// <summary>
/// 加解密
/// </summary>
public static class crypthelper
{
    #region rsa加密

    /// <summary>
    /// 用指定公钥加密文本
    /// </summary>
    /// <param name="publickey">公钥</param>
    /// <param name="input">文本</param>
    /// <returns></returns>
    public static string rsaencrypt(string publickey, string input)
    {
        const string sformat =
            "<rsakeyvalue><modulus>{0}</modulus><exponent>aqab</exponent></rsakeyvalue>";
        var provider = new rsacryptoserviceprovider();
        var arrpubkey = hex2bytes(publickey);
        var pkb64 = convert.tobase64string(arrpubkey);
        var xmlstr = string.format(sformat, pkb64);
        var bytesinput = encoding.utf8.getbytes(input);
        provider.fromxmlstring(xmlstr);
        var encryptarray = provider.encrypt(bytesinput, false);
        var hexstr = bitconverter.tostring(encryptarray).replace("-", "");
        return hexstr.tolower();
    }

    #endregion

    #region 公用函数

    /// <summary>
    /// 将字节转换成16进制数字
    /// </summary>
    /// <param name="bytes">字节数组</param>
    /// <returns></returns>
    public static string bytes2hex(byte[] bytes)
    {
        var buffer = new stringbuilder();
        foreach (var t in bytes)
        {
            buffer.appendformat("{0:x2}", t);
        }
        return buffer.tostring();
    }

    /// <summary>
    /// 将16进制数组转换成字节
    /// </summary>
    /// <param name="input">16进制数组</param>
    /// <returns></returns>
    /// <exception cref="argumentoutofrangeexception"></exception>
    public static byte[] hex2bytes(string input)
    {
        if (string.isnullorempty(input)) return null;
        var offset = input.length%2;
        if (offset == 1) input = "0" + input;
        int i;
        var list = new list<byte>();
        for (i = 0; i < input.length; i += 2)
        {
            var temp = input.substring(i, 2);
            byte bv;
            var success = byte.tryparse(temp, numberstyles.hexnumber, null, out bv);
            if (!success) throw new argumentoutofrangeexception();
            list.add(bv);
        }
        return list.toarray();
    }

    #endregion

    #region sha1加密

    /// <summary>
    /// sha1加密 使用缺省密钥给字符串加密
    /// </summary>
    /// <param name="sourcestring"></param>
    /// <returns></returns>
    public static string sha1encrypt(string sourcestring)
    {
        var data = encoding.default.getbytes(sourcestring);
        hashalgorithm sha = new sha1cryptoserviceprovider();
        var bytes = sha.computehash(data);
        return bitconverter.tostring(bytes).replace("-", "");
    }

    #endregion

    #region des加解密

    /// <summary>
    /// des加密
    /// </summary>
    /// <param name="pubkey"></param>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string desencrypt(string pubkey, string input)
    {
        var des = new descryptoserviceprovider();
        var bytes = encoding.utf8.getbytes(input);
        des.key = encoding.ascii.getbytes(pubkey);
        des.iv = encoding.ascii.getbytes(pubkey);
        using (var ms = new memorystream())
        {
            using (var cryptostream = new cryptostream(ms, des.createencryptor(), cryptostreammode.write))
            {
                cryptostream.write(bytes, 0, bytes.length);
                cryptostream.flushfinalblock();
            }
            var data = ms.toarray();
            return bitconverter.tostring(data).replace("-", "");
        }
    }

    /// <summary>
    /// des解密
    /// </summary>
    /// <param name="pubkey"></param>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string desdecrypt(string pubkey, string input)
    {
        var des = new descryptoserviceprovider();
        var bytes = hex2bytes(input);
        des.key = encoding.ascii.getbytes(pubkey);
        des.iv = encoding.ascii.getbytes(pubkey);
        using (var ms = new memorystream())
        {
            using (var cryptostream = new cryptostream(ms, des.createdecryptor(), cryptostreammode.write))
            {
                cryptostream.write(bytes, 0, bytes.length);
                cryptostream.flushfinalblock();
            }
            var data = ms.toarray();
            return encoding.utf8.getstring(data);
        }
    }

    #endregion

    #region md5加密

    /// <summary>
    /// md5加密
    /// </summary>
    /// <param name="input">输入</param>
    /// <returns></returns>
    public static string md5encrypt(string input)
    {
        md5 md5 = new md5cryptoserviceprovider();
        var data = encoding.utf8.getbytes(input);
        var encs = md5.computehash(data);
        return bitconverter.tostring(encs).replace("-", "");
    }

    #endregion
}