zoukankan      html  css  js  c++  java
  • C# 常用加密方法收集 对称,非对称

    1.用RSACryptoServiceProvider签名验签, 私钥加密,公钥验证

    RSACryptoServiceProvider
     1   //生成公钥私钥对
    2 byte[] messagebytes = Encoding.UTF8.GetBytes("luo罗");
    3 RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider();
    4 string privatekey = oRSA.ToXmlString(true);
    5 string publickey = oRSA.ToXmlString(false);
    6
    7 //私钥签名
    8 RSACryptoServiceProvider oRSA3 = new RSACryptoServiceProvider();
    9 oRSA3.FromXmlString(privatekey);
    10 byte[] AOutput = oRSA3.SignData(messagebytes, "SHA1");
    11 //公钥验证
    12 RSACryptoServiceProvider oRSA4 = new RSACryptoServiceProvider();
    13 oRSA4.FromXmlString(publickey);
    14 bool bVerify = oRSA4.VerifyData(messagebytes, "SHA1", AOutput);

      

    2.RSA 公钥加密, 私钥解密

    RSA非对称加密
     1 using System;
    2 using System.Security.Cryptography;
    3 /// <summary>
    4 /// 生成公钥,私钥对
    5 /// </summary>
    6 public static string[] GenerateKeys()
    7 {
    8 string[] sKeys = new String[2];
    9 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    10 sKeys[0] = rsa.ToXmlString(true);//私钥
    11 sKeys[1] = rsa.ToXmlString(false);//公钥
    12 return sKeys;
    13 }
    14
    15 /// <summary>
    16 /// RSA 加密
    17 /// </summary>
    18 /// <param name="sSource" >明文</param>
    19 /// <param name="sPublicKey" >公钥</param>
    20 public static string EncryptString(string sSource, string sPublicKey)
    21 {
    22 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    23 string plaintext = sSource;
    24 rsa.FromXmlString(sPublicKey);
    25 byte[] cipherbytes;
    26 byte[] byteEn = rsa.Encrypt(Encoding.UTF8.GetBytes("a"), false);
    27 cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(plaintext), false);
    28
    29 StringBuilder sbString = new StringBuilder();
    30 for (int i = 0; i < cipherbytes.Length; i++)
    31 {
    32 sbString.Append(cipherbytes[i] + ",");
    33 }
    34 return sbString.ToString();
    35 }
    36
    37
    38 /// <summary>
    39 /// RSA 解密
    40 /// </summary>
    41 /// <param name="sSource">密文</param>
    42 /// <param name="sPrivateKey">私钥</param>
    43 public static string DecryptString(String sSource, string sPrivateKey)
    44 {
    45 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    46 rsa.FromXmlString(sPrivateKey);
    47 byte[] byteEn = rsa.Encrypt(Encoding.UTF8.GetBytes("a"), false);
    48 string[] sBytes = sSource.Split(',');
    49
    50 for (int j = 0; j < sBytes.Length; j++)
    51 {
    52 if (sBytes[j] != "")
    53 {
    54 byteEn[j] = Byte.Parse(sBytes[j]);
    55 }
    56 }
    57 byte[] plaintbytes = rsa.Decrypt(byteEn, false);
    58 return Encoding.UTF8.GetString(plaintbytes);
    59 }

      

    3.RSA 改进版, 私钥加密, 公钥解密

    代码参考这里, 代码较多就不贴出来了:

    http://www.cnblogs.com/hhh/archive/2011/06/03/2070692.html

    相关链接:
    [1]RSA私钥加密公钥解密算法。

    http://blog.csdn.net/zhilunchen/archive/2008/09/17/2943158.aspx

    [2]BigInteger大整数运算类。 

    http://www.codeproject.com/KB/cs/biginteger.aspx
    4.DES 加密
    DES加密
    /// <summary>
    /// DES算法描述简介:
    /// DES是Data Encryption Standard(数据加密标准)的缩写。它是由IBM公司研制的一种加密算法,
    /// 美国国家标准局于1977年公布把它作为非机要部门使用的数据加密标准;
    /// 它是一个分组加密算法,他以64位为分组对数据加密。
    /// 同时DES也是一个对称算法:加密和解密用的是同一个算法。
    /// 它的密匙长度是56位(因为每个第8 位都用作奇偶校验),
    /// 密匙可以是任意的56位的数,而且可以任意时候改变.
    /// </summary>
    private static byte[] IV = { 0x14, 0xFC, 0x29, 0xC8, 0x9A, 0xCB, 0xCA, 0xE7 };//加密IV向量
    public static String Encrypt(String Key, String str)
    {
    byte[] bKey = Encoding.UTF8.GetBytes(Key.Substring(0, 8));
    byte[] bIV = IV;
    byte[] bStr = Encoding.UTF8.GetBytes(str);
    try
    {
    DESCryptoServiceProvider desc
    = new DESCryptoServiceProvider();
    MemoryStream mStream
    = new MemoryStream();
    CryptoStream cStream
    = new CryptoStream(mStream, desc.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write);
    cStream.Write(bStr,
    0, bStr.Length);
    cStream.FlushFinalBlock();
    return Convert.ToBase64String(mStream.ToArray());
    }
    catch
    {
    return string.Empty;
    }
    }

    public static String Decrypt(String Key, String DecryptStr)
    {
    try
    {
    byte[] bKey = Encoding.UTF8.GetBytes(Key.Substring(0, 8));
    byte[] bIV = IV;
    byte[] bStr = Convert.FromBase64String(DecryptStr);
    DESCryptoServiceProvider desc
    = new DESCryptoServiceProvider();
    MemoryStream mStream
    = new MemoryStream();
    CryptoStream cStream
    = new CryptoStream(mStream, desc.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write);
    cStream.Write(bStr,
    0, bStr.Length);
    cStream.FlushFinalBlock();
    return Encoding.UTF8.GetString(mStream.ToArray());
    }
    catch
    {
    return string.Empty;
    }
    }

      

    5.常用SHA1, MD5

    MD5, SHA1
     1 /// <summary>
    2 /// MD5加密
    3 /// </summary>
    4 private static string GetMd5(string md5)
    5 {
    6 System.Security.Cryptography.MD5CryptoServiceProvider md = new System.Security.Cryptography.MD5CryptoServiceProvider();
    7 byte[] value, hash;
    8 value = System.Text.Encoding.UTF8.GetBytes(md5);
    9 hash = md.ComputeHash(value);
    10 md.Clear();
    11 string temp = "";
    12 for (int i = 0, len = hash.Length; i < len; i++)
    13 {
    14 temp += hash[i].ToString("X").PadLeft(2, '0');
    15 }
    16 return temp;
    17 }
    18
    19 /// <summary>
    20 /// SHA1加密
    21 /// </summary>
    22 private static string Get_SHA1(string strSource)
    23 {
    24 System.Security.Cryptography.SHA1 sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();
    25 byte[] bytResult = sha.ComputeHash(System.Text.Encoding.Default.GetBytes(strSource));
    26 //转换成字符串,32位
    27 string strResult = BitConverter.ToString(bytResult);
    28 //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉
    29 strResult = strResult.Replace("-", "");
    30 return strResult;
    31 }

      

  • 相关阅读:
    python-----贴图 和 报错:OSError: image file is truncated (8 bytes not processed)的处理
    springboot集成RabbitMQ
    MySQL数据库设计规范
    腾讯云COS对象存储
    腾讯云OCR图片文字识别
    java基础之 java注释
    centos7下自动备份mysql数据库
    nginx配置ssl证书
    java基础之 控制语句
    js -- 操作sqlite数据库
  • 原文地址:https://www.cnblogs.com/hanf/p/2137590.html
Copyright © 2011-2022 走看看