AES 字符串加密/解密字符间隔

发布于 2024-12-08 14:00:09 字数 2626 浏览 0 评论 0原文

我正在使用 AES 加密然后解密字符串。但我的输出看起来是这样的:

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

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

这里的问题出在哪里呢?

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;
    }
}

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.

Where is the problem here?

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花浅忆 2024-12-15 14:00:09

您对 GetBytes 和 GetString: 使用不同的编码,

ASCIIEncoding.Unicode.GetBytes(originalText);

然后

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

尝试对这两项操作使用相同的编码。

克里斯

You are using different Encoding to GetBytes and GetString:

ASCIIEncoding.Unicode.GetBytes(originalText);

and then

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

Try to use same one for both operations.

Kris

天涯沦落人 2024-12-15 14:00:09

你的字符串编码弄乱了。 ASCIIEncoding.Unicode 基本上是无稽之谈。 (实际上,它与 Encoding.Unicode 相同,但编写方式具有误导性。)

您想使用哪种编码? UTF-16、UTF-8 还是 ASCII?根据选择,使用 Encoding.UnicodeEncoding.UTF8Encoding.ASCII,并坚持使用。我认为 UTF-8 是最好的选择,因此,使用这个:

// ...
string originalText = "HI WORLD";
byte[] myMess = Encoding.UTF8.GetBytes(originalText);
Console.WriteLine("Original text >> " + Encoding.UTF8.GetString(myMess));
byte[] myEcnryptedMess = Cryptography.Encrypt(myMess, aesKey);
Console.WriteLine("Encrypted text >> " + Encoding.UTF8.GetString(myEcnryptedMess));
Console.WriteLine("Decrypted text >> " + Cryptography.Decrypt(myEcnryptedMess, aesKey));

You are making a mess with your string encodings. ASCIIEncoding.Unicode is, basically, nonsense. (Actually, it is just the same as Encoding.Unicode, but written in a misleading way.)

Which encoding do you want to use? UTF-16, UTF-8, or ASCII? According to the choice, use either Encoding.Unicode, Encoding.UTF8, or Encoding.ASCII, and stick to it. I would presume UTF-8 is the best choice, therefore, use this:

// ...
string originalText = "HI WORLD";
byte[] myMess = Encoding.UTF8.GetBytes(originalText);
Console.WriteLine("Original text >> " + Encoding.UTF8.GetString(myMess));
byte[] myEcnryptedMess = Cryptography.Encrypt(myMess, aesKey);
Console.WriteLine("Encrypted text >> " + Encoding.UTF8.GetString(myEcnryptedMess));
Console.WriteLine("Decrypted text >> " + Cryptography.Decrypt(myEcnryptedMess, aesKey));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文