开始开源项目OpenTools的创作

2024-08-13 19:01:56 浏览数 (2)

前言:开始开源项目的创作。目的是开发一系列方便大家拿来即可使用的工具集合,希望能够让.net的生态更加丰富。该项目采用MIT开源。并且如果引用nuget包,该项目可以兼容.net framework4.6.1以及以上的所有.nrt framework以及所有.net core或以上环境的所有.net版本。

Wesky.Net.OpenTools,开源地址:https://gitee.com/dreamer_j/open-tools.git

第一个nuget包版本,目前只有AES加密和解密,以及Byte数据转换两个功能。将来会继续不断完善、拓展。

先使用下第一个功能,AES加解密。

编写一个简单的控制台,,nuget包引用 WSesky.Net.OpenTools

编写加密和解密功能

运行,得到加密和解密结果。

加密解密代码解析:

代码语言:javascript复制
 /// <summary>
 /// 使用AES加密算法加密文本。
 /// Encrypts the text using AES encryption algorithm.
 /// </summary>
 /// <param name="key">加密密钥,必须是32字符长。/ Encryption key, must be 32 characters long.</param>
 /// <param name="password">要加密的文本。/ The text to be encrypted.</param>
 /// <param name="iv">初始化向量,必须是16字符长。/ Initialization vector, must be 16 characters long.</param>
 /// <returns>加密后的十六进制字符串。/ Encrypted text in hexadecimal string format.</returns>
 public static string AesEncrypt(string key, string password, string iv)
 {
     if (key == null || key.Length < 32)
         throw new ArgumentException("Key must be at least 32 characters long.", nameof(key));
     if (iv == null || iv.Length < 16)
         throw new ArgumentException("IV must be at least 16 characters long.", nameof(iv));

     byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 32));
     byte[] ivBytes = Encoding.UTF8.GetBytes(iv.Substring(0, 16));

     using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
     {
         aesAlg.Key = keyBytes;
         aesAlg.IV = ivBytes;

         ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
         using (MemoryStream msEncrypt = new MemoryStream())
         {
             using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
             {
                 using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                 {
                     swEncrypt.Write(password);
                 }
                 return ByteConvert.ByteArrayToHexString(msEncrypt.ToArray());
             }
         }
     }
 }

 /// <summary>
 /// 使用AES解密算法解密文本。
 /// Decrypts the text using the AES decryption algorithm.
 /// </summary>
 /// <param name="key">解密密钥,必须是32字符长。/ Decryption key, must be 32 characters long.</param>
 /// <param name="encryptedText">要解密的文本,以十六进制字符串格式。/ The text to be decrypted, in hexadecimal string format.</param>
 /// <param name="iv">初始化向量,必须是16字符长。/ Initialization vector, must be 16 characters long.</param>
 /// <returns>解密后的字符串。/ Decrypted string.</returns>
 public static string AESDecrypt(string key, string encryptedText, string iv)
 {
     if (key == null || key.Length < 32)
         throw new ArgumentException("Key must be at least 32 characters long.", nameof(key));
     if (iv == null || iv.Length < 16)
         throw new ArgumentException("IV must be at least 16 characters long.", nameof(iv));

     byte[] inputBytes = ByteConvert.HexStringToByteArray(encryptedText);
     byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 32));
     byte[] ivBytes = Encoding.UTF8.GetBytes(iv.Substring(0, 16));

     using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
     {
         aesAlg.Key = keyBytes;
         aesAlg.IV = ivBytes;

         ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
         using (MemoryStream msDecrypt = new MemoryStream(inputBytes))
         {
             using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
             {
                 using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                 {
                     return srDecrypt.ReadToEnd();
                 }
             }
         }
     }
 }

0 人点赞