If I run Convert.FromBase64String("test"开发者_StackOverflow) I get the error that it's invalid length. I'm trying to convert to a byte array so I can encrypt it, but on shorter strings it's giving the length error. I tried rpadding with '=' but no matter how many I put nothing seems to work.
What are my options with this?
If you have text data that you need to store as binary:
- You convert a string to byte[] via Encoding.GetBytes(), for example Encoding.UTF8.GetBytes()
- you convert such text data back to a string via Encoding.GetString(); this requires the binary to be valid text data via this encoding (not arbitrary binary)
If you have binary data you need to store as a string;
- you convert arbitrary binary to a string with Convert.ToBase64String()
- you convert such a string back to binary with Convert.FromBase64String(); this requires the string to be a valid base-64 string (not an arbitrary string)
So: look at Encoding.
Base64 is a method of converting a byte sequence into a specially-formatted string.
"test"
isn't a Base64 string.
You're looking for Encoding.UTF8.GetBytes("test")
精彩评论