开发者

.NET FromBase64String length [closed]

开发者 https://www.devze.com 2023-03-21 14:08 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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")

0

精彩评论

暂无评论...
验证码 换一张
取 消