使用SharpAESCrypt加密字符串 [英] Using SharpAESCrypt to encrypt strings

查看:229
本文介绍了使用SharpAESCrypt加密字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定用SharpAESCrypt执行AES加密在C#。根据他们的文档( https://www.aescrypt.com/sharp_aes_crypt.html ),你应该能够使用一个静态方法,提供了一个口令字符串,明文输入流和输出流。我得到了我的输出流中的数据似乎是全零。

I decided to use the SharpAESCrypt implementation of AES encryption in C#. According to their documentation (https://www.aescrypt.com/sharp_aes_crypt.html) you should be able to use a static method, providing a password string, plain-text input stream and a output stream. The data I get out of my output stream appears to be all zero's.

我怀疑我做错了什么字符串转换到一个流和背部。任何人都可以看到任何明显错误与下面的代码? (它编译和运行,但newByteArray是在结满了0)

I suspect I am doing something wrong converting strings to a stream and back. Can anyone see anything that is obviously wrong with the code below? (It compiles and runs, but the newByteArray is filled with 0's at the end)

    private void encryptMemStream()
    {
        byte[] byteArray = Encoding.UTF8.GetBytes(DecryptedTB.Text);
        using (MemoryStream plainText = new MemoryStream(byteArray))
        {
            using (MemoryStream encryptedData = new MemoryStream())
            {
                //plainText.Write(byteArray, 0, byteArray.Length);
                SharpAESCrypt.Encrypt(PasswordTB.Text, plainText, encryptedData);
                encryptedData.Position = 0;
                byte[] newByteArray = new byte[encryptedData.Length];
                newByteArray = encryptedData.ToArray();
                EncryptedTB.Text = Encoding.UTF8.GetString(newByteArray);
            }
        }
    }



编辑:本机实现的该代码使用文件流,但它应该与MemoryStreams工作了。我将测试文件流并在这里添加我的结果。

The native implementation of this code uses fileStreams, but it should work with MemoryStreams too. I will test filestreams and add my results here.

更多编辑:

所以,你把这个代码

    //Code from the AES Crypt Library
    public static void Encrypt(string password, string inputfile, string outputfile)
    {
        using (FileStream infs = File.OpenRead(inputfile))
        using (FileStream outfs = File.Create(outputfile))
            Encrypt(password, infs, outfs);
    }



其中要求,我一直在直接调用函数

Which calls a function which I have been calling directly

    //Code from the AES Crypt Library
    public static void Encrypt(string password, Stream input, Stream output)
    {
        int a;
        byte[] buffer = new byte[1024 * 4];
        SharpAESCrypt c = new SharpAESCrypt(password, output, OperationMode.Encrypt);
        while ((a = input.Read(buffer, 0, buffer.Length)) != 0)
            c.Write(buffer, 0, a);
        c.FlushFinalBlock();
    }

有明显使用一个MemoryStream一些微妙的差异和FileStream我不要不懂。的FileStream正常工作,那里的MemoryStream的返回一个空数组...

There is obviously some subtle difference in using a MemoryStream and a FileStream which I don't understand. FileStream works fine, where as MemoryStream returns a blank array...

推荐答案

一个快速的代码审查发现了一些东西,包括什么,从讨论中,似乎是你所面对的主要问题。有问题的行

A quick code review reveals a few things including what, from discussion, seems to be the main problem you are facing. The problematic line is

EncryptedTB.Text = Encoding.UTF8.GetString(newByteArray);

这行是newByteArray走的是二进制数据,并告诉你的代码,这是一个有效的UTF8字节数组。机会是,它几乎肯定不会实际上将是有效的UTF8。它的某些部分可能(如标题),但是实际的加密数据不会是,所以你不应该使用这个方法来获取一个字符串。

This line is taking the binary data in newByteArray and telling your code that it is a valid UTF8 byte array. Chances are that it is almost certainly not actually going to be valid UTF8. Parts of it may be (such as the header) but the actually encrypted data is not going to be and so you shouldn't use this method to get a string.

如果需要打印字符串摆在某种形式的文本框,然后为二进制数据做正确的方法是通过base64编码。

If you need a printable string to put in a textbox of some sort then the correct way to do this for binary data is through base64 encoding.

EncryptedTB.Text = Convert.ToBase64String(newByteArray);



Base64编码有效需要3个字节(24比特)组,并将其转换为四个字符组。

Base64 encoding effectively takes groups of three bytes (24 bits) and converts them to groups of four characters.

进一步的说明,而我在这里的是,在这两行:

A further note while I'm here is that in these two lines:

byte[] newByteArray = new byte[encryptedData.Length];
newByteArray = encryptedData.ToArray();

您声明一个字节数组,并分配一个新的数组给它。然后,立即赞成阵列从 encryptedData.ToArray放弃这个()。这不把数据转换为新的字节[encryptedData.Length] 而是创建一个新的数组,并把它放入 newByteArray 变量。更好的是只做到:

You declare a byte array and allocate a new array to it. You then immediately discard this in favour of the array from encryptedData.ToArray(). This does not put data into the array allocated by new byte[encryptedData.Length] but creates a new array and puts it into the newByteArray variable. Better would be to just do:

byte[] newByteArray = encryptedData.ToArray();



以下完整的工作代码:

Full working code below:

byte[] byteArray = Encoding.UTF8.GetBytes(sourceText);
Byte[] newByteArray;
using (MemoryStream plainText = new MemoryStream(byteArray))
{
    using (MemoryStream encryptedData = new MemoryStream())
    {
        SharpAESCrypt.Encrypt(password, plainText, encryptedData);
        newByteArray = encryptedData.ToArray();
    }
}
EncryptedTB.Text = Convert.ToBase64String(newByteArray);

这篇关于使用SharpAESCrypt加密字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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