Reputation: 331
When I m trying to convert the value1 to byte[] using the following code:
string value1 = "4rdHFh%2BHYoS8oLdVvbUzEVqB8Lvm7kSPnuwF0AAABYQ%3D";
byte[] value2 = Convert.FromBase64String(value1);
Ι received an error that the string contents invalid base-64 characters...
What's wrong?
Thanks!
Upvotes: 16
Views: 137317
Reputation: 316
I had the same problem, but none of these answer helping me! so I ask my question here: How to encrypt strings that may have non-base 64 characters and @luke-park solve my problem.(all credits go to him) So long story short is:
We have to convert our inputString
data into a byte array then convert that to bsae64
. Now our data is valid base64
format and can encrypt it:
byte[] data = Encoding.UTF8.GetBytes(inputString);
string b64 = Convert.ToBase64String(data);
I hope this help others people with the same problem.
PS: There are Two Encryption Classes in my post, that are great to use them for encrypt/decrypt strings.
Upvotes: 6
Reputation: 98740
s is composed of base 64 digits, white space characters, and trailing padding characters. The base 64 digits in ascending order from zero are the uppercase characters 'A' to 'Z', lowercase characters 'a' to 'z', numerals '0' to '9', and the symbols '+' and '/'.
%
is not allowed in base64
encoding.
Check out The Base64 index table from Wikipedia
Value Char
0 A 16 Q 32 g 48 w
1 B 17 R 33 h 49 x
2 C 18 S 34 i 50 y
3 D 19 T 35 j 51 z
4 E 20 U 36 k 52 0
5 F 21 V 37 l 53 1
6 G 22 W 38 m 54 2
7 H 23 X 39 n 55 3
8 I 24 Y 40 o 56 4
9 J 25 Z 41 p 57 5
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /
Upvotes: 19
Reputation: 1796
It's because of '%' char in the string. It's not allowed in base64. Where do you take this string from?
Upvotes: 0
Reputation: 44268
%2B is a + sign URL Encoded %3D is an = sign URL Encoded.
Your input string is actually.
4rdHFh+HYoS8oLdVvbUzEVqB8Lvm7kSPnuwF0AAABYQ=
Upvotes: 7
Reputation: 6698
The string contains invalid characters. In this case the %
sign.
If you unescape %2B
to "+" and %3D
to "=", then the string will be accepted.
Upvotes: 4
Reputation: 30097
I think the problem is with character %
. It is not allowed in base64 string
According to MSDN
public static byte[] FromBase64String( string s)
s is composed of base-64 digits, white-space characters, and trailing padding characters. The base-64 digits in ascending order from zero are the uppercase characters "A" to "Z", lowercase characters "a" to "z", numerals "0" to "9", and the symbols "+" and "/".
Upvotes: 3