Зашифруйте и расшифруйте изображение .net - PullRequest
0 голосов
/ 29 сентября 2010

Может ли кто-нибудь дать мне пример шифрования и дешифрования изображения с использованием .net с asp.net

Я хочу, чтобы это шифрование для изображения сохранялось на сервере sql в виде двоичных данных.

Ответы [ 2 ]

5 голосов
/ 29 сентября 2010

Включить эти пространства имен

using System.IO;
using System.Security.Cryptography;

Для шифрования создайте ниже функцию:

private void EncryptFile(string inputFile, string outputFile)
{

    try
    {
        string password = @"myKey123"; // Your Key Here
        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        string cryptFile = outputFile;
        FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

        RijndaelManaged RMCrypto = new RijndaelManaged();

        CryptoStream cs = new CryptoStream(fsCrypt,
            RMCrypto.CreateEncryptor(key, key),
            CryptoStreamMode.Write);

        FileStream fsIn = new FileStream(inputFile, FileMode.Open);

        int data;
        while ((data = fsIn.ReadByte()) != -1)
            cs.WriteByte((byte)data);


        fsIn.Close();
        cs.Close();
        fsCrypt.Close();
    }
    catch
    {
        MessageBox.Show("Encryption failed!", "Error");
    }
}

Для расшифровки создайте нижеприведенную функцию:

private void DecryptFile(string inputFile, string outputFile)
{

    {
        string password = @"myKey123"; // Your Key Here

        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

        RijndaelManaged RMCrypto = new RijndaelManaged();

        CryptoStream cs = new CryptoStream(fsCrypt,
            RMCrypto.CreateDecryptor(key, key),
            CryptoStreamMode.Read);

        FileStream fsOut = new FileStream(outputFile, FileMode.Create);

        int data;
        while ((data = cs.ReadByte()) != -1)
            fsOut.WriteByte((byte)data);

        fsOut.Close();
        cs.Close();
        fsCrypt.Close();

    }
}

Вы можете позвонить, как это

   EncryptFile(@"D:\OriginalImage.png", @"D:\VizioEncrypted.png"); //To Encrypt

   DecryptFile(@"D:\VizioEncrypted.png", @"D:\VizioDecrypted.png"); //To Decrypt

Это поможет

2 голосов
/ 29 сентября 2010

Наконец-то я нашел решение этой проблемы.Я добавлю код для помощи кому это нужно.

Метод шифрования:

    Public Function EncryptStream(ByVal input As Byte()) As Byte()
    Dim rijn As New RijndaelManaged()
    Dim encrypted As Byte()
    Dim key As Byte() = New Byte() {&H22, &HC0, &H6D, &HCB, &H23, &HA6, _
     &H3, &H1B, &H5A, &H1D, &HD3, &H9F, _
     &H85, &HD, &HC1, &H72, &HED, &HF4, _
     &H54, &HE6, &HBA, &H65, &HC, &H22, _
     &H62, &HBE, &HF3, &HEC, &H14, &H81, _
     &HA8, &HA}
    '32
    Dim IV As Byte() = New Byte() {&H43, &HB1, &H93, &HB, &H1A, &H87, _
     &H52, &H62, &HFB, &H8, &HD, &HC0, _
     &HCA, &H40, &HC2, &HDB}
    '16
    'Get an encryptor.
    Dim encryptor As ICryptoTransform = rijn.CreateEncryptor(key, IV)

    'Encrypt the data.
    Dim msEncrypt As New MemoryStream()
    Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)


    'Write all data to the crypto stream and flush it.
    csEncrypt.Write(input, 0, input.Length)
    csEncrypt.FlushFinalBlock()

    'Get encrypted array of bytes.
    encrypted = msEncrypt.ToArray()

    Return encrypted

End Function

Метод расшифровки:

    Public Function DecryptStream(ByVal input As Byte()) As Byte()
    Dim rijn As New RijndaelManaged()
    Dim decrypted As Byte()
    Dim key As Byte() = New Byte() {&H22, &HC0, &H6D, &HCB, &H23, &HA6, _
     &H3, &H1B, &H5A, &H1D, &HD3, &H9F, _
     &H85, &HD, &HC1, &H72, &HED, &HF4, _
     &H54, &HE6, &HBA, &H65, &HC, &H22, _
     &H62, &HBE, &HF3, &HEC, &H14, &H81, _
     &HA8, &HA}
    '32
    Dim IV As Byte() = New Byte() {&H43, &HB1, &H93, &HB, &H1A, &H87, _
     &H52, &H62, &HFB, &H8, &HD, &HC0, _
     &HCA, &H40, &HC2, &HDB}
    '16 


    'Get a decryptor that uses the same key and IV as the encryptor.
    Dim decryptor As ICryptoTransform = rijn.CreateDecryptor(key, IV)

    'Now decrypt the previously encrypted message using the decryptor
    ' obtained in the above step.
    Dim msDecrypt As New MemoryStream(input)
    Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

    decrypted = New Byte(input.Length - 1) {}

    'Read the data out of the crypto stream.
    csDecrypt.Read(decrypted, 0, decrypted.Length)

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