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
MrScautHD2y 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
MrScautHD2y 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
MrScautHD2y 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
MrScautHD2y 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
MrScautHD2y ago
:/ ok
MrScautHD
MrScautHD2y ago
That is how it should look after a line
MrScautHD
MrScautHD2y ago
but it does just 1 very long line
MrScautHD
MrScautHD2y ago
MrScautHD
MrScautHD2y 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
MrScautHD2y ago
yes
Want results from more Discord servers?
Add your server