❔ How to convert line endings from my not encypted string to my encypted string

MMrScautHD2/4/2023
    public string EncryptString(string text) {
        if (this._encrypt) {
            byte[] iv = new byte[16];
            byte[] array;

            using (Aes aes = Aes.Create()) {
                aes.Key = Encoding.UTF8.GetBytes(this._encryptKey);
                aes.IV = iv;

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

                using (MemoryStream memoryStream = new MemoryStream()) {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) {
                        using (StreamWriter streamWriter = new StreamWriter(cryptoStream)) {
                            streamWriter.Write(text);
                        }

                        array = memoryStream.ToArray();
                    }
                }
            }

            return Convert.ToBase64String(array);
        }

        return text;
    }

I tried already
return Convert.ToBase64String(array).ReplaceLineEndings(text);

But that does not work
MMrScautHD2/4/2023
i Encrypt my text
MMrScautHD2/4/2023
But
MMrScautHD2/4/2023
it does not copy the lines
MMrScautHD2/4/2023
because i wanna write that in a file
MMrScautHD2/4/2023
but it goes all in 1 long line
MMrScautHD2/4/2023
and not like before in more lines...
MMrScautHD2/4/2023
i hope anyone can help me 🙂
MMrScautHD2/4/2023
Has no one a idea?
MMrScautHD2/4/2023
:/
AAntonC2/4/2023
idk what you're doing but I can point out two things about the code you should improve
AAntonC2/4/2023
1. use early returns
AAntonC2/4/2023
2. using has a second form that applies to the current scope
AAntonC2/4/2023
use that form
AAntonC2/4/2023
using var stream = new blah();
MMrScautHD2/4/2023
    public string EncryptString(string text) {
        if (this._encrypt) {
            byte[] iv = new byte[16];

            using (Aes aes = Aes.Create()) {
                aes.Key = Encoding.UTF8.GetBytes(this._encryptKey);
                aes.IV = iv;

                ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
                
                using (var memoryStream = new MemoryStream()) {
                    using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) {
                        using (var streamWriter = new StreamWriter(cryptoStream)) {
                            streamWriter.Write(text);
                        }
                        
                        return Convert.ToBase64String(memoryStream.ToArray());
                    }
                }
            }
        }

        return text;
    }
MMrScautHD2/4/2023
like that?
EEro2/4/2023
did you even change anything
MMrScautHD2/4/2023
i try to give me Encrypted string the same line brakes
MMrScautHD2/4/2023
yes...
EEro2/4/2023
public string EncryptString(string text) {
    if (!_encrypt) {
        return text;
    }

    byte[] iv = new byte[16];

    using Aes aes = Aes.Create();
    aes.Key = Encoding.UTF8.GetBytes(this._encryptKey);
    aes.IV = iv;

    ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
    
    using var memoryStream = new MemoryStream();
    using var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
    using var streamWriter = new StreamWriter(cryptoStream);
    streamWriter.Write(text);

    return Convert.ToBase64String(memoryStream.ToArray());
}
EEro2/4/2023
they mean like this
MMrScautHD2/4/2023
oh
MMrScautHD2/4/2023
yea well thats cleaner
MMrScautHD2/4/2023
thxx
MMrScautHD2/4/2023
But do you know
MMrScautHD2/4/2023
how i can give the EncryptString the same linke brakes
EEro2/4/2023
no clue, sorry
MMrScautHD2/4/2023
:/ ok
MMrScautHD2/4/2023
That is how it should look after a line
Image
MMrScautHD2/4/2023
but it does just 1 very long line
MMrScautHD2/4/2023
Image
MMrScautHD2/4/2023
i try to do the old line brakes before it get encrypted to the encrypted one
MMrScautHD2/4/2023
any one a idea?
EEro2/4/2023
so just to clarify, you want to keep the line breaks from the original string? and somehow take those over to the encrypted one?
EEro2/4/2023
or do you just want to add line breaks after a certain amount of characters
EEro2/4/2023
cause i mean, Base64FormattingOptions.InsertLineBreaks exists
MMrScautHD2/4/2023
yes
EEro2/4/2023
but i don't know if that's what you want
EEro2/4/2023
alright well, that can't work
EEro2/4/2023
that can never work
MMrScautHD2/4/2023
thats not possbile?
EEro2/4/2023
the only way i see this happening is if you first .Split() the string on the line breaks
EEro2/4/2023
and then encrypt each line individually
MMrScautHD2/4/2023
ouf
MMrScautHD2/4/2023
is it possible to get the count of characters of a line?
MMrScautHD2/4/2023
then i remove the spaces in it
MMrScautHD2/4/2023
and then i split it with the encpyted one every time when a normal line should end
would that not work?
MMrScautHD2/4/2023
btw this code does not work... (EDIT: i fixed it)
MMrScautHD2/4/2023
and return then a array?
EEro2/4/2023
and then .Join again
EEro2/4/2023
with Environment.NewLine as the delimitor
MMrScautHD2/4/2023
hey question
MMrScautHD2/4/2023
is it allowed to have just 1 character in a line?
MMrScautHD2/4/2023
because {
EEro2/4/2023
sure
MMrScautHD2/4/2023
you sure?
EEro2/4/2023
of course
MMrScautHD2/4/2023
because my console print
MMrScautHD2/4/2023
 ---> System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
EEro2/4/2023
don't really know, sorry
MMrScautHD2/4/2023
    public string DecryptString(string text) {
        if (!this._encrypt) {
            return text;
        }
        
        using var aes = Aes.Create();
        aes.Key = Encoding.UTF8.GetBytes(this._encryptKey);
        aes.IV = new byte[16];
                
        ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

        using var memoryStream = new MemoryStream(Convert.FromBase64String(text));
        using var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
        using var streamReader = new StreamReader(cryptoStream);
        
        return streamReader.ReadToEnd();
    }
MMrScautHD2/4/2023
thats my Decrypter
MMrScautHD2/4/2023
ouf
AAccord2/5/2023
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.