Как я могу зашифровать с помощью Postgres и расшифровать с помощью C #? - PullRequest
1 голос
/ 07 апреля 2019

Я зашифровываю пароль в postgres и хочу расшифровать его в c #, но два способа не совпадают. Как я могу это сделать?

private static byte[] TruncateHash(string key, int length)
{
    SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
    // Hash the key.
    byte[] keyBytes = System.Text.Encoding.Unicode.GetBytes(key);
    byte[] hash = sha1.ComputeHash(keyBytes);

    // Truncate or pad the hash.
    Array.Resize(ref hash, length);
    return hash;
}

public static string EncryptString(string plaintext, string Passphrase)
{
    TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider();
    // Initialize the crypto provider.
    tripleDes.Key = TruncateHash(Passphrase, tripleDes.KeySize / 8);
    tripleDes.IV = TruncateHash("", tripleDes.BlockSize / 8);

    // Convert the plaintext string to a byte array.
    byte[] plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plaintext);

    // Create the stream.
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    // Create the encoder to write to the stream.
    CryptoStream encStream = new CryptoStream(ms, tripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

    // Use the crypto stream to write the byte array to the stream.
    encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
    encStream.FlushFinalBlock();

    // Convert the encrypted stream to a printable string.
    return Convert.ToBase64String(ms.ToArray());
}

public static string DecryptString(string encryptedtext, string Passphrase)
{
    TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider();
    // Initialize the crypto provider.
    tripleDes.Key = TruncateHash(Passphrase, tripleDes.KeySize / 8);
    tripleDes.IV = TruncateHash("", tripleDes.BlockSize / 8);
    // Convert the encrypted text string to a byte array.
    byte[] encryptedBytes = Convert.FromBase64String(encryptedtext);

    // Create the stream.
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    // Create the decoder to write to the stream.
    CryptoStream decStream = new CryptoStream(ms, tripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

    // Use the crypto stream to write the byte array to the stream.
    decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
    decStream.FlushFinalBlock();

    // Convert the plaintext stream to a string.
    return System.Text.Encoding.Unicode.GetString(ms.ToArray());
}

Я нашел способ шифрования в postgres с помощью pgcrypto.А ниже - шифрование и дешифрование в postgres.

SELECT encode(encrypt_iv('ABCDE121212','Key123', '','3des'), 'base64');
select decrypt_iv(decode('jEI4V5q6h5/p12NRJm666g==','base64'),'Key123','','3des')

Что не так в моем коде, c # и postgres не могут не совпадать.Я хочу сохранить код c # и изменить код postgres на соответствующий

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...