zoukankan      html  css  js  c++  java
  • .net core AES加密解密及RSA 签名验签

    引用

    using Org.BouncyCastle.Crypto.Parameters;
    using Org.BouncyCastle.Security;
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    AES加密

       public static string AesEncrypt(string content, string key)
            {
                byte[] keyArray = Convert.FromBase64String(key);
                byte[] toEncryptArray = Encoding.UTF8.GetBytes(content);
    
                SymmetricAlgorithm des = Aes.Create();
                des.Key = keyArray;
                des.Mode = CipherMode.ECB;
                des.Padding = PaddingMode.PKCS7;
    
                ICryptoTransform cTransform = des.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
                return Convert.ToBase64String(resultArray);
            }

    AES解密

     public static string AesDecrypt(string content, string key)
            {
                byte[] keyArray = Convert.FromBase64String(key);
                byte[] toEncryptArray = Convert.FromBase64String(content);
    
                SymmetricAlgorithm des = Aes.Create();
                des.Key = keyArray;
                des.Mode = CipherMode.ECB;
                des.Padding = PaddingMode.PKCS7;
    
                ICryptoTransform cTransform = des.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
                return Convert.ToBase64String(resultArray);
            }

    RSA签名 需要引用nuget包 BouncyCastle.NetCore

     /// <summary>
            /// RSA签名
            /// </summary>
            /// <param name="content">数据</param>
            /// <param name="privateKey">RSA密钥</param>
            /// <returns></returns>
            public static string RasSign(string content, string privateKey)
            {
                var signer = SignerUtilities.GetSigner("SHA1withRSA");
            //将java格式的rsa密钥转换成.net格式
    var privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey)); signer.Init(true, privateKeyParam); var plainBytes = Encoding.UTF8.GetBytes(content); signer.BlockUpdate(plainBytes, 0, plainBytes.Length); var signBytes = signer.GenerateSignature(); return Convert.ToBase64String(signBytes); }

    RSA 验签

     /// <summary>
            /// RSA验签
            /// </summary>
            /// <param name="content">内容</param>
            /// <param name="publicKey">RSA公钥</param>
            /// <param name="signData">签名字段</param>
            /// <returns></returns>
            public static bool VerifySign(string content, string publicKey, string signData)
            {
                var signer = SignerUtilities.GetSigner("SHA1withRSA");
                var publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
                signer.Init(false, publicKeyParam);
                var signBytes = Convert.FromBase64String(signData);
                var plainBytes = Encoding.UTF8.GetBytes(content);
                signer.BlockUpdate(plainBytes, 0, plainBytes.Length);
                var ret = signer.VerifySignature(signBytes);
                return ret;
            }

    ------------------------------------------------------分割线----------------------------------------------------------

    此代码是在framework4.6.2下实现的

     public static string RasSign(string content, string privateKey)
            {
                var pkey = RSAPrivateKeyJava2DotNet(privateKey);
                string halg = "SHA1";//SHA1 MD5 SHA256  
                var  signData = RSAHelper.SignData(pkey, content, halg);//SHA1  
                return signData;
            }
            /// <summary>      
            /// RSA私钥格式转换,java->.net      
            /// </summary>      
            /// <param name="privateKey">java生成的RSA私钥</param>      
            /// <returns></returns>     
            private static string RSAPrivateKeyJava2DotNet(this string privateKey)
            {
                RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
                return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
                Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
                Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
                Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
                Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
                Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
                Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
                Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
                Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));
            }

    github:https://github.com/aspros-luo/Qwerty.Encrypt.Demo

    以上 :)

  • 相关阅读:
    小程序携带参数转发
    小程序开发过程中问题终结
    在小程序中使用md5处理需要加密的字符串(含中文的字符串)
    php 字符串的处理
    PHP 数组(array)
    php数学运算
    php 单双引号的区别
    PHP 结构控制 if else / switch / while / do while
    JavaScript best practices JS最佳实践
    java入门概念梳理总结
  • 原文地址:https://www.cnblogs.com/AsprosL/p/7234957.html
Copyright © 2011-2022 走看看