目前我在类文件本身中存储了我的C#mysql连接信息,这看起来并不聪明,因为最终用户可以简单地使用反射器来查看源代码,以防它没有被模糊.
我怎么能以安全的方式存储这些信息?
源代码:
private void Initialize()
{
server = "xxx";
database = "xxx";
uid = "xxx";
password = "xxx";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
Run Code Online (Sandbox Code Playgroud) 我需要加密和解密字符串值,例如电子邮件地址和数值,但加密的字符串中不应包含'/',因为我在URL中使用它并使用'/'作为分隔符来获取某些值.
我目前正在使用以下方法:
string passPhrase = "Pas5pr@se"; // can be any string
string saltValue = "s@1tValue"; // can be any string
string hashAlgorithm = "SHA1"; // can be "MD5"
int passwordIterations = 2; // can be any number
string initVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
int keySize = 256; // can be 192 or 128
public string Encrypt(string plainText)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase,saltValueBytes,hashAlgorithm,passwordIterations);
byte[] keyBytes …Run Code Online (Sandbox Code Playgroud) 在我的 Windows 服务中,我需要能够检索存储在同一网络上的 SQL Server 2012 数据库中的第三方 REST API 的凭据。我的每个客户可能都分配有不同的 API 凭证。例如:
Customer Name | API ID | API Password In Plain Text
-----------------------------------------------------
Customer 1 1234 somepassword
Customer 2 1234 somepassword
Customer 3 5678 anotherpassword
Run Code Online (Sandbox Code Playgroud)
在此服务的第一次迭代中,所有客户都使用相同的 API 凭据,并使用SectionInformation.ProtectSection在 Windows 服务的 app.config 中对其进行加密。
我是否只使用 .NET 框架提供的加密/解密方法之一并将该值存储在数据库中?例如,此处提供的解决方案之一:Encrypting & Decrypting a String in C# ? 我可以研究任何建议或其他解决方案?
我正在使用Visual Studio,我对存储配置字符串的最佳方法感到困惑.我正在创建一个Windows窗体应用程序.我需要非常基本的安全性 - 我不希望密码在app.config中可读,但我并不担心有人为了解决这个问题而反汇编我的代码.
因此,在数据源向导中,我说"不保存密码",然后我将以下代码放在Settings.Designer.CS中:
public string MyConnectionString {
get {
return ((string)("Data Source=SQLSERVER\\ACCOUNTING;Initial Catalog=ACCOUNTING;User ID=MyUser;Password=28947239SKJFKJF"));
}
}
Run Code Online (Sandbox Code Playgroud)
我意识到这不是最好的解决方案,但我想不出更好的解决方案.我很感激任何人的帮助和意见.
谢谢 -
大小姐.
更新:问题是我!我犯了一个错误,否则两个 Cods(下面和PS上的那个都是正确的)但仍然感谢 @Luke Park 的精彩回答,我学到了一些新东西。
我对加密/解密算法不熟悉,因此我在网上搜索并找到了这个类:
代码是:(我在Decrypt方法中添加了一个 Try/Catch,以防密码错误return "";)
using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Linq;
namespace EncryptStringSample
{
public static class StringCipher
{
// This constant is used to determine the keysize of the encryption algorithm in bits.
// We divide this by 8 within the code below to get the equivalent number of bytes.
private const int Keysize = 256;
// This constant …Run Code Online (Sandbox Code Playgroud) 我试图将以下c#代码转换为PHP,但我不知道要使用哪个pack()参数.
var xorkey = BitConverter.ToUInt32(filedata, 0) >> 8;
xorkey *= 0x8083;
for (var i = 8; i < filedata.Length; i += 0x4)
{
BitConverter.GetBytes(BitConverter.ToUInt32(filedata, i) ^ xorkey).CopyTo(filedata, i);
xorkey ^= BitConverter.ToUInt32(filedata, i);
}
filedata = filedata.Skip(4).ToArray();
Run Code Online (Sandbox Code Playgroud)
编辑 - 一直在努力,我到目前为止似乎工作.
$xorkey = unpack("L", $filedata)[1] >> 8;
$xorkey *= 32899; // 32899 == 0x8083
for ($i = 8; $i < strlen($filedata); $i += 4) {
$bytes = unpack("L", substr($filedata, $i))[1] ^ $xorkey;
// left to do: BitConverter.GetBytes($bytes).CopyTo(filedata, i);
// this doesn't work: …Run Code Online (Sandbox Code Playgroud) 我正在尝试修改一个示例RinjaelManaged 加密类(请参阅:加密和解密 C# 中的字符串)以改为使用 AesCryptoServiceProvider,以便它可以在设置为仅使用 FIPS 兼容算法的计算机上运行。
然而,这似乎不像换出类名那么简单,因为我现在收到关于初始化向量长度的错误。我意识到已经有几个关于此的问题,但是在我的特定用例中尝试使用其他问题的答案时没有成功。
我需要改变什么才能使 IV 长度与需要的相匹配?
namespace Encryption
{
#region Using Statements
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
#endregion
public class EncryptionHelper
{
#region Private Fields
// This constant determines the number of iterations for the password bytes generation function.
private const int DerivationIterations = 1000;
// This constant is used to determine the keysize of the encryption algorithm in bits.
// We divide this by 8 …Run Code Online (Sandbox Code Playgroud) c# ×8
cryptography ×3
encryption ×2
sql-server ×2
.net ×1
aes ×1
asp.net ×1
base64 ×1
mysql ×1
php ×1
rsa ×1
winforms ×1