2

ポーランド文字 (f. ex ą、ę、ó、ł、ń など) を含むデータを受け取るように構成された MySQL データベースがあります。

これらのポーランド文字を含むデータを を使用してデータベースに送信しAES_ENCRYPT()、そこから を使用して取得したいと考えていますAES_DECRYPT()。私の問題は、X が受信したテキストの長さである X 要素を持つ C# の byte[] 配列を受信することです。また、すべての配列要素には、それが表す文字の ASCII コードがあります。Encoding Class を使用して簡単にテキストに変換できましたが、出力テキストにポーランド語の文字が含まれません。

F. 例:

AES_ENCRYPT('ąąą', '123')私はdbに送信します。私は取得し、どれが 3 つの要素を持っているAES_DECRYPT('sql command','123')かを取得します。洗練された文字を DB に送信/取得できる方法で使用する方法は?! または、byte[] の代わりに aes_decrypt() から文字列出力を取得する方法は?byte[]'aaa''ąąą'AES_DECRYPT/ENCRYPT

4

3 に答える 3

2

エンコーディングを使用して変換すると役立つ場合があります。

select convert(aes_decrypt(aes_encrypt('ąąą', 'abcdefg'), 'abcdefg') using UTF8);
于 2013-01-19T13:39:58.090 に答える
1

MySQLデータは文字単位ですが、暗号化はバイトで機能します。暗号化する前に文字をバイトに変換する必要があります。また、復号化されたバイトを文字に戻す必要があります。つまり、両端で使用する文字エンコードを明示的に指定して、それらが一致するようにする必要があります。現在の標準はUTF-8であるため、両端で指定する必要があります。UTF-8が機能しない場合は、両端でMicrosoft固有の文字エンコードを試してください。

于 2013-01-19T14:18:48.807 に答える
1

クエリではなくコードで暗号化/復号化を実装してみませんか?

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
于 2013-01-19T13:34:12.750 に答える