-2

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
8K Tek
  • 3
  • 1

2 Answers2

0

You missed synchronizing the buffer:

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

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 .

Xecrets
  • 79
  • 5