
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Achieving Secure Unicode Encoding
|
1009
The Data property can be a very useful property to a developer. This property con-
tains key-value pairs that the developer creates and fills with information concerning
why this exception occurred. In this property, you can place variable names and the
data they contained at the time of the exception. This can give you even more clues
as to why this exception was thrown. See Recipe 7.18 for more information on the
Exception.Data property.
See Also
See the “SecurityException” topic in the MSDN documentation.
17.19 Achieving Secure Unicode Encoding
Problem
You want to make sure that your UnicodeEncoding or UTF8Encoding class detects any
errors, such as an invalid sequence of bytes.
Solution
Use the constructor for the UnicodeEncoding class that accepts three parameters:
UnicodeEncoding encoding = new UnicodeEncoding(false, true, true);
Or use the constructor for the UTF8Encoding class that accepts two parameters:
UTF8Encoding encoding = new UTF8Encoding(true, true);
Discussion
The final argument to both these constructors should be true. This turns on error
detection for this class. Error detection will help when an attacker somehow is able
to access and modify a Unicode- or a UTF8-encoded stream of characters. If the
attacker is not careful she can