AES字符串加密/解密字符间隔开 [英] AES string encryption/decryption characters are spaced out

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

问题描述

我正在加密,然后使用AES解密字符串。但我的输出如下所示:

I'm encrypting and then decrypting a string using AES. But my output looks like this:

Original text >> HI WORLD  
Decrypted text >> H I   W O R L D

我尝试了很多代码,但我没有发现问题。

I have tried a lot of code but I have not found the problem.

这里的问题在哪里?

class Program
{
    public static void Main(string[] args)
    {
        byte[] aesKey = Cryptography.GenerateAes128Key();
        Console.WriteLine("AES key >> " + aesKey.Length);
        string originalText = "HI WORLD";
        byte[] myMess = ASCIIEncoding.Unicode.GetBytes(originalText);
        Console.WriteLine("Original text >> " + ASCIIEncoding.Unicode.GetString(myMess));
        byte[] myEcnryptedMess = Cryptography.Encrypt(myMess, aesKey);
        Console.WriteLine("Encrypted text >> " + ASCIIEncoding.Unicode.GetString(myEcnryptedMess));
        Console.WriteLine("Decrypted text >> " + Cryptography.Decrypt(myEcnryptedMess, aesKey));
        Console.WriteLine("Press any key to continue . . . ");
        Console.ReadKey(true);
    }

    public static byte[] Encrypt(byte[] plainTextBytes, byte[] Key)
    {
        byte[] iv = new byte[Key.Length];
        Aes myAes = Aes.Create();
        ICryptoTransform encryptor = myAes.CreateEncryptor(Key, iv);
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
        cryptoStream.FlushFinalBlock();
        byte[] cipherTextBytes = memoryStream.ToArray();
        // Close both streams.
        memoryStream.Close();
        cryptoStream.Close();
        return cipherTextBytes;
    }

    public static string Decrypt(byte[] cipherTextBytes, byte[] Key)
    {
        byte[] iv = new byte[Key.Length];
        Aes myAes = Aes.Create();
        ICryptoTransform decryptor = myAes.CreateDecryptor(Key, iv);
        MemoryStream  memoryStream = new MemoryStream(cipherTextBytes);
        CryptoStream  cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
        byte[] plainTextBytes = new byte[cipherTextBytes.Length];
        // Start decrypting.
        int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
        // Close both streams.
        memoryStream.Close();
        cryptoStream.Close();
        // Convert decrypted data into a string.
        // Let us assume that the original plaintext string was UTF8-encoded.
        string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        // Return decrypted string.
        return plainText;
    }
}


推荐答案

正在使用不同的Encoding to GetBytes和GetString:

You are using different Encoding to GetBytes and GetString:

ASCIIEncoding.Unicode.GetBytes(originalText);

,然后

Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)

尝试使用一个用于两个操作。

Try to use same one for both operations.

Kris

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

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