diff --git a/src/NSExt/Extensions/ByteExtensions.cs b/src/NSExt/Extensions/ByteExtensions.cs index c2813be..abfa269 100644 --- a/src/NSExt/Extensions/ByteExtensions.cs +++ b/src/NSExt/Extensions/ByteExtensions.cs @@ -23,6 +23,15 @@ public static class ByteExtensions { return e.GetString(me); } -} + /// + /// 将字节数组解码成字符串 + /// + /// 字节数组 + /// 解码后的原始字符串 + public static string HexDe(this byte[] me) + { + return me.HexDe(Encoding.UTF8); + } +} \ No newline at end of file diff --git a/src/NSExt/Extensions/StringExtensions.cs b/src/NSExt/Extensions/StringExtensions.cs index dbf8d63..20b2ba4 100644 --- a/src/NSExt/Extensions/StringExtensions.cs +++ b/src/NSExt/Extensions/StringExtensions.cs @@ -201,6 +201,17 @@ public static class StringExtensions return e.GetBytes(me); } + /// + /// 将字符串转换成字节数组形式 + /// + /// 字符串 + /// 字节数组 + public static byte[] Hex(this string me) + { + return me.Hex(Encoding.UTF8); + } + + /// /// 对一个字符串进行sha1 hash运算 /// @@ -238,6 +249,26 @@ public static class StringExtensions } + /// + /// string to double + /// + /// string + /// Int32 + public static double Double(this string me) + { + return double.Parse(me, CultureInfo.CurrentCulture); + } + + /// + /// string to float + /// + /// string + /// Int32 + public static float Float(this string me) + { + return float.Parse(me, CultureInfo.CurrentCulture); + } + /// /// 尝试将字符串转为int32 /// @@ -484,4 +515,63 @@ public static class StringExtensions { return Uri.UnescapeDataString(me); } + + + /// + /// 蛇形命名 + /// + /// + /// + public static string Snakecase(this string me) + { + return Regex.Replace(me, "([A-Z])", "-$1").ToLower().TrimStart('-'); + } + + + /// + /// aes加密 + /// + /// 要加密的串 + /// 密钥 + /// 指定要用于加密的块密码模式。 + /// 指定在消息数据块短于加密操作所需的完整字节数时要应用的填充类型。 + /// + public static string Aes(this string me, + string key, + CipherMode cipherMode = CipherMode.ECB, + PaddingMode paddingMode = PaddingMode.PKCS7) + { + using var aes = System.Security.Cryptography.Aes.Create(); + aes.Padding = PaddingMode.PKCS7; + aes.Mode = CipherMode.ECB; + aes.Key = key.Hex(); + using var encryptor = aes.CreateEncryptor(); + var bytes = me.Hex(); + var decrypted = encryptor.TransformFinalBlock(bytes, 0, bytes.Length); + return decrypted.Base64(); + } + + + /// + /// aes解密 + /// + /// 要加密的串 + /// 密钥 + /// 指定要用于加密的块密码模式。 + /// 指定在消息数据块短于加密操作所需的完整字节数时要应用的填充类型。 + /// + public static string AesDe(this string me, + string key, + CipherMode cipherMode = CipherMode.ECB, + PaddingMode paddingMode = PaddingMode.PKCS7) + { + using var aes = System.Security.Cryptography.Aes.Create(); + aes.Padding = PaddingMode.PKCS7; + aes.Mode = CipherMode.ECB; + aes.Key = key.Hex(); + using var encryptor = aes.CreateDecryptor(); + var bytes = me.Base64De(); + var decrypted = encryptor.TransformFinalBlock(bytes, 0, bytes.Length); + return decrypted.HexDe(); + } } \ No newline at end of file