如何在我的应用程序中加密用户设置(如密码)?我想为用户提供保存加密个人数据的能力。这可能是微不足道的,或者之前可能有人问过,但我找不到一个易于使用的方法来加密/解密密码的示例。我真的不需要任何超级神奇的牢不可破的密码。我只需要难以破解的密码。我看过一些msdn和SO问题,但没有找到可以使用的东西。大卫,我认为你的答案很漂亮,但我认为这些作为扩展方法会更加狡猾。这将允许这样的语法:stringcypherText;字符串明文;使用(varsecureString="Somestringtoencrypt".ToSecureString()){cypherText=secureString.EncryptString();}使用(varsecureString=cypherText.DecryptString()){clearText=secureString.ToInsecureString();这是更新的代码:usingSystem;使用System.Collections.Generic;使用System.Runtime.InteropServices;使用系统安全;使用System.Security.Cryptography;使用系统文本;publicstaticclassSecureIt{privatestaticreadonlybyte[]entropy=Encoding.Unicode.GetBytes("SaltIsNotAPassword");publicstaticstringEncryptString(thisSecureStringinput){if(input==null){returnnull;}varencryptedData=ProtectedData.Protect(Encoding.Unicode.GetBytes(input.ToInsecureString()),entropy,DataProtectionScope.CurrentUser);返回Convert.ToBase64String(encryptedData);}publicstaticSecureStringDecryptString(此字符串加密数据){如果(加密数据==null){返回n空;}try{vardecryptedData=ProtectedData.Unprotect(Convert.FromBase64String(encryptedData),entropy,DataProtectionScope.CurrentUser);返回Encoding.Unicode.GetString(decryptedData).ToSecureString();}catch{返回新的SecureString();}publicstaticSecureStringToSecureString(thisIEnumerableinput){if(input==null){returnnull;}varsecure=newSecureString();foreach(输入中的varc){安全。追加字符(c);}安全的。只读();返回安全;}publicstaticstringToInsecureString(thisSecureStringinput){if(input==null){returnnull;}varptr=Marshal.SecureStringToBSTR(输入);尝试{returnMarshal.PtrToStringBSTR(ptr);最后{Marshal.ZeroFreeBSTR(ptr);下面就是这么简单,假设你真的只想加密/解密字符串并将其存储到磁盘请注意,这不使用密码,它使用具有System.Security的登录用户。Cryptography.DataProtectionScope.CurrentUser的安全上下文来保护数据。publicclassSecureIt{staticbyte[]entropy=System.Text.Encoding.Unicode.GetBytes("SaltIsNotAPassword");公共静态字符串EncryptString(System.Security.SecureString输入){byte[]encryptedData=System.Security.Cryptography.ProtectedData.Protect(System.Text.Encoding.Unicode.GetBytes(ToInsecureString(输入)),熵,System.Security。密码学.DataProtectionScope.CurrentUser);返回Convert.ToBase64String(encryptedData);}publicstaticSecureStringDecryptString(stringencryptedData){try{byte[]decryptedData=System.Security.Cryptography.ProtectedData.Unprotect(Convert.FromBase64String(encryptedData),熵,System.Security.Cryptography.DataProtectionScope.CurrentUser);返回ToSecureString(System.Text.Encoding.Unicode.GetString(解密数据));}catch{返回新的SecureString();}}publicstaticSecureStringToSecureString(字符串输入){SecureStringsecure=newSecureString();foreach(输入中的字符c){安全e.AppendChar(c);}secure.MakeReadOnly();返回安全;}publicstaticstringToInsecureString(SecureStringinput){stringreturnValue=string.Empty;IntPtrptr=System.Runtime.InteropServices.Marshal.SecureStringToBSTR(输入);尝试{returnValue=System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr);}最后{System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ptr);}返回返回值;然后加密字符串:varclearText="Somestringtoencrypt";varcypherText=SecureIt.EncryptString(SecureIt.ToSecureString(clearText));随后解密:varclearText=SecureIt.ToInsecureString(SecureIt.DecryptString(cypherText));我为我的目的修改了JesseC.Slicer解决方案,不要使用SecureString因为保护内存对我来说并不重要我只需要序列化加密的字符串。为了使这项工作正常进行,项目需要引用System.Security(而不仅仅是使用语句)。publicstaticclassStringSecurityHelper{privatestaticreadonlybyte[]entropy=Encoding.Unicode.GetBytes("5ID'&mc%sJo@lGtbi%n!G^fiVn8*tNh3eB%rDaVijn!.cb");publicstaticstringEncryptString(thisstringinput){if(input==null){returnnull;}byte[]encryptedData=ProtectedData.Protect(Encoding.Unicode.GetBytes(input),entropy,DataProtectionScope.CurrentUser);返回Convert.ToBase64String(encryptedData);}publicstaticstringDecryptString(thisstringencryptedData){if(encryptedData==null){returnnull;}try{byte[]decryptedData=ProtectedData.Unprotect(Convert.FromBase64String(encryptedData),entropy,DataProtectionScope.CurrentUser);返回Encoding.Unicode.GetString(decryptedData);}赶上{返回空;}}}并使用:以上是C#学习教程:Howtoencryptusersettings(suchaspasswordsinmyapplication)?如果分享的内容对你有用,需要了解更多C#学习教程,希望大家多多关注——stringcypherText="Mystring".EncryptString();字符串clearText=cypherText.DecryptString();本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
