一个使用C#编写的加密算法程序存在问题。

huangapple go评论105阅读模式
英文:

A problem with an encryption algorithm program written in C#

问题

我写了一个在.NET 7中的程序,它加密一个字符串并返回包含第一个字符串加密形式的另一个字符串。问题是这个程序没有返回任何东西,只是一个空字符串。

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

internal class Program
{
    private static void Main(string[] args)
    {
        string unencrypted = Console.ReadLine();
        byte[] transformed = Encoding.UTF8.GetBytes(unencrypted);
        byte[] encrypted;
        string cipher;

        Aes aes = Aes.Create();
        MemoryStream memoryStream = new MemoryStream();
        ICryptoTransform cryptoTransform = aes.CreateEncryptor(aes.Key, aes.IV);

        CryptoStream cstream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write);
        StreamWriter streamW = new StreamWriter(cstream);
        streamW.Write(transformed);
        streamW.Flush();
        cstream.FlushFinalBlock();

        encrypted = memoryStream.ToArray();
        cipher = Convert.ToBase64String(encrypted);
        Console.WriteLine(cipher);
    }
}

我希望这个程序现在能够返回包含第一个字符串加密形式的字符串。如果需要进一步的帮助,请告诉我。

英文:

I wrote a program in .NET 7 that encrypts a string and returns another string that contains the encrypted form of the first string. The problem is the program does not return anything, only a null string

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

internal class Program
{
    private static void Main(string[] args)
    {
        string unencrypted = Console.ReadLine();
        byte[] transformed = Encoding.UTF8.GetBytes(necriptat);
        byte[] encrypted;
        string cipher;

        Aes aes = Aes.Create();
        MemoryStream memoryStream = new MemoryStream();
        ICryptoTransform cryptoTransform = aes.CreateEncryptor(aes.Key, aes.IV);

        CryptoStream cstream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write);
        StreamWriter streamW = new StreamWriter(cstream);
        streamW.Write(transformed);

        encrypted = memoryStream.ToArray();
        cipher = Encoding.UTF8.GetString(encrypted);
        Console.WriteLine(cipher);
    }
}

I want from this program to return a string containing the encrypted form of the first string. I would be thankful if you can help me to fix my problem

答案1

得分: 0

你错过了同步缓冲区:

streamW.Write(transformed);
cstream.FlushFinalBlock(); <-- 这里
encrypted = memoryStream.ToArray();
英文:

You missed synchronizing the buffer:

streamW.Write(transformed);
cstream.FlushFinalBlock(); <-- Here
encrypted = memoryStream.ToArray();

答案2

得分: 0

尝试像这样开始:

    // 非常不安全!仅供演示目的使用。不要用于实际场景!
    using System.Security.Cryptography;
    using System.Text;

    string originalPlainText = Console.ReadLine() ?? string.Empty;

    Aes aes = Aes.Create();
    ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

    string cipherString;
    using (MemoryStream cipherStream = new MemoryStream())
    {
        using (CryptoStream encryptingStream = new CryptoStream(cipherStream, encryptor, CryptoStreamMode.Write))
        {
            using MemoryStream plainStream = new MemoryStream(Encoding.UTF8.GetBytes(originalPlainText));
            plainStream.CopyTo(encryptingStream);
        }
        // 必须在访问输出流之前关闭或调用 FlushFinalBlock() 的 CryptoStream 流

        byte[] cipherBytes = cipherStream.ToArray();
        // 加密数据通常无法表示为 UTF8
        cipherString = Convert.ToBase64String(cipherBytes);
    }
    Console.WriteLine(cipherString);

    // IV 应该被编码并与加密数据一起传输,而不是保持不变或分开,
    // 它必须是独特且不可预测的。
    ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
    using (MemoryStream plainStream = new MemoryStream())
    {
        using (CryptoStream cryptoStream = new CryptoStream(plainStream, decryptor, CryptoStreamMode.Write))
        {
            using MemoryStream cipherStream = new MemoryStream(Convert.FromBase64String(cipherString));
            cipherStream.CopyTo(cryptoStream);
        }
    
        string plainString = Encoding.UTF8.GetString(plainStream.ToArray());
        Console.WriteLine(plainString);
    }

以正确和安全的方式完成这项工作远非看上去那么简单。对于一些真正的生产代码,请查看 https://github.com/xecrets/xecrets-file-cli 和 https://github.com/axantum/xecrets-net 。
英文:

Try something like this to get started:

// Very insecure! Only for demonstration purposes. Do not use for anything real!
using System.Security.Cryptography;
using System.Text;

string originalPlainText = Console.ReadLine() ?? string.Empty;

Aes aes = Aes.Create();
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

string cipherString;
using (MemoryStream cipherStream = new MemoryStream())
{
    using (CryptoStream encryptingStream = new CryptoStream(cipherStream, encryptor, CryptoStreamMode.Write))
    {
        using MemoryStream plainStream = new MemoryStream(Encoding.UTF8.GetBytes(originalPlainText));
        plainStream.CopyTo(encryptingStream);
    }
    // A CryptoStream stream must be closed or FlushFinalBlock() called before accessing the output stream

    byte[] cipherBytes = cipherStream.ToArray();
    // Encrypted data can typically not be represented as UTF8
    cipherString = Convert.ToBase64String(cipherBytes);
}
Console.WriteLine(cipherString);

// The IV should be encoded and carried with the encrypted data, not kept constant or separately,
// it must be unique and non-predictable.
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream plainStream = new MemoryStream())
{
    using (CryptoStream cryptoStream = new CryptoStream(plainStream, decryptor, CryptoStreamMode.Write))
    {
        using MemoryStream cipherStream = new MemoryStream(Convert.FromBase64String(cipherString));
        cipherStream.CopyTo(cryptoStream);
    }

    string plainString = Encoding.UTF8.GetString(plainStream.ToArray());
    Console.WriteLine(plainString);
}

Doing this really properly and securely is far from as trivial as it seems. For some real production code, sneak a peak at https://github.com/xecrets/xecrets-file-cli and https://github.com/axantum/xecrets-net .

huangapple
  • 本文由 发表于 2023年6月30日 00:27:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定