.net core AES加密解密及RSA 签名验签
阅读原文时间:2023年07月08日阅读:1

引用

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

///

/// RSA签名 ///
/// 数据
/// RSA密钥
///
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 验签

///

/// RSA验签 ///
/// 内容
/// RSA公钥
/// 签名字段
///
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

以上 :)