加密和解密的图像.NET [英] Encrypt and Decrypt Image .net

查看:131
本文介绍了加密和解密的图像.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个加密的例子,使用带有asp.net .NET解密图片

Can anyone give me an example for encrypt and decrypt an image using .net with asp.net

我想这个加密的形象,当我将它保存到SQL Server为二进制数据。

I want this encryption to the image when I save it into sql server as binary data.

推荐答案

最后我找到了这个问题的解决方案。
我将添加code帮助谁需要这一点。

Finally I found the solution for this problem. I will add the code for helping who need that.

加密方式:

    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

这篇关于加密和解密的图像.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆