Is there any tryparse for Convert.FromBase64String
or we just count the character if it is equal 64 character or not.
I copy a encryption and decryption class, but there is an error on the following line. I want to check whether or not the cipherText can be converted without error
byte[] bytes = Convert.FromBase64String(cipherText);
Well, you could check the string first. It must have the right number of characters, verify with (str.Length * 6) % 8 == 0. And you can check every character, it must be in the set A-Z, a-z, 0-9, +, / and =. The = character can only appear at the end.
This is expensive, it is actually cheaper to just catch the exception. The reason .NET doesn’t have a TryXxx() version.