C
C#2y ago
MrScautHD

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

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;
}
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);
return Convert.ToBase64String(array).ReplaceLineEndings(text);
But that does not work
28 Replies
MrScautHD
MrScautHDOP2y ago
i Encrypt my text But it does not copy the lines because i wanna write that in a file but it goes all in 1 long line and not like before in more lines... i hope anyone can help me 🙂 Has no one a idea? :/
Anton
Anton2y ago
idk what you're doing but I can point out two things about the code you should improve 1. use early returns 2. using has a second form that applies to the current scope use that form using var stream = new blah();
MrScautHD
MrScautHDOP2y ago
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;
}
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;
}
like that?
ero
ero2y ago
did you even change anything
MrScautHD
MrScautHDOP2y ago
i try to give me Encrypted string the same line brakes yes...
ero
ero2y ago
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());
}
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());
}
they mean like this
MrScautHD
MrScautHDOP2y ago
oh yea well thats cleaner thxx But do you know how i can give the EncryptString the same linke brakes
ero
ero2y ago
no clue, sorry
MrScautHD
MrScautHDOP2y ago
:/ ok
MrScautHD
MrScautHDOP2y ago
That is how it should look after a line
MrScautHD
MrScautHDOP2y ago
but it does just 1 very long line
MrScautHD
MrScautHDOP2y ago
MrScautHD
MrScautHDOP2y ago
i try to do the old line brakes before it get encrypted to the encrypted one any one a idea?
ero
ero2y ago
so just to clarify, you want to keep the line breaks from the original string? and somehow take those over to the encrypted one? or do you just want to add line breaks after a certain amount of characters cause i mean, Base64FormattingOptions.InsertLineBreaks exists
MrScautHD
MrScautHDOP2y ago
yes
ero
ero2y ago
but i don't know if that's what you want alright well, that can't work that can never work
MrScautHD
MrScautHDOP2y ago
thats not possbile?
ero
ero2y ago
the only way i see this happening is if you first .Split() the string on the line breaks and then encrypt each line individually
MrScautHD
MrScautHDOP2y ago
ouf is it possible to get the count of characters of a line? then i remove the spaces in it and then i split it with the encpyted one every time when a normal line should end would that not work? btw this code does not work... (EDIT: i fixed it) and return then a array?
ero
ero2y ago
and then .Join again with Environment.NewLine as the delimitor
MrScautHD
MrScautHDOP2y ago
hey question is it allowed to have just 1 character in a line? because {
ero
ero2y ago
sure
MrScautHD
MrScautHDOP2y ago
you sure?
ero
ero2y ago
of course
MrScautHD
MrScautHDOP2y ago
because my console print
---> 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.
---> 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.
ero
ero2y ago
don't really know, sorry
MrScautHD
MrScautHDOP2y ago
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();
}
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();
}
thats my Decrypter ouf
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server