Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MySQL中使用波兰语字符的AES_DECRYPT()和AES_ENCRYPT()_C#_Mysql_Encryption_Aes - Fatal编程技术网

C# MySQL中使用波兰语字符的AES_DECRYPT()和AES_ENCRYPT()

C# MySQL中使用波兰语字符的AES_DECRYPT()和AES_ENCRYPT(),c#,mysql,encryption,aes,C#,Mysql,Encryption,Aes,我有一个MySQL数据库,它被配置为接收带有波兰语字符(f.exą、ę、ó、ł等)的数据 现在,我想使用AES\u ENCRYPT()将带有这些波兰字符的数据发送到数据库,然后使用AES\u DECRYPT()从那里获取它们。 我的问题是,我在C#中接收到一个byte[]数组,该数组有X个元素,其中X是我接收到的文本的长度。每个数组元素都有一个ASCII码,它代表一个字符。我可以使用Encoding类轻松地将其转换为文本,但我不会在输出文本中获得波兰语字符 F.例如: 我向db发送AES加密('

我有一个MySQL数据库,它被配置为接收带有波兰语字符(f.exą、ę、ó、ł等)的数据

现在,我想使用
AES\u ENCRYPT()
将带有这些波兰字符的数据发送到数据库,然后使用
AES\u DECRYPT()
从那里获取它们。 我的问题是,我在C#中接收到一个byte[]数组,该数组有X个元素,其中X是我接收到的文本的长度。每个数组元素都有一个ASCII码,它代表一个字符。我可以使用Encoding类轻松地将其转换为文本,但我不会在输出文本中获得波兰语字符

F.例如:

我向db发送AES加密('123')。 我得到了AES_DECRYPT('sql命令','123'),我得到了
byte[]
,它有3个元素,每个元素都有'97'值,表示
'aaa'
-而不是
'。
如何使用AES_解密/加密
,使我能够向数据库发送/获取波兰语字符?!
或者如何从aes_decrypt()而不是字节[]获取字符串输出?

为什么不在代码中而不是在查询中实现加密/解密

private static Byte[] Encrypt(String toEncrypt, Byte[] Key, Byte[] IV)
{
    CryptoStream streamCrypto = null;
    MemoryStream streamMemory = null;
    RijndaelManaged aes = null;
    StreamWriter streamWriter = null;

    try
    {
        aes = new RijndaelManaged();
        aes.Key = Key;
        aes.IV = IV;

        ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

        streamMemory = new MemoryStream();
        streamCrypto = new CryptoStream(streamMemory, encryptor, CryptoStreamMode.Write);
        streamWriter = new StreamWriter(streamCrypto);

        streamWriter.Write(toEncrypt);

    }
    finally
    {
        if (streamWriter != null)
            streamWriter.Close();

        if (streamCrypto != null)
            streamCrypto.Close();

        if (streamMemory != null)
            streamMemory.Close();

        if (aes != null)
            aes.Clear();
    }

    return streamMemory.ToArray();
}

public static String Decrypt(Byte[] toDecrypt, Byte[] Key, Byte[] IV)
{
    CryptoStream streamCrypto = null;
    MemoryStream streamMemory = null;
    RijndaelManaged aes = null;
    StreamReader streamReader = null;
    String output = null;

    try
    {
        aes = new RijndaelManaged();
        aes.Key = Key;
        aes.IV = IV;

        ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

        streamMemory = new MemoryStream(toDecrypt);
        streamCrypto = new CryptoStream(streamMemory, decryptor, CryptoStreamMode.Read);
        streamReader = new StreamReader(streamCrypto);

        output = streamReader.ReadToEnd();
    }
    finally
    {
        if (streamReader != null)
            streamReader.Close();

        if (streamCrypto != null)
            streamCrypto.Close();

        if (streamMemory != null)
            streamMemory.Close();

        if (aes != null)
            aes.Clear();
    }

    return output;
}
在代码中,加密字符串,然后将加密数据发送到数据库:

Byte[] encrypted = Encrypt(yourString, Key, IV);  
当您从数据库中取出数据时,您只需使用以下命令取回字符串:

String decrypted = Decrypt(dbData, Key, IV);
如果您不喜欢这种方式,请使用如下查询:

INSERT INTO mysecrets (mysecret1, mysecret2) VALUES (AES_ENCRYPT(secret1, YOUR_ENCRYPTION_KEY), AES_ENCRYPT(secret2, YOUR_ENCRYPTION_KEY))

SELECT AES_DECRYPT(mysecret1, YOUR_ENCRYPTION_KEY) AS secret1, AES_DECRYPT(mysecret1, YOUR_ENCRYPTION_KEY) AS secret2 FROM mysecrets

为什么不在代码中而不是在查询中实现加密/解密呢

private static Byte[] Encrypt(String toEncrypt, Byte[] Key, Byte[] IV)
{
    CryptoStream streamCrypto = null;
    MemoryStream streamMemory = null;
    RijndaelManaged aes = null;
    StreamWriter streamWriter = null;

    try
    {
        aes = new RijndaelManaged();
        aes.Key = Key;
        aes.IV = IV;

        ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

        streamMemory = new MemoryStream();
        streamCrypto = new CryptoStream(streamMemory, encryptor, CryptoStreamMode.Write);
        streamWriter = new StreamWriter(streamCrypto);

        streamWriter.Write(toEncrypt);

    }
    finally
    {
        if (streamWriter != null)
            streamWriter.Close();

        if (streamCrypto != null)
            streamCrypto.Close();

        if (streamMemory != null)
            streamMemory.Close();

        if (aes != null)
            aes.Clear();
    }

    return streamMemory.ToArray();
}

public static String Decrypt(Byte[] toDecrypt, Byte[] Key, Byte[] IV)
{
    CryptoStream streamCrypto = null;
    MemoryStream streamMemory = null;
    RijndaelManaged aes = null;
    StreamReader streamReader = null;
    String output = null;

    try
    {
        aes = new RijndaelManaged();
        aes.Key = Key;
        aes.IV = IV;

        ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

        streamMemory = new MemoryStream(toDecrypt);
        streamCrypto = new CryptoStream(streamMemory, decryptor, CryptoStreamMode.Read);
        streamReader = new StreamReader(streamCrypto);

        output = streamReader.ReadToEnd();
    }
    finally
    {
        if (streamReader != null)
            streamReader.Close();

        if (streamCrypto != null)
            streamCrypto.Close();

        if (streamMemory != null)
            streamMemory.Close();

        if (aes != null)
            aes.Clear();
    }

    return output;
}
在代码中,加密字符串,然后将加密数据发送到数据库:

Byte[] encrypted = Encrypt(yourString, Key, IV);  
当您从数据库中取出数据时,您只需使用以下命令取回字符串:

String decrypted = Decrypt(dbData, Key, IV);
如果您不喜欢这种方式,请使用如下查询:

INSERT INTO mysecrets (mysecret1, mysecret2) VALUES (AES_ENCRYPT(secret1, YOUR_ENCRYPTION_KEY), AES_ENCRYPT(secret2, YOUR_ENCRYPTION_KEY))

SELECT AES_DECRYPT(mysecret1, YOUR_ENCRYPTION_KEY) AS secret1, AES_DECRYPT(mysecret1, YOUR_ENCRYPTION_KEY) AS secret2 FROM mysecrets

使用编码转换可能会对您有所帮助

select convert(aes_decrypt(aes_encrypt('ąąą', 'abcdefg'), 'abcdefg') using UTF8);

使用编码转换可能会对您有所帮助

select convert(aes_decrypt(aes_encrypt('ąąą', 'abcdefg'), 'abcdefg') using UTF8);


MySQL数据以字符为单位,而加密则以字节为单位。在加密字符之前,需要将字符转换为字节,还需要将解密后的字节转换回字符。这意味着您需要显式指定两端使用的字符编码,以便它们匹配。当前的标准是UTF-8,因此您应该在每一端指定它。如果UTF-8不起作用,那么在两端尝试一些Microsoft特定的字符编码。

您的MySQL数据是以字符为单位的,而加密是以字节为单位的。在加密字符之前,需要将字符转换为字节,还需要将解密后的字节转换回字符。这意味着您需要显式指定两端使用的字符编码,以便它们匹配。当前的标准是UTF-8,因此您应该在每一端指定它。如果UTF-8不起作用,那么在两端尝试一些Microsoft特定的字符编码。

这需要做很多工作。首先,我想尝试使用mysql函数来实现它,这需要做很多工作。首先,我想尝试使用mysql函数来实现它。嗯,当我在mysql工作台中实现它时,它就可以工作了!它有波兰字!但是当我在我的C代码中使用
ExecuteScalar()
时,我仍然得到没有波兰语字符的字符串。有什么想法吗?如何在C#中转换它?我认为您需要在C#encoding中找到字符串编码方法。convert调用ExecuteScalar()时的结果是什么。结果是字符串,但没有波兰语字符。这很奇怪,因为MySQL Wrokbench(同一个命令)返回的字符串带有波兰语字符!你有没有在加密之前尝试过编码
aesąu encrypt(使用UTF8转换,'abcdefg')
Hmm,当我在MySQL WorkBench中这样做时,它可以工作!它有波兰字!但是当我在我的C代码中使用
ExecuteScalar()
时,我仍然得到没有波兰语字符的字符串。有什么想法吗?如何在C#中转换它?我认为您需要在C#encoding中找到字符串编码方法。convert调用ExecuteScalar()时的结果是什么。结果是字符串,但没有波兰语字符。这很奇怪,因为MySQL Wrokbench(同一个命令)返回的字符串带有波兰语字符!您在加密之前是否尝试过编码
aesąu encrypt(使用UTF8转换,'abcdefg')
Ok,现在我试试您的想法。我已使用
byte[]bytes=Encoding.UTF8.GetBytes(string)将字符串转换为byte[]byte[]bytes=Encoding.UTF8.GetBytes(string)将字符串转换为byte[]