C#\u4e2d\u6709\u5f88\u591a\u52a0\u5bc6\u7b97\u6cd5\u548c\u5e93\u53ef\u4f9b\u4f7f\u7528\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u4e2d\u6587\u52a0\u5bc6\u8f6f\u4ef6\u3002\n\ncsharp\nusing System;\nusing System.Security.Cryptography;\nusing System.Text;\n\nnamespace EncryptionApp\n{\n class Program\n {\n static void Main(string[] args)\n {\n Console.WriteLine("\u8f93\u5165\u8981\u52a0\u5bc6\u7684\u6587\u672c\uff1a");\n string plainText = Console.ReadLine();\n\n Console.WriteLine("\u8bf7\u9009\u62e9\u52a0\u5bc6\u7b97\u6cd5\uff1a");\n Console.WriteLine("1. MD5");\n Console.WriteLine("2. SHA1");\n Console.WriteLine("3. SHA256");\n Console.WriteLine("4. AES");\n\n int algorithmChoice = int.Parse(Console.ReadLine());\n\n string encryptedText = string.Empty;\n\n switch (algorithmChoice)\n {\n case 1:\n encryptedText = EncryptMD5(plainText);\n break;\n case 2:\n encryptedText = EncryptSHA1(plainText);\n break;\n case 3:\n encryptedText = EncryptSHA256(plainText);\n break;\n case 4:\n Console.WriteLine("\u8f93\u5165\u52a0\u5bc6\u5bc6\u94a5\uff1a");\n string aesKey = Console.ReadLine();\n Console.WriteLine("\u8f93\u5165\u52a0\u5bc6\u521d\u59cb\u5411\u91cf\uff1a");\n string aesIV = Console.ReadLine();\n encryptedText = EncryptAES(plainText, aesKey, aesIV);\n break;\n default:\n Console.WriteLine("\u65e0\u6548\u7684\u9009\u62e9\u3002");\n return;\n }\n\n Console.WriteLine("\u52a0\u5bc6\u540e\u7684\u6587\u672c\uff1a");\n Console.WriteLine(encryptedText);\n }\n\n static string EncryptMD5(string plainText)\n {\n using (MD5 md5 = MD5.Create())\n {\n byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(plainText));\n\n StringBuilder builder = new StringBuilder();\n for (int i = 0; i < hash.Length; i++)\n {\n builder.Append(hash[i].ToString("x2"));\n }\n\n return builder.ToString();\n }\n }\n\n static string EncryptSHA1(string plainText)\n {\n using (SHA1 sha1 = SHA1.Create())\n {\n byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(plainText));\n\n StringBuilder builder = new StringBuilder();\n for (int i = 0; i < hash.Length; i++)\n {\n builder.Append(hash[i].ToString("x2"));\n }\n\n return builder.ToString();\n }\n }\n\n static string EncryptSHA256(string plainText)\n {\n using (SHA256 sha256 = SHA256.Create())\n {\n byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(plainText));\n\n StringBuilder builder = new StringBuilder();\n for (int i = 0; i < hash.Length; i++)\n {\n builder.Append(hash[i].ToString("x2"));\n }\n\n return builder.ToString();\n }\n }\n\n static string EncryptAES(string plainText, string key, string iv)\n {\n using (Aes aes = Aes.Create())\n {\n aes.Key = Encoding.UTF8.GetBytes(key);\n aes.IV = Encoding.UTF8.GetBytes(iv);\n\n ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);\n\n byte[] encryptedBytes;\n using (var memoryStream = new System.IO.MemoryStream())\n {\n using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))\n {\n using (var streamWriter = new System.IO.StreamWriter(cryptoStream))\n {\n streamWriter.Write(plainText);\n }\n encryptedBytes = memoryStream.ToArray();\n }\n }\n\n return Convert.ToBase64String(encryptedBytes);\n }\n }\n }\n}\n\n\n\u6b64\u4ee3\u7801\u793a\u4f8b\u5305\u542b\u4e86\u4e2d\u6587\u7684 MD5\u3001SHA1\u3001SHA256\u548c\u4e2d\u6587\u7684\u52a0\u5bc6\u7b97\u6cd5\u7684\u793a\u4f8b\u3002\u4f7f\u7528\u65f6\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u8981\u4f7f\u7528\u7684\u52a0\u5bc6\u7b97\u6cd5\uff0c\u5e76\u8f93\u5165\u8981\u52a0\u5bc6\u7684\u6587\u672c\u3002\u5bf9\u4e8e\u4e2d\u6587\u7684\u52a0\u5bc6\uff0c\u7528\u6237\u8fd8\u9700\u8981\u8f93\u5165\u4e00\u4e2a\u5bc6\u94a5\u548c\u4e00\u4e2a\u521d\u59cb\u5411\u91cf\u3002\u4ee3\u7801\u5c06\u52a0\u5bc6\u540e\u7684\u6587\u672c\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\u3002\n\n\u8bf7\u6ce8\u610f\uff0c\u8fd9\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4e0d\u5305\u542b\u5168\u90e8\u7684\u9519\u8bef\u5904\u7406\u548c\u5b89\u5168\u63aa\u65bd\u3002\u5728\u5b9e\u9645\u7684\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u5f97\u5f53\u7684\u4fee\u6539\u548c\u6539\u8fdb\u3002


原文地址: http://www.cveoy.top/t/topic/pNro 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录