.NET中的密码加解密代码我想简单的在C#中加解密密码。如何在数据库中以加密格式保存密码并通过解密检索原始格式?干得好。我在互联网上的某个地方找到了它。对我来说很好用。//////加密给定密码并返回加密数据///作为base64字符串。//////需要///保护的未加密字符串。///代表加密///二进制数据的base64编码字符串。//////这个解决方案并不真正安全,因为我们///将字符串保存在内存中。如果运行时保护必不可少,则应使用///。///如果///是空引用。publicstringEncrypt(stringplainText){if(plainText==null)thrownewArgumentNullException("plainText");//加密数据vardata=Encoding.Unicode.GetBytes(plainText);byte[]encrypted=ProtectedData.Protect(data,null,Scope);//返回base64字符串returnConvert.ToBase64String(encrypted);}//////解密给定的字符串。//////通过或///扩展方法创建的base64编码字符串///。///解密后的字符串。///请记住,解密后的字符串会保留在内存中///并使您的应用程序容易受到攻击本身能。如果运行时保护///必不可少,则应使用。///如果///是空引用。publicstringDecrypt(stringcipher){if(cipher==null)thrownewArgumentNullException("cipher");//解析base64字符串byte[]data=Convert.FromBase64String(cipher);//解密数据byte[]decrypted=ProtectedData.Unprotect(data,null,Scope);返回Encoding.Unicode.GetString(解密);编辑:这是一个非常古老的答案,SHA1在2011年被弃用,现在在实践中被打破了。https://shattered.io/改用更新的标准(例如SHA256、SHA512等)。如果您对我的评论中的问题回答“否”,我正在使用:publicstaticbyte[]HashPassword(stringpassword){varprovider=newSHA1CryptoServiceProvider();varencoding=newUnicodeEncoding();返回provider.ComputeHash(encoding.GetBytes(password));这个问题将回答如何加密/解密:加密和解密字符串你没有指定数据库,但你需要使用Convert.toBase64String对其进行base-64编码。例如,您可以使用:http://www.opinionatedgeek.com/Blog/blogentry=000361/BlogEntry.aspx然后您可以将其保存在varchar或clob中,具体取决于加密消息的长度,但对于密码,varchar应该工作。上面的示例还将涵盖解码base64后的解密更新:实际上,您可能不需要使用base64编码,但我发现它在我想打印它或通过网络发送它时很有用。如果消息足够长,我发现先压缩再加密会有所帮助,因为当消息已经是二进制形式时很难使用暴力破解,所以很难判断何时成功破解加密。我使用RC2CryptoServiceProvider。publicstaticstringEncryptText(stringopenText){RC2CryptoServiceProviderrc2CSP=newRC2CryptoServiceProvider();ICryptoTransform加密器=rc2CSP.CreateEncryptor(Convert.FromBase64String(c_key),Convert.FromBase64String(c_iv));使用(MemoryStreammsEncrypt=newMemoryStream()){使用(CryptoStreamcsEncrypt=newCryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write)){byte[]toEncrypt=Encoding.Unicode.GetBytes(openText);csEncrypt.Write(toEncrypt,0,toEncrypt.Length);csEncrypt.FlushFinalBlock();byte[]encrypted=msEncrypt.ToArray();返回Convert.ToBase64String(加密);}}}publicstaticstringDecryptText(stringencryptedText){RC2CryptoServiceProviderrc2CSP=newRC2CryptoServiceProvider();ICryptoTransform解密器=rc2CSP.CreateDecryptor(Convert.FromBase64String(c_key),Convert.FromBase64String(c_iv));使用(MemoryStreammsDecrypt=newMemoryStream(Convert.FromBase64String(encryptedText))){使用(加密StreamcsDecrypt=newCryptoStream(msDecrypt,decryptor,CryptoStreamMode.Read)){列表字节=newList();诠释乙;做{b=csDecrypt.ReadByte();如果(b!=-1){bytes.Add(Convert.ToByte(b));}}while(b!=-1);返回Encoding.Unicode.GetString(bytes.ToArray());}}}首先创建一个类:publicclassEncryption{publicstaticstringEncrypt(stringclearText){stringEncryptionKey="MAKV2SPBNI99212";byte[]clearBytes=Encoding.Unicode.GetBytes(clearText);使用(Aes加密器=Aes.Create()){Rfc2898DeriveBytespdb=newRfc2898DeriveBytes(EncryptionKey,newbyte[]{0x49,0x76,0x61,0x6e,0x20,0x4d,0x65,0x64,0x76,0x65,0x64,0x765});encryptor.Key=pdb.GetBytes(32);encryptor.IV=pdb.GetBytes(16);使用(MemoryStreamms=newMemoryStream()){使用(CryptoStreamcs=newCryptoStream(ms,encryptor.CreateEncryptor(),CryptoStreamMode.Write)){cs.Write(clearBytes,0,clearBytes.Length);cs.关闭();}clearText=Convert.ToBase64String(ms.ToArray());}}返回明文;}publicstaticstringDecrypt(stringcipherText){stringEncryptionKey="MAKV2SPBNI99212";byte[]cipherBytes=Convert.FromBase64String(cipherText);使用(Aes加密器=Aes.Create()){Rfc2898DeriveBytespdb=newRfc2898DeriveBytes(EncryptionKey,newbyte[]{0x49,0x76,0x61,0x6e,0x20,0x4d,0x65,0x64,0x76,0x65,0x64,0x765});encryptor.Key=pdb.GetBytes(32);encryptor.IV=pdb.GetBytes(16);使用(MemoryStreamms=newMemoryStream()){使用(CryptoStreamcs=newCryptoStream(ms,encryptor.CreateDecryptor(),CryptoStreamMode.Write)){cs.Write(cipherBytes,0,cipherBytes.Length);cs.关闭();}cipherText=Encoding.Unicode.GetString(ms.ToArray());}}返回密文;}}**在控制器**添加这种加密类的参考:usingtestdemo.ModelspublicActionResultIndex(){returnView();}[HttpPost]publicActionResultIndex(stringtext){if(Request["txtEncrypt"]!=null){stringgetEncryptionCode=Request["txtEncrypt"];字符串DecryptCode=Encryption.Decrypt(HttpUtility.UrlDecode(getEncryptionCode));ViewBag.GetDecryptCode=DecryptCode;返回视图();}else{stringgetDecryptCode=Request["txtDecrypt"];.UrlEncode(加密。加密(getDecryptCode));ViewBag.GetEncryptionCode=EncryptionCode;返回视图();}}解密代码@using(Html.BeginForm()){加密代码@ViewBag.GetDecryptCode}加密代码@using(Html.BeginForm()){解密代码@ViewBag.GetEncryptionCode}最简单的加密方法之一(如果你有自己创建一个,因为.NET已经拥有如此强大的加密库[由我以前的Cogwheel提供])是将输入字符串的每个字符的ASCII值与已知的“键”值进行XORC#中的XOR函数是使用什么完成的我认为是^键。然后,您可以将XOR的结果中的值转换回ASCII字符,并将它们存储在数据库中。它不是很安全,但它是最简单的加密方法之一。此外,如果使用对数据库的访问,我发现某些字符放在字符串前面时会使整个字段在打开数据库本身时不可读。但是,即使是恶意用户,您的应用程序仍然可以读取该字段。但是谁不再使用访问权限了呢?stringclearText=txtPassword.Text;字符串加密密钥="MAKV2SPBNI99212";byte[]clearBytes=Encoding.Unicode.GetBytes(clearText);使用(Aes加密器=Aes.Create()){Rfc2898DeriveBytespdb=newRfc2898DeriveBytes(字节[]{0x49、0x76、0x61、0x6e、0x20、0x4d、0x65、0x64、0x76、0x65、0x64、0x65、0x76);encryptor.Key=pdb.GetBytes(32);encryptor.IV=pdb.GetBytes(16);使用(MemoryStreamms=newMemoryStream()){使用(CryptoStreamcs=newCryptoStream(ms,encryptor.CreateEncryptor(),CryptoStreamMode.Write)){cs.Write(clearBytes,0,clearBytes.Length);CS.关闭();}clearText=Convert.ToBase64String(ms.ToArray());您可以使用托管的.Net加密库,然后将加密的字符串保存到数据库中。如果要验证密码,可以将存储的数据库字符串与用户输入的散列值进行比较。有关SHA512Managed的??更多信息,请参阅此处的UsingSystem.Security.Cryptography;以上就是《C#学习教程:.NET密码加解密代码分享》的全部内容,如果对你有用,还需要了解更多C#学习教程,希望大家多多关注——publicstaticstringEncryptSHA512Managed(字符串密码){UnicodeEncodinguEncode=newUnicodeEncoding();byte[]bytPassword=uEncode.GetBytes(密码);SHA512Managedsha=newSHA512Managed();byte[]hash=sha.ComputeHash(bytPassword);返回Convert.ToBase64String(哈希);}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
