string to byte array (to string to XML) and back again
where i get my first problem. If the strings lenght is a not a multiple of 4 (s.lenght % 4 == 0) i get a "Invalid length for a Base-64 char array" error.
That suggests that it's not base64 to start with. It sounds like you're going in the wrong direction here - base64 is used to convert binary data into text. To convert text into a binary form, you should normally just use Encoding.GetBytes
:
return Encoding.UTF8.GetBytes(text);
Now if you needed to encode the result of the encryption (which will be binary data) as text, then you'd use base64. (Because the result of encrypting UTF-8-encoded text is not UTF-8-encoded text.)
So something like:
public static string EncryptText(string input)
{
byte[] unencryptedBytes = Encoding.UTF8.GetBytes(input);
byte[] encryptedBytes = EncryptBytes(unencryptedBytes); // Not shown here
return Convert.ToBase64String(encryptedBytes);
}
public static string DecryptText(string input)
{
byte[] encryptedBytes = Convert.FromBase64String(input);
byte[] unencryptedBytes = DecryptBytes(encryptedBytes); // Not shown here
return Encoding.UTF8.GetString(unencryptedBytes);
}
Alex
Who the fuck is General Failure and why is he reading my hard disk?
Updated on June 29, 2022Comments
-
Alex almost 2 years
i know there are 1million questions about "string - byte array" conversion out there but none of them fit my problem.
For the installation of my software i need to save some informations from the user (serveraddress, userID, password and so on). Some of these informations need do be protected (encrypted using DPAPI). For that i have to convert the
string
(SecureString
) tobyte[]
public static byte[] StringToByte(string s) { return Convert.FromBase64String(s); }
where i get my first problem. If the strings lenght is a not a multiple of 4 (
s.lenght % 4 == 0
) i get a "Invalid length for a Base-64 char array" error. I've read that i can (have to) add "=" to the end of thestring
but some of these strings may be passwords (which may contain "="). I need to store the (encrypted) data in a XML-file why i can't use Unicode encoding (i don't know why but it corrupts the XML file ... because of encoding i would suppose).As last step i have to go back the way to get the stored data on app startup.
Does someone of you can help me solving this problem ? I don't care the output in the XML as long as it is "readable".
best regards Alex
-
terrybozzio over 10 yearssorry to bother you jon but i have a pending question related to an old post of yours where you said(and since then i have taken that as a rule),that the saffest way to convert textual data to and from byte[] is using base64,you meant in all scenarios or just some? sorry again.
-
Alex over 10 yearspublic static string ByteToString(byte[] bytes) { return Convert.ToBase64String(bytes); } public static byte[] StringToByte(string s) { return Encoding.UTF8.GetBytes(s); } tried this, but if i use it
string s = ByteToString(StringToByte("test"));
the result is "dGVzdA==" which is obviously not "test" -
Jon Skeet over 10 years@Alex: Well yes, because now you're still converting from bytes to a string with
Convert.ToBase64String
! UseEncoding.GetString
to reverse the operation ofEncoding.GetBytes
. -
Jon Skeet over 10 years@terrybozzio: No - if you're starting with arbitrary binary data, you use Base64 to get to text, and you can get back to the same binary data that way. If you're starting with text data, use a normal encoding such as UTF-8.
-
Alex over 10 yearsok, do i understand you correct, i have to start with
Encoding.UTF8
because it's "string input" then i have to encrypt it and go back to string withConvert.ToBase64String
bacause it's now "raw binary" and vice versa to get my saved datas ? -
Alex over 10 yearsok, that was my mistake, i thought i use the same encoding all the time. thanks a lot
-
Jon Skeet over 10 years@Alex: Do you understand why you should use a different encoding in the two different cases? If not, it's worth thinking about it further. It's basically a matter of determining the encoding based on the type of the original data.