加密图像文件简单的加密/解密方法 [英] Simple Encryption/Decryption method for encrypting an image file

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

问题描述

我的要求是,我需要简单的加密/解密方法在C#加密和
解密的图像(也许GIF / JPEG)。简单,因为我必须将它存储在数据库中的BLOB字段andsome其他开发商在其他一些编程语言(如Java)可能需要提取和显示此image.I并不需要太多的安全性的模糊安全导致其只是一个物质(的生命)。

My requirement is that I need simple encryption/decryption methods in C# to encrypt and decrypt an image (maybe gif/jpeg).Simple cause I have to store it in the database in a BLOB field andsome other developers in some other programming language(like java) may need to extract and display this image.I don't need much security cause its just a matter of "security by obscuring"(life).

Gulp..can有人帮...

Gulp..can someone help...

推荐答案

既然你并不需要太多的安全你也许可以管理与像 AES(Rijndael算法)度日。它采用了对称密钥并有大量的在.NET框架,使得帮助是很容易实现。有大量的信息在 MSDN上的Rijndael类,你可能会发现有用的。

Since you "don't need much security" you can probably manage to get by with something like AES (Rijndael). It uses a symmetric-key and there is plenty of help in the .NET framework to make is easy to implement. There is plenty of info in MSDN on the Rijndael class that you might find helpful.

下面是加密的一个非常精简的例子/解密,可用于使用字节数组(二进制内容)...

Here is a very stripped down example of encrypt / decrypt methods which can be used for working with byte arrays (binary contents)...

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

public class RijndaelHelper
{
    // Example usage: EncryptBytes(someFileBytes, "SensitivePhrase", "SodiumChloride");
    public static byte[] EncryptBytes(byte[] inputBytes, string passPhrase, string saltValue)
    {
        RijndaelManaged RijndaelCipher = new RijndaelManaged();

        RijndaelCipher.Mode = CipherMode.CBC;
        byte[] salt = Encoding.ASCII.GetBytes(saltValue);
        PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);

        ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(password.GetBytes(32), password.GetBytes(16));

        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
        cryptoStream.Write(inputBytes, 0, inputBytes.Length);
        cryptoStream.FlushFinalBlock();
        byte[] CipherBytes = memoryStream.ToArray();

        memoryStream.Close();
        cryptoStream.Close();

        return CipherBytes;
    }

    // Example usage: DecryptBytes(encryptedBytes, "SensitivePhrase", "SodiumChloride");
    public static byte[] DecryptBytes(byte[] encryptedBytes, string passPhrase, string saltValue)
    {
        RijndaelManaged RijndaelCipher = new RijndaelManaged();

        RijndaelCipher.Mode = CipherMode.CBC;
        byte[] salt = Encoding.ASCII.GetBytes(saltValue);
        PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);

        ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(password.GetBytes(32), password.GetBytes(16));

        MemoryStream memoryStream = new MemoryStream(encryptedBytes);
        CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
        byte[] plainBytes = new byte[encryptedBytes.Length];

        int DecryptedCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length);

        memoryStream.Close();
        cryptoStream.Close();

        return plainBytes;
    }
}

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

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