C#加密与java 互通

文章目录

  • 前言
  • 对方接口签名要求
  • 我方对接思路
    • 1.RSA 加密
    • 2.AES256加密
  • 完整的加密帮助类


前言

提示:这里可以添加本文要记录的大概内容:

在我们对接其他公司接口的时候,时常会出现对方使用的开发语言和我方使用的开发语言不同的情况,因为每个语言之间都会有些许差别,在进行加密签名中就会出现签名一直对接不上的问题,下面我就来和各位分享一下我所遇到的问题以及解决方案。


对方接口签名要求

  1. 报文发送方自行产生随机密钥,然后再使用接收方公钥对随机秘钥进行RSA 非对称加密,生成 encrypted。
  2. 按照报文规范生成报文原文,然后对报文原文(JSON 格式字符串)使用 对称密钥字节数组进行 AES256 算法加密,再进行 Base64 编码,生成msg
  3. 再对报文原文进行签名,得到签名结果(十六进制字符串),签名算法采用“SHA1withRSA”,生成 signature

可以看到上面这一堆文字,看似字很少,实则却使用到了很多种加密方式
可以梳理出会用到的加密有:RSA加密,AES256加密,Base64加密,SHA1加密

我方对接思路

1.RSA 加密

由于对方时使用的java,java密钥的key格式和我们C#这边RSA加密使用的key格式不一样,需要进行格式处理也就是下面的“RSAPrivateKeyJava2DotNet”方法。

  public class RSAUtils{/// <summary>/// 生成私钥/// </summary>/// <returns></returns>public static string CreatePrivateKey(){string str = Guid.NewGuid().ToString("N");Byte[] bytes = Encoding.GetEncoding("utf-8").GetBytes(str);byte[] aesKey = new byte[16];for (int i = 0; i < 16; i++){aesKey[i] = bytes[i];}return Convert.ToBase64String(aesKey);}/// <summary>/// 生成密钥/// <param name="privateKey">私钥</param>/// <param name="publicKey">公钥</param>/// <param name="keySize">密钥长度:512,1024,2048,4096,8192</param>/// </summary>public static void Generator(out string privateKey, out string publicKey, int keySize = 1024){RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize);privateKey = rsa.ToXmlString(true); //将RSA算法的私钥导出到字符串PrivateKey中 参数为true表示导出私钥 true 表示同时包含 RSA 公钥和私钥;false 表示仅包含公钥。publicKey = rsa.ToXmlString(false); //将RSA算法的公钥导出到字符串PublicKey中 参数为false表示不导出私钥 true 表示同时包含 RSA 公钥和私钥;false 表示仅包含公钥。}/// <summary>/// RSA加密 将公钥导入到RSA对象中,准备加密/// </summary>/// <param name="publicKey">公钥</param>/// <param name="encryptstring">待加密的字符串</param>public static string RsaEncrypt(string publicKey, string encryptstring){using (var rsaProvider = new RSACryptoServiceProvider()){string key = RSAPublicKeyJava2DotNet(publicKey);var inputBytes = Encoding.UTF8.GetBytes(encryptstring);//有含义的字符串转化为字节流rsaProvider.FromXmlString(key);//载入公钥int bufferSize = (rsaProvider.KeySize / 8) - 11;//单块最大长度var buffer = new byte[bufferSize];using (MemoryStream inputStream = new MemoryStream(inputBytes), outputStream = new MemoryStream()){while (true){ //分段加密int readSize = inputStream.Read(buffer, 0, bufferSize);if (readSize <= 0){break;}var temp = new byte[readSize];Array.Copy(buffer, 0, temp, 0, readSize);var encryptedBytes = rsaProvider.Encrypt(temp, false);outputStream.Write(encryptedBytes, 0, encryptedBytes.Length);}return Convert.ToBase64String(outputStream.ToArray());//转化为字节流方便传输}}}/// <summary>/// Java转.net格式/// </summary>/// <param name="JavaPublicKey">Java格式公钥</param>/// <returns></returns>public static string RSAPublicKeyJava2DotNet(string JavaPublicKey){RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(JavaPublicKey));return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));}/// <summary>/// .NET格式转Java格式/// </summary>/// <param name="cPublicKey">c#的.net格式公钥</param>/// <returns></returns>public static string RSAPublicKeyDotNet2Java(string cPublicKey){XmlDocument doc = new XmlDocument(); doc.LoadXml(cPublicKey);BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));RsaKeyParameters pub = new RsaKeyParameters(false, m, p);SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();return Convert.ToBase64String(serializedPublicBytes);}/// <summary>/// .java格式密钥转c#使用的.net格式密钥/// </summary>/// <param name="JavaPrivateKey">.java密钥</param>/// <returns></returns>public static string RSAPrivateKeyJava2DotNet(string JavaPrivateKey){RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(JavaPrivateKey));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()));}/// <summary>/// c#使用的.net格式密钥转换成.Java格式密钥/// </summary>/// <param name="cPrivateKey">.net格式密钥</param>/// <returns></returns>public static string RSAPrivateKeyDotNet2Java(string cPrivateKey){XmlDocument doc = new XmlDocument();doc.LoadXml(cPrivateKey);BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText));RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv);PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam);byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();return Convert.ToBase64String(serializedPrivateBytes);}/// <summary>  /// RSA解密 载入私钥,解密数据  /// </summary>  /// <param name="privateKey">私钥</param>  /// <param name="decryptstring">待解密的字符串</param>  public static string RsaDecrypt(string privateKey, string decryptstring){using (var rsaProvider = new RSACryptoServiceProvider()){string key = RSAPrivateKeyJava2DotNet(privateKey);rsaProvider.FromXmlString(key); //载入私钥  var encryptedBytes = Convert.FromBase64String(decryptstring); //将传入的字符串转化为字节流  //var outputStream = new MemoryStream(encryptedBytes);var bufferSize = rsaProvider.KeySize / 8;var buffer = new byte[bufferSize];using (MemoryStream inputStream = new MemoryStream(encryptedBytes), outputStream = new MemoryStream()){while (true){int readSize = inputStream.Read(buffer, 0, bufferSize);if (readSize <= 0){break;}var temp = new byte[readSize];Array.Copy(buffer, 0, temp, 0, readSize);var decryptedBytes = rsaProvider.Decrypt(temp, false);outputStream.Write(decryptedBytes, 0, decryptedBytes.Length);}return Encoding.UTF8.GetString(outputStream.ToArray()); //转化为字符串  }}}/// <summary>/// RSA私钥加密/// </summary>/// <param name="privateKey">私钥</param>/// <param name="encryptstring">待加密的字符串</param>public static string RsaPrivateEncrypt(string privateKey, string encryptstring){var rsaProvider = new RSACryptoServiceProvider();rsaProvider.FromXmlString(privateKey);//载入私钥var inputBytes = Convert.FromBase64String(encryptstring);//有含义的字符串转化为字节流int bufferSize = (rsaProvider.KeySize / 8) - 11;//单块最大长度var buffer = new byte[bufferSize];using (MemoryStream inputStream = new MemoryStream(inputBytes), outputStream = new MemoryStream()){while (true){//分段加密int readSize = inputStream.Read(buffer, 0, bufferSize);if (readSize <= 0){break;}var temp = new byte[readSize];Array.Copy(buffer, 0, temp, 0, readSize);var encryptedBytes = RsaPrivateEncrypt(privateKey, temp);outputStream.Write(encryptedBytes, 0, encryptedBytes.Length);}return Convert.ToBase64String(outputStream.ToArray());//转化为字节流方便传输}}/// <summary>  /// RSA公钥解密/// </summary>  /// <param name="publicKey">公钥</param>  /// <param name="decryptstring">待解密的字符串</param>  public static string RsaPublicDecrypt(string publicKey, string decryptstring){var rsaProvider = new RSACryptoServiceProvider();rsaProvider.FromXmlString(publicKey); //载入私钥  var encryptedBytes = Convert.FromBase64String(decryptstring); //将传入的字符串转化为字节流  var bufferSize = rsaProvider.KeySize / 8;var buffer = new byte[bufferSize];using (MemoryStream inputStream = new MemoryStream(encryptedBytes), outputStream = new MemoryStream()){while (true){int readSize = inputStream.Read(buffer, 0, bufferSize);if (readSize <= 0){break;}var temp = new byte[readSize];Array.Copy(buffer, 0, temp, 0, readSize);var decryptedBytes = decryptByPublicKey(publicKey, temp);outputStream.Write(decryptedBytes, 0, decryptedBytes.Length);}return Convert.ToBase64String(outputStream.ToArray());}}/// <summary>/// SHA1 加密,返回大写字符串/// </summary>/// <param name="content">需要加密字符串</param>/// <returns>返回40位UTF8 大写</returns>public static string SHA1(string content){return SHA1(content, Encoding.UTF8);}/// <summary>/// SHA1 加密,返回大写字符串/// </summary>/// <param name="content">需要加密字符串</param>/// <param name="encode">指定加密编码</param>/// <returns>返回40位大写字符串</returns>private static string SHA1(string content, Encoding encode){try{SHA1 sha1 = new SHA1CryptoServiceProvider();byte[] bytes_in = encode.GetBytes(content);byte[] bytes_out = sha1.ComputeHash(bytes_in);sha1.Dispose();string result = BitConverter.ToString(bytes_out);result = result.Replace("-", "");return result;}catch (Exception ex){throw new Exception("SHA1加密出错:" + ex.Message);}}/// <summary>/// 私钥加密/// 这个方法只能加密 私钥长度/8 -11 个字符,分段加密的代码要自己处理了。/// </summary>/// <param name="privateKey">密钥</param>/// <param name="data">要加密的数据</param>/// <returns></returns>public static byte[] RsaPrivateEncrypt(string privateKey, byte[] data){string xmlPrivateKey = privateKey;//加载私钥  RSACryptoServiceProvider privateRsa = new RSACryptoServiceProvider();privateRsa.FromXmlString(xmlPrivateKey);//转换密钥  AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetKeyPair(privateRsa);//IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");// 参数与Java中加密解密的参数一致       IBufferedCipher c = CipherUtilities.GetCipher("RSA");c.Init(true, keyPair.Private); //第一个参数为true表示加密,为false表示解密;第二个参数表示密钥 byte[] DataToEncrypt = data;byte[] outBytes = c.DoFinal(DataToEncrypt);//加密  return outBytes;}/// <summary>/// 用公钥解密/// 这个方法只能加密 私钥长度/8 -11 个字符,分段加密的代码要自己处理了。/// </summary>/// <param name="data"></param>/// <param name="key"></param>/// <returns></returns>public static byte[] decryptByPublicKey(string publicKey, byte[] data){string xmlPublicKey = publicKey;RSACryptoServiceProvider publicRsa = new RSACryptoServiceProvider();publicRsa.FromXmlString(xmlPublicKey);AsymmetricKeyParameter keyPair = DotNetUtilities.GetRsaPublicKey(publicRsa);//转换密钥  // AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetRsaKeyPair(publicRsa);//IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");// 参数与Java中加密解密的参数一致       IBufferedCipher c = CipherUtilities.GetCipher("RSA");c.Init(false, keyPair); //第一个参数为true表示加密,为false表示解密;第二个参数表示密钥 byte[] DataToEncrypt = data;byte[] outBytes = c.DoFinal(DataToEncrypt);//解密  return outBytes;}}

调用方法

  string pkey= RSAUtils.CreatePrivateKey();encrypt.data.encrypted = RSAUtils.RsaEncrypt(publicKey, pkey);

其中publicKey 是对方公司(JAVA)给我们的公钥,
这里我们生成的KEY是:Q2THHWOD1H4L81DO
加密出来的值是:rZdVr8ksB4EyJ0L6GGjcgHR+XYKwRnxxs+94Hyd/Y52SgqaROFva3DLACjYrzyAHetmGrMpPLKf4TRq0V8F5eDrjZRhOQelA5ogSWRoLwzpN4KZirBc1HCHyrfaEvHDtoJeNabZzKFTDHvNZ94NRQfpHXqABlS7TzOXJKLK1Z/BiQjGuyOGL3xohwhyZ+vgjqNFjpTYU7gqobno5kBK6zoG8B2wilsDP+4hkF7IAn6dAlw8scBMpuULZF6ceBCEakkgWrBYn4E5DiC5c1tz2x3yViBNO+2XV0YdZIrBVkWKZGCWBYVkq53ovyPavuMMyK16HF7CnKlE3RdMc2WvT6g==

这一步我们加密后对方公司的java是能解密出正确明文的。

2.AES256加密

这里问题就来了

下面是错误方法:

        /// <summary>/// AES加密/// </summary>/// <param name="encryptStr">明文</param>/// <param name="key">密钥</param>/// <returns></returns>public static string Encrypt(string encryptStr,string key){byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);RijndaelManaged rDel = new RijndaelManaged();rDel.Key = keyArray;rDel.Mode = CipherMode.ECB;rDel.Padding = PaddingMode.PKCS7;ICryptoTransform cTransform = rDel.CreateEncryptor();byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);return Convert.ToBase64String(resultArray, 0, resultArray.Length);}/// <summary>/// AES解密/// </summary>/// <param name="decryptStr">密文</param>/// <param name="key">密钥</param>/// <returns></returns>public static string Decrypt(string decryptStr,string key){byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);byte[] toEncryptArray = Convert.FromBase64String(decryptStr);RijndaelManaged rDel = new RijndaelManaged();rDel.Key = keyArray;rDel.Mode = CipherMode.ECB;rDel.Padding = PaddingMode.PKCS7;ICryptoTransform cTransform = rDel.CreateDecryptor();byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);return UTF8Encoding.UTF8.GetString(resultArray);}

这种方式加密在网上的在线解密工具都解密不了,只有我们C#写的解密方法能解密,估计是因为填充类型或者其他什么地方有问题。在这里我也困惑了很久

下面是正确方法:

public class AESUtils
{/// <summary>/// AES256加密/// </summary>/// <param name="content">明文</param>/// <param name="passphase">密钥</param>/// <returns></returns>public static string Encrypt(string content, string passphase){byte[] bytes = Encoding.UTF8.GetBytes(content);byte[] key, iv, salt = new byte[8];using (var rng = new RNGCryptoServiceProvider()){rng.GetBytes(salt); }using (var md5 = new MD5CryptoServiceProvider()){var preHash = Encoding.UTF8.GetBytes(passphase).Concat(salt).ToArray();var bs1 = md5.ComputeHash(preHash);var bs2 = md5.ComputeHash(bs1.Concat(preHash).ToArray());var bs3 = md5.ComputeHash(bs2.Concat(preHash).ToArray());key = bs1.Concat(bs2).ToArray();iv = bs3;}using (var aes = new AesCryptoServiceProvider() { Key = key, IV = iv })using (var encryptor = aes.CreateEncryptor()){var encryptedBytes = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);var concated = Encoding.UTF8.GetBytes("Salted__").Concat(salt).Concat(encryptedBytes);return Convert.ToBase64String(concated.ToArray());}}/// <summary>/// AES256解密/// </summary>/// <param name="content">密文</param>/// <param name="passphase">密钥</param>/// <returns></returns>public static string Decrypt(string content, string passphase){// openssl aes-256-cbc -k 123456789 -md md5 -e -base64//byte[] bytes = Convert.FromBase64String(content);byte[] key, iv, salt = new byte[8], encryptedBytes = new byte[bytes.Length - 8 - 8];//提取 saltArray.ConstrainedCopy(sourceArray: bytes,sourceIndex: 8, //剔除开头的 "Salted__"destinationArray: salt,destinationIndex: 0,length: salt.Length);//提取 encryptedBytesArray.ConstrainedCopy(sourceArray: bytes,sourceIndex: 8 + 8, //并剔除开头的 saltdestinationArray: encryptedBytes,destinationIndex: 0,length: encryptedBytes.Length);using (var md5 = new MD5CryptoServiceProvider()){var preHash = Encoding.UTF8.GetBytes(passphase).Concat(salt).ToArray();var bs1 = md5.ComputeHash(preHash);var bs2 = md5.ComputeHash(bs1.Concat(preHash).ToArray());var bs3 = md5.ComputeHash(bs2.Concat(preHash).ToArray());key = bs1.Concat(bs2).ToArray();iv = bs3;}using (var aes = new AesCryptoServiceProvider() { Key = key, IV = iv })using (var decryptor = aes.CreateDecryptor()){byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);return Encoding.UTF8.GetString(decryptedBytes);}}/// <summary>/// 加密 (新的思路)/// </summary>/// <param name="content">明文</param>/// <param name="passphase">密码(固定32位)</param>/// <returns>密文(base64)</returns>static string CryptoJsEncrypt_New(string content, string passphase ){byte[] bytes = Encoding.UTF8.GetBytes(content);byte[] key = Encoding.UTF8.GetBytes(passphase); //32位的keybyte[] iv = new byte[16];using (var rng = new RNGCryptoServiceProvider())rng.GetBytes(iv); // 产生16个字节的随机ivusing (var aes = new AesCryptoServiceProvider() { Key = key, IV = iv })using (var encryptor = aes.CreateEncryptor()){var encryptedBytes = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);var concated = iv.Concat(encryptedBytes);return "aes256:" + Convert.ToBase64String(concated.ToArray());}}/// <summary>/// 解密 (新的思路)/// </summary>/// <param name="content">密文(base64)</param>/// <param name="passphase">密码(固定32位)</param>/// <returns>明文</returns>static string CryptoJsDecrypt_New(string content, string passphase ){byte[] bytes = Convert.FromBase64String(content.Substring(7)); //剔除开头的 "aes256:"byte[] key = Encoding.UTF8.GetBytes(passphase); //32位的keybyte[] iv = new byte[16];byte[] encryptedBytes = new byte[bytes.Length - 16];//提取 ivArray.ConstrainedCopy(sourceArray: bytes,sourceIndex: 0,destinationArray: iv,destinationIndex: 0,length: iv.Length);//提取 加密结果Array.ConstrainedCopy(sourceArray: bytes,sourceIndex: 16, //并剔除开头的 ivdestinationArray: encryptedBytes,destinationIndex: 0,length: encryptedBytes.Length);using (var aes = new AesCryptoServiceProvider() { Key = key, IV = iv })using (var decryptor = aes.CreateDecryptor()){byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);return Encoding.UTF8.GetString(decryptedBytes);}}
}

调用方法

AESUtils.Encrypt(datastr, pkey);

加密前明文字符串:
{"storeId":"1788431406828474370","merchantId":"1788027169854296065","tradeNo":"123456","phone":"13366237305","tradeAmount":"5.0","notifyUrl":"http://wx.wjy99.com/onlinemtorder/yunxin/orderMsg","callbackUrl":"http://wx.wjy99.com/onlinemtorder/yunxin/orderMsg","remark":"测试请求"}
加密后密文:
U2FsdGVkX1+FSSnYrXRRcvfDoSeLLPcaA2LEN0/ix7Z41jXIYRdCdIm3DMob+xwvVdMnFwv4iq7LhFxAZcetRHbS3hxhbjObhNvkQrvd79+AnZdEMoM/BGTx66U/c8xtdMheqxrRWoXoIIpfRf++UQCGZBuU5upwMjTmuU3vJKQX/2r5qUHkjH05qkR+ioLb6bvi1FKI6vp3Bs4sKPEKtq+5M26aBdOfo5RmmrSJ023GkvC4DGxlyVn7ac97bBj3X/8h2yfpT1E6zW6FwZIBK+aVPlPFVeKVeXBZwdX5tgpgB+4e6duHPAjArQ1XYRfx2ugNn38IDxwtap7ocBi3GnPB8U4iTXPCz+/7uFutDzIQi0hRVEMpVRqaaaw2Ri2fFV84QYgotJC+qXkKRwyx9g==

这种加密方式,网上的解密都能解开了。


完整的加密帮助类

  /// <summary>/// RSA/// </summary>public class RSAUtils{/// <summary>/// 生成私钥/// </summary>/// <returns></returns>public static string CreatePrivateKey(){string str = Guid.NewGuid().ToString("N");Byte[] bytes = Encoding.GetEncoding("utf-8").GetBytes(str);byte[] aesKey = new byte[16];for (int i = 0; i < 16; i++){aesKey[i] = bytes[i];}return Convert.ToBase64String(aesKey);}/// <summary>/// 生成密钥/// <param name="privateKey">私钥</param>/// <param name="publicKey">公钥</param>/// <param name="keySize">密钥长度:512,1024,2048,4096,8192</param>/// </summary>public static void Generator(out string privateKey, out string publicKey, int keySize = 1024){RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize);privateKey = rsa.ToXmlString(true); //将RSA算法的私钥导出到字符串PrivateKey中 参数为true表示导出私钥 true 表示同时包含 RSA 公钥和私钥;false 表示仅包含公钥。publicKey = rsa.ToXmlString(false); //将RSA算法的公钥导出到字符串PublicKey中 参数为false表示不导出私钥 true 表示同时包含 RSA 公钥和私钥;false 表示仅包含公钥。}/// <summary>/// RSA加密 将公钥导入到RSA对象中,准备加密/// </summary>/// <param name="publicKey">公钥</param>/// <param name="encryptstring">待加密的字符串</param>public static string RsaEncrypt(string publicKey, string encryptstring){using (var rsaProvider = new RSACryptoServiceProvider()){string key = RSAPublicKeyJava2DotNet(publicKey);var inputBytes = Encoding.UTF8.GetBytes(encryptstring);//有含义的字符串转化为字节流rsaProvider.FromXmlString(key);//载入公钥int bufferSize = (rsaProvider.KeySize / 8) - 11;//单块最大长度var buffer = new byte[bufferSize];using (MemoryStream inputStream = new MemoryStream(inputBytes), outputStream = new MemoryStream()){while (true){ //分段加密int readSize = inputStream.Read(buffer, 0, bufferSize);if (readSize <= 0){break;}var temp = new byte[readSize];Array.Copy(buffer, 0, temp, 0, readSize);var encryptedBytes = rsaProvider.Encrypt(temp, false);outputStream.Write(encryptedBytes, 0, encryptedBytes.Length);}return Convert.ToBase64String(outputStream.ToArray());//转化为字节流方便传输}}}/// <summary>/// Java转.net格式/// </summary>/// <param name="JavaPublicKey">Java格式公钥</param>/// <returns></returns>public static string RSAPublicKeyJava2DotNet(string JavaPublicKey){RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(JavaPublicKey));return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));}/// <summary>/// .NET格式转Java格式/// </summary>/// <param name="cPublicKey">c#的.net格式公钥</param>/// <returns></returns>public static string RSAPublicKeyDotNet2Java(string cPublicKey){XmlDocument doc = new XmlDocument(); doc.LoadXml(cPublicKey);BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));RsaKeyParameters pub = new RsaKeyParameters(false, m, p);SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();return Convert.ToBase64String(serializedPublicBytes);}/// <summary>/// .java格式密钥转c#使用的.net格式密钥/// </summary>/// <param name="JavaPrivateKey">.java密钥</param>/// <returns></returns>public static string RSAPrivateKeyJava2DotNet(string JavaPrivateKey){RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(JavaPrivateKey));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()));}/// <summary>/// c#使用的.net格式密钥转换成.Java格式密钥/// </summary>/// <param name="cPrivateKey">.net格式密钥</param>/// <returns></returns>public static string RSAPrivateKeyDotNet2Java(string cPrivateKey){XmlDocument doc = new XmlDocument();doc.LoadXml(cPrivateKey);BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText));RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv);PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam);byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();return Convert.ToBase64String(serializedPrivateBytes);}/// <summary>  /// RSA解密 载入私钥,解密数据  /// </summary>  /// <param name="privateKey">私钥</param>  /// <param name="decryptstring">待解密的字符串</param>  public static string RsaDecrypt(string privateKey, string decryptstring){using (var rsaProvider = new RSACryptoServiceProvider()){string key = RSAPrivateKeyJava2DotNet(privateKey);rsaProvider.FromXmlString(key); //载入私钥  var encryptedBytes = Convert.FromBase64String(decryptstring); //将传入的字符串转化为字节流  //var outputStream = new MemoryStream(encryptedBytes);var bufferSize = rsaProvider.KeySize / 8;var buffer = new byte[bufferSize];using (MemoryStream inputStream = new MemoryStream(encryptedBytes), outputStream = new MemoryStream()){while (true){int readSize = inputStream.Read(buffer, 0, bufferSize);if (readSize <= 0){break;}var temp = new byte[readSize];Array.Copy(buffer, 0, temp, 0, readSize);var decryptedBytes = rsaProvider.Decrypt(temp, false);outputStream.Write(decryptedBytes, 0, decryptedBytes.Length);}return Encoding.UTF8.GetString(outputStream.ToArray()); //转化为字符串  }}}/// <summary>/// RSA私钥加密/// </summary>/// <param name="privateKey">私钥</param>/// <param name="encryptstring">待加密的字符串</param>public static string RsaPrivateEncrypt(string privateKey, string encryptstring){var rsaProvider = new RSACryptoServiceProvider();rsaProvider.FromXmlString(privateKey);//载入私钥var inputBytes = Convert.FromBase64String(encryptstring);//有含义的字符串转化为字节流int bufferSize = (rsaProvider.KeySize / 8) - 11;//单块最大长度var buffer = new byte[bufferSize];using (MemoryStream inputStream = new MemoryStream(inputBytes), outputStream = new MemoryStream()){while (true){//分段加密int readSize = inputStream.Read(buffer, 0, bufferSize);if (readSize <= 0){break;}var temp = new byte[readSize];Array.Copy(buffer, 0, temp, 0, readSize);var encryptedBytes = RsaPrivateEncrypt(privateKey, temp);outputStream.Write(encryptedBytes, 0, encryptedBytes.Length);}return Convert.ToBase64String(outputStream.ToArray());//转化为字节流方便传输}}/// <summary>  /// RSA公钥解密/// </summary>  /// <param name="publicKey">公钥</param>  /// <param name="decryptstring">待解密的字符串</param>  public static string RsaPublicDecrypt(string publicKey, string decryptstring){var rsaProvider = new RSACryptoServiceProvider();rsaProvider.FromXmlString(publicKey); //载入私钥  var encryptedBytes = Convert.FromBase64String(decryptstring); //将传入的字符串转化为字节流  var bufferSize = rsaProvider.KeySize / 8;var buffer = new byte[bufferSize];using (MemoryStream inputStream = new MemoryStream(encryptedBytes), outputStream = new MemoryStream()){while (true){int readSize = inputStream.Read(buffer, 0, bufferSize);if (readSize <= 0){break;}var temp = new byte[readSize];Array.Copy(buffer, 0, temp, 0, readSize);var decryptedBytes = decryptByPublicKey(publicKey, temp);outputStream.Write(decryptedBytes, 0, decryptedBytes.Length);}return Convert.ToBase64String(outputStream.ToArray());}}/// <summary>/// SHA1 加密,返回大写字符串/// </summary>/// <param name="content">需要加密字符串</param>/// <returns>返回40位UTF8 大写</returns>public static string SHA1(string content){return SHA1(content, Encoding.UTF8);}/// <summary>/// SHA1 加密,返回大写字符串/// </summary>/// <param name="content">需要加密字符串</param>/// <param name="encode">指定加密编码</param>/// <returns>返回40位大写字符串</returns>private static string SHA1(string content, Encoding encode){try{SHA1 sha1 = new SHA1CryptoServiceProvider();byte[] bytes_in = encode.GetBytes(content);byte[] bytes_out = sha1.ComputeHash(bytes_in);sha1.Dispose();string result = BitConverter.ToString(bytes_out);result = result.Replace("-", "");return result;}catch (Exception ex){throw new Exception("SHA1加密出错:" + ex.Message);}}/// <summary>/// 私钥加密/// 这个方法只能加密 私钥长度/8 -11 个字符,分段加密的代码要自己处理了。/// </summary>/// <param name="privateKey">密钥</param>/// <param name="data">要加密的数据</param>/// <returns></returns>public static byte[] RsaPrivateEncrypt(string privateKey, byte[] data){string xmlPrivateKey = privateKey;//加载私钥  RSACryptoServiceProvider privateRsa = new RSACryptoServiceProvider();privateRsa.FromXmlString(xmlPrivateKey);//转换密钥  AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetKeyPair(privateRsa);//IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");// 参数与Java中加密解密的参数一致       IBufferedCipher c = CipherUtilities.GetCipher("RSA");c.Init(true, keyPair.Private); //第一个参数为true表示加密,为false表示解密;第二个参数表示密钥 byte[] DataToEncrypt = data;byte[] outBytes = c.DoFinal(DataToEncrypt);//加密  return outBytes;}/// <summary>/// 用公钥解密/// 这个方法只能加密 私钥长度/8 -11 个字符,分段加密的代码要自己处理了。/// </summary>/// <param name="data"></param>/// <param name="key"></param>/// <returns></returns>public static byte[] decryptByPublicKey(string publicKey, byte[] data){string xmlPublicKey = publicKey;RSACryptoServiceProvider publicRsa = new RSACryptoServiceProvider();publicRsa.FromXmlString(xmlPublicKey);AsymmetricKeyParameter keyPair = DotNetUtilities.GetRsaPublicKey(publicRsa);//转换密钥  // AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetRsaKeyPair(publicRsa);//IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");// 参数与Java中加密解密的参数一致       IBufferedCipher c = CipherUtilities.GetCipher("RSA");c.Init(false, keyPair); //第一个参数为true表示加密,为false表示解密;第二个参数表示密钥 byte[] DataToEncrypt = data;byte[] outBytes = c.DoFinal(DataToEncrypt);//解密  return outBytes;}}/// <summary>/// AES /// </summary>public class AESUtils{/// <summary>/// AES256加密/// </summary>/// <param name="content">明文</param>/// <param name="passphase">密钥</param>/// <returns></returns>public static string Encrypt(string content, string passphase){byte[] bytes = Encoding.UTF8.GetBytes(content);byte[] key, iv, salt = new byte[8];using (var rng = new RNGCryptoServiceProvider()){rng.GetBytes(salt); }using (var md5 = new MD5CryptoServiceProvider()){var preHash = Encoding.UTF8.GetBytes(passphase).Concat(salt).ToArray();var bs1 = md5.ComputeHash(preHash);var bs2 = md5.ComputeHash(bs1.Concat(preHash).ToArray());var bs3 = md5.ComputeHash(bs2.Concat(preHash).ToArray());key = bs1.Concat(bs2).ToArray();iv = bs3;}using (var aes = new AesCryptoServiceProvider() { Key = key, IV = iv })using (var encryptor = aes.CreateEncryptor()){var encryptedBytes = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);var concated = Encoding.UTF8.GetBytes("Salted__").Concat(salt).Concat(encryptedBytes);return Convert.ToBase64String(concated.ToArray());}}/// <summary>/// AES256解密/// </summary>/// <param name="content">密文</param>/// <param name="passphase">密钥</param>/// <returns></returns>public static string Decrypt(string content, string passphase){// openssl aes-256-cbc -k 123456789 -md md5 -e -base64//byte[] bytes = Convert.FromBase64String(content);byte[] key, iv, salt = new byte[8], encryptedBytes = new byte[bytes.Length - 8 - 8];//提取 saltArray.ConstrainedCopy(sourceArray: bytes,sourceIndex: 8, //剔除开头的 "Salted__"destinationArray: salt,destinationIndex: 0,length: salt.Length);//提取 encryptedBytesArray.ConstrainedCopy(sourceArray: bytes,sourceIndex: 8 + 8, //并剔除开头的 saltdestinationArray: encryptedBytes,destinationIndex: 0,length: encryptedBytes.Length);using (var md5 = new MD5CryptoServiceProvider()){var preHash = Encoding.UTF8.GetBytes(passphase).Concat(salt).ToArray();var bs1 = md5.ComputeHash(preHash);var bs2 = md5.ComputeHash(bs1.Concat(preHash).ToArray());var bs3 = md5.ComputeHash(bs2.Concat(preHash).ToArray());key = bs1.Concat(bs2).ToArray();iv = bs3;}using (var aes = new AesCryptoServiceProvider() { Key = key, IV = iv })using (var decryptor = aes.CreateDecryptor()){byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);return Encoding.UTF8.GetString(decryptedBytes);}}/// <summary>/// 加密 (新的思路)/// </summary>/// <param name="content">明文</param>/// <param name="passphase">密码(固定32位)</param>/// <returns>密文(base64)</returns>static string CryptoJsEncrypt_New(string content, string passphase ){byte[] bytes = Encoding.UTF8.GetBytes(content);byte[] key = Encoding.UTF8.GetBytes(passphase); //32位的keybyte[] iv = new byte[16];using (var rng = new RNGCryptoServiceProvider())rng.GetBytes(iv); // 产生16个字节的随机ivusing (var aes = new AesCryptoServiceProvider() { Key = key, IV = iv })using (var encryptor = aes.CreateEncryptor()){var encryptedBytes = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);var concated = iv.Concat(encryptedBytes);return "aes256:" + Convert.ToBase64String(concated.ToArray());}}/// <summary>/// 解密 (新的思路)/// </summary>/// <param name="content">密文(base64)</param>/// <param name="passphase">密码(固定32位)</param>/// <returns>明文</returns>static string CryptoJsDecrypt_New(string content, string passphase ){byte[] bytes = Convert.FromBase64String(content.Substring(7)); //剔除开头的 "aes256:"byte[] key = Encoding.UTF8.GetBytes(passphase); //32位的keybyte[] iv = new byte[16];byte[] encryptedBytes = new byte[bytes.Length - 16];//提取 ivArray.ConstrainedCopy(sourceArray: bytes,sourceIndex: 0,destinationArray: iv,destinationIndex: 0,length: iv.Length);//提取 加密结果Array.ConstrainedCopy(sourceArray: bytes,sourceIndex: 16, //并剔除开头的 ivdestinationArray: encryptedBytes,destinationIndex: 0,length: encryptedBytes.Length);using (var aes = new AesCryptoServiceProvider() { Key = key, IV = iv })using (var decryptor = aes.CreateDecryptor()){byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);return Encoding.UTF8.GetString(decryptedBytes);}}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/19684.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python Django 5 Web应用开发实战

Django 是一个非常强大的 Python Web 框架&#xff0c;它可以帮助你快速、安全地开发高质量的 Web 应用。以下是一个基于 Django 的 Web 应用开发实战的概述&#xff0c;我们将分步骤介绍如何构建一个包含五个基本功能的 Web 应用。 步骤 1: 安装和设置 Django 安装 Django&am…

ODBC访问达梦数据库Ubuntu18.04 x86-x64(亲测有效)

ODBC访问达梦数据库Ubuntu18.04 x86-x64 第1步&#xff1a;安装unixodbc驱动,使用下面命令。第2步&#xff1a;拷贝已经安装好的达梦数据库驱动程序第3步&#xff1a;配置ODBC必要的参数文件&#xff0c;如下图第4步&#xff1a;设置环境变量第5步&#xff1a;连接测试 说明&am…

如何 使用Cubemax配置串口1.5得停止位

title: Cubemax配置串口1.5得停止位 tags: STM32HalCubemax Cubemax里面没有这个修改停止位为1.5得 我去配置函数里面也没看见 然后我去刨基础库 发现了 1.5倍 发现 原来就是 那个寄存器的值 改成 》 0x3000 后来自己修改几笔 HAL 库里面好像也有 改之 USART_CR2_STOP_Po…

【农村电商1004】 电子商务进农村示范县名单:全面数据集等你探索!

今天给大家分享的发表在国内顶级期刊金融研究的2023年论文《农村发展电子商务能减缓资本与劳动力要素外流吗&#xff1f;——以电子商务进农村综合示范案例为例》使用到的重要数据集电子商务进农村综合示范政策县数据&#xff0c;该论文采用了双重差分法和全国县域面板数据研究…

JDK JRE JVM 三者的关系

总结&#xff1a; 1. jdk 中 的 javac 编译器将 .java 文件编译为 .class 字节码文件 &#xff08;编译&#xff09; 2. jre 执行 .class 字节码文件 &#xff08;运行&#xff09; 3. jre 通过 jvm 运行程序&#xff0c;确保程序能够在不同平台上正确执行&#xff08;实现跨平…

【Spring Cloud】微服务日志收集系统-ELK+Kafka

目录 任务背景本文相关文件资料Elasticsearch特性 LogstashKibanaELKELK的缺点引入消息中间件 ELKKafkaKafka概念 ELKKafka环境搭建1.将安装素材上传至服务器 cd /usr/local/soft2.防止Elasticsearch因虚拟内存问题启动失败3.创建镜像li/centos7-elasticsearch4.创建容器5.验证…

FOC - BLDC六步换相驱动原理

文章目录 1 . 前言2 . 电机旋转原理3 . BLDC特点4 . BLDC反电动势投影位置5 . BLDC换相时刻6 . BLDC换相注意事项7 . 小结 【全文大纲】 : https://blog.csdn.net/Engineer_LU/article/details/135149485 1 . 前言 无刷直流电机在这里区分为两种&#xff0c;一是永磁无刷直流电…

MedSAM 学习笔记(续):训练自定义数据集

1、下载官方权重 官方的预训练权重:https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth 下载后保存在:work_dir/SAM/sam_vit_b_01ec64.pth 目录 2、摆放数据集 因为MedSAM 分割模型需要对3D数据集进行切片处理,也就是对nii.gz 数据处理成 npy 格式 …

掌握这三点软文营销秘诀,快速实现品牌用户增长

在信息爆炸的时代&#xff0c;品牌用户如何快速实现增长已成为每个企业发展的必经之虑。而软文营销&#xff0c;作为品牌推广中不可或缺的一环&#xff0c;也正逐渐受到越来越多企业的青睐。它与传统的硬广告相比&#xff0c;不会直接的进行产品销售&#xff0c;软文更注重与读…

object.defineProperty和proxy

1、object.defineProperty() 作用 在一个对象上定义一个新属性&#xff0c;或者修改一个对象的现有属性&#xff0c;并返回这个对象 基本使用 语法&#xff1a;Object.defineProperty(obj, prop, descriptor) 参数&#xff1a; 要添加属性的对象 要定义或修改的属性的名称…

泛微ecology开发修炼之旅

我将多年泛微ecology开发经验&#xff0c;进行了总结&#xff0c;然后分享给大家。 泛微开发修炼之旅 泛微开发修炼之旅--01搭建开发环境 泛微开发修炼之旅--02开发接口demo 泛微开发修炼之旅--03常用数据表结构讲解 泛微开发修炼之旅--04常用数据库操作工具类封装 。。。。 我…

iOS ------ 多线程 GCD

一&#xff0c;GCD简介 GCD是Apple开发的一个多线程的较新的解决方案。它主要用于优化应用程序以支持多核处理器以及其他对称处理系统。它是一个在线程池模式的基础上执行的并发任务。 为什么要使用GCD&#xff1f; GCD&#xff01;可用于多核的并行运算GCD会自动利用更多的…

中高级前端开发岗

定位&#xff1a; 日常迭代任务的核心研发&#xff0c;具备高质、高效完成迭代任务的能力。 素质要求&#xff1a; 业务专家或擅长某一方向技术&#xff1b;有较丰富的开发经验&#xff1b;需要具备良好的沟通和协作能力&#xff0c;能够与其他部门和团队进行有效的沟通和协…

Linux配置java,maven,marshalsec环境

文章目录 一. Linux配置java环境1.下载jdk文件2.解压tar.gz文件3.设置java环境变量4.验证是否成功 二. Linux配置maven环境1.下载压缩包2.解压tar.gz3. 配置环境变量 三. Linux配置marshalsec环境 一. Linux配置java环境 1.下载jdk文件 mkdir /opt/javawget https://repo.hua…

STM32使用ST-LINK下载程序中需要注意的几点

使用keil5的ST-link下载界面 前提是ST-LINK已经连接好&#xff0c;&#xff08;下图中是没有连接ST-link设备&#xff09;&#xff0c;只是为了展示如何查看STlink设备是否连接的方式 下载前一定设置下载完成后自启动 这个虽然不是必须&#xff0c;但对立即看到新程序的现象…

CVE-2020-0688 远程代码执行漏洞

CVE-2020-0688 远程代码执行漏洞 漏洞产生的主要原因就是在Exchange ECP组件中发现&#xff0c;邮件服务在安装的过程中不会随机生成秘钥&#xff0c;也就是说所有默认安装的Exchange服务器中的validationKey和decryptionKey的值都是相同的&#xff0c;攻击者可以利用静态秘钥…

【深度学习基础】使用Pytorch搭建DNN深度神经网络与手写数字识别

目录 写在开头 一、DNN的搭建 问题描述与数据集 神经网络搭建 模型训练 模型评估 模型复用 二、手写数字识别 任务描述 数据集 神经网络搭建 模型训练 模型评估 写在最后 写在开头 本文将介绍如何使用PyTorch框架搭建深度神经网络模型。实现模型的搭建、模…

开发数据库的思路

一、设计目的 关系型网络数据库。 二、设计要求&#xff1a; (一)安装快速 安装程序特别简单。 (二)配置简单 最小化配置。 (三)使用简单&#xff0c;高效 程序占用空间小&#xff0c;支持基本的SQL语法。 (四)稳定可靠 尽量不依赖其他不稳定库。 (五)其他要求 属于关系型数…

USART串口外设

USART介绍 USART&#xff1a;另外我们经常还会遇到串口&#xff0c;叫UART&#xff0c;少了个S&#xff0c;就是通用异步收发器&#xff0c;一般我们串口很少使用这个同步功能&#xff0c;所以USART和UART使用起来&#xff0c;也没有什么区别。 其实这个STM32的USART同步模式&a…

大模型日报2024-05-30

大模型日报 2024-05-30 大模型资讯 大型语言模型未来将超越人类金融分析师 摘要: 新研究表明&#xff0c;大型语言模型在金融预测方面表现优于人类专家&#xff0c;为交易策略提供了宝贵的洞见。 DLM技术提高语音识别准确率 摘要: 本文介绍了通过去噪语言模型&#xff08;DLM&a…