Sometimes we encrypt data and send it over the network. A common flow is AES-256 → byte[] → string → transmit → decrypt on the other side.
In C#, we often convert a byte array to string with Convert.ToBase64String():
| |
This usually works, but if you pass the data via URL, special characters like +, space, % can break query strings and cause mismatches, leading to decryption failures.
References (Chinese):
- URL encoding basics: http://www.ruanyifeng.com/blog/2010/02/url_encoding.html
- Handling
+, space,=,%,&,#in URL params: http://blog.csdn.net/luo_deng/article/details/12186535
You might consider HttpUtility.UrlEncode / HttpUtility.UrlDecode. But beware: browsers may auto-decode once, and if your backend also decodes, you’ll end up decoding twice — corrupting the data.
Conclusion: Convert.ToBase64String() can yield URL-unsafe characters. One workaround: encode the bytes as hex strings (0–255 → two hex chars), avoiding reserved characters entirely.
| |
Problem solved.