public void TestEncodeOptionalEncoded() { string UniCodeString = "\u0021\u0026\u002A\u003B"; byte[] UTF7Bytes = null; //Optional Characters are allowed. UTF7Encoding UTF7enc = new UTF7Encoding(true); UTF7Bytes = UTF7enc.GetBytes(UniCodeString); Assert.AreEqual(0x21, UTF7Bytes [0], "UTF7 #1"); Assert.AreEqual(0x26, UTF7Bytes [1], "UTF7 #2"); Assert.AreEqual(0x2A, UTF7Bytes [2], "UTF7 #3"); Assert.AreEqual(0x3B, UTF7Bytes [3], "UTF7 #4"); //Optional characters are not allowed. UTF7enc = new UTF7Encoding(false); UTF7Bytes = UTF7enc.GetBytes(UniCodeString); Assert.AreEqual(0x2B, UTF7Bytes [0], "UTF7 #5"); Assert.AreEqual(0x41, UTF7Bytes [1], "UTF7 #6"); Assert.AreEqual(0x43, UTF7Bytes [2], "UTF7 #7"); Assert.AreEqual(0x45, UTF7Bytes [3], "UTF7 #8"); Assert.AreEqual(0x41, UTF7Bytes [1], "UTF7 #6"); }
public static void Main() { // A few optional characters. string chars = "!@#$"; // The default Encoding does not allow optional characters. // Alternate byte values are used. UTF7Encoding utf7 = new UTF7Encoding(); Byte[] bytes1 = utf7.GetBytes(chars); Console.WriteLine("Default UTF7 Encoding:"); ShowArray(bytes1); // Convert back to characters. Console.WriteLine("Characters:"); ShowArray(utf7.GetChars(bytes1)); // Now, allow optional characters. // Optional characters are encoded with their normal code points. UTF7Encoding utf7AllowOptionals = new UTF7Encoding(true); Byte[] bytes2 = utf7AllowOptionals.GetBytes(chars); Console.WriteLine("UTF7 Encoding with optional characters allowed:"); ShowArray(bytes2); // Convert back to characters. Console.WriteLine("Characters:"); ShowArray(utf7AllowOptionals.GetChars(bytes2)); }
public static void Main() { Byte[] bytes; // Unicode characters. Char[] chars = new Char[] { '\u0023', // # '\u0025', // % '\u03a0', // Pi '\u03a3' // Sigma }; UTF7Encoding utf7 = new UTF7Encoding(); int byteCount = utf7.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; int bytesEncodedCount = utf7.GetBytes(chars, 1, 2, bytes, 0); Console.WriteLine( "{0} bytes used to encode characters.", bytesEncodedCount ); Console.Write("Encoded bytes: "); foreach (Byte b in bytes) { Console.Write("[{0}]", b); } Console.WriteLine(); }
public static void Main() { // Create a UTF-7 encoding. UTF7Encoding utf7 = new UTF7Encoding(); // A Unicode string with two characters outside a 7-bit code range. String unicodeString = "This Unicode string contains two characters " + "with codes outside a 7-bit code range, " + "Pi (\u03a0) and Sigma (\u03a3)."; Console.WriteLine("Original string:"); Console.WriteLine(unicodeString); // Encode the string. Byte[] encodedBytes = utf7.GetBytes(unicodeString); Console.WriteLine(); Console.WriteLine("Encoded bytes:"); foreach (Byte b in encodedBytes) { Console.Write("[{0}]", b); } Console.WriteLine(); // Decode bytes back to string. // Notice Pi and Sigma characters are still present. String decodedString = utf7.GetString(encodedBytes); Console.WriteLine(); Console.WriteLine("Decoded bytes:"); Console.WriteLine(decodedString); }
public static ValidationResponse ValidateByUpload(string validationData, string encodingType = "UNICODE_ENCODING") { #region Data encoding byte[] encodedValidationDataByteArr; if (encodingType == UNICODE_ENCODING) { // Use Unicode encoding UnicodeEncoding encodedValidationData = new UnicodeEncoding(); encodedValidationDataByteArr = encodedValidationData.GetBytes(validationData); } else if (encodingType == ASCII_ENCODING) { // Use ASCII (7-bit) encoding ASCIIEncoding encodedValidationData = new ASCIIEncoding(); encodedValidationDataByteArr = encodedValidationData.GetBytes(validationData); } else if (encodingType == UTF7_ENCODING) { // Use UTF-7 encoding UTF7Encoding encodedValidationData = new UTF7Encoding(); encodedValidationDataByteArr = encodedValidationData.GetBytes(validationData); } else if (encodingType == UTF8_ENCODING) { // Use UTF-8 encoding UTF8Encoding encodedValidationData = new UTF8Encoding(); encodedValidationDataByteArr = encodedValidationData.GetBytes(validationData); } else if (encodingType == UTF32_ENCODING) { // Use UTF-32 encoding UTF32Encoding encodedValidationData = new UTF32Encoding(); encodedValidationDataByteArr = encodedValidationData.GetBytes(validationData); } else { throw new InvalidEncodingException("An invalid encoding type was specified. Type: " + encodingType); } #endregion #region Request formation HttpWebRequest uploadRequest = HttpWebRequest.CreateHttp(VALIDATOR_URL + VALIDATOR_URL_GET_ROOT + VALIDATOR_URL_GET_TYPE); uploadRequest.Method = REQUEST_METHOD; uploadRequest.ContentType = CONTENT_TYPE; uploadRequest.ContentLength = encodedValidationDataByteArr.Length; Stream uploadRequestStream = uploadRequest.GetRequestStream(); uploadRequestStream.Write(encodedValidationDataByteArr, 0, encodedValidationDataByteArr.Length); #endregion // Make request // Stores response object in `uploadRequestResponse` HttpWebResponse uploadRequestResponse = (HttpWebResponse)uploadRequest.GetResponse(); // Get response data Stream uploadRequestResponseStream = uploadRequestResponse.GetResponseStream(); StreamReader uploadRequestResponseStreamReader = new StreamReader(uploadRequestResponseStream); string uploadRequestResponseString = uploadRequestResponseStreamReader.ReadToEnd(); ValidationResponse response = ParseValidationResponse(uploadRequestResponseString); }
private static void VerifyUtf7Encoding(UTF7Encoding encoding, bool allowOptionals) { Assert.Empty(encoding.GetPreamble()); Assert.Equal(new EncoderReplacementFallback(string.Empty), encoding.EncoderFallback); Assert.Equal(1, encoding.DecoderFallback.MaxCharCount); Assert.Equal(984, encoding.DecoderFallback.GetHashCode()); if (allowOptionals) { Assert.Equal(new byte[] { 33 }, encoding.GetBytes("!")); } else { Assert.Equal(new byte[] { 43, 65, 67, 69, 45 }, encoding.GetBytes("!")); } }
public void PosTest1() { Byte[] bytes; String chars = "UTF7 Encoding Example"; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = chars.Length; bytes = new Byte[byteCount]; int bytesEncodedCount = UTF7.GetBytes(chars, 1, 2, bytes, 0); }
public static void VerifyUtf7Encoding(UTF7Encoding encoding, bool allowOptionals) { Assert.Empty(encoding.GetPreamble()); Assert.Equal(new EncoderReplacementFallback(string.Empty), encoding.EncoderFallback); Assert.Equal(1, encoding.DecoderFallback.MaxCharCount); Assert.Equal(984, encoding.DecoderFallback.GetHashCode()); if (allowOptionals) { Assert.Equal(new byte[] { 33 }, encoding.GetBytes("!")); } else { Assert.Equal(new byte[] { 43, 65, 67, 69, 45 }, encoding.GetBytes("!")); } }
public void PosTest2() { Byte[] bytes; String chars = ""; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = chars.Length; bytes = new Byte[byteCount]; int bytesEncodedCount = UTF7.GetBytes(chars, 0, byteCount, bytes, 0); Assert.Equal(0, bytesEncodedCount); }
public void PosTest2() { Byte[] bytes; Char[] chars = new Char[] { }; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = UTF7.GetByteCount(chars, 0, 0); bytes = new Byte[byteCount]; int bytesEncodedCount = UTF7.GetBytes(chars, 0, 0, bytes, 0); Assert.Equal(0, bytesEncodedCount); }
//=============================================================================== // Name: Function ComputeHash // Input: // ByRef strMessage As String - String to be hashed // Output: // String - Hashed string // Purpose: MD5 Hash function // Remarks: This function is primarily used by the Short Key Function // to hash strings; it matches the hash generated in the VB6 version // therefore both .NET and VB6 versions generate the same serial number and key //=============================================================================== public static string ComputeHash(ref string strMessage) { byte[] hashedDataBytes = null; UTF7Encoding encoder = new UTF7Encoding(); MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(strMessage)); string strHash = BitConverter.ToString(hashedDataBytes); return(strHash.Replace("-", "").ToLower()); }
public void NegTest2() { Byte[] bytes; String chars = "UTF7 Encoding Example"; UTF7Encoding UTF7 = new UTF7Encoding(); bytes = null; Assert.Throws<ArgumentNullException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, 1, 2, bytes, 0); }); }
public void NegTest3() { Byte[] bytes; String chars = "UTF7 Encoding Example"; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = chars.Length; bytes = new Byte[byteCount]; Assert.Throws<ArgumentOutOfRangeException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, -1, 2, bytes, 0); }); }
public void NegTest2() { Byte[] bytes; String chars = "UTF7 Encoding Example"; UTF7Encoding UTF7 = new UTF7Encoding(); bytes = null; Assert.Throws <ArgumentNullException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, 1, 2, bytes, 0); }); }
public void NegTest1() { Byte[] bytes; String chars = null; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = 10; bytes = new Byte[byteCount]; Assert.Throws<ArgumentNullException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, 1, 2, bytes, 0); }); }
protected byte[] Decode7Bit(string value) { try { var utf7 = new UTF7Encoding(); return(utf7.GetBytes(value)); } catch { return(null); } }
public void NegTest1() { Byte[] bytes; Char[] chars = null; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = 10; bytes = new Byte[byteCount]; Assert.Throws <ArgumentNullException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, 1, 2, bytes, 0); }); }
public void NegTest4() { Byte[] bytes; String chars = "UTF7 Encoding Example"; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = chars.Length; bytes = new Byte[byteCount]; Assert.Throws <ArgumentOutOfRangeException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, 1, -2, bytes, 0); }); }
public void PosTest1() { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = UTF7.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; int bytesEncodedCount = UTF7.GetBytes(chars, 1, 2, bytes, 0); }
public void NegTest2() { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = UTF7.GetByteCount(chars, 1, 2); bytes = null; Assert.Throws<ArgumentNullException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, 1, 2, bytes, 0); }); }
/* * Print * * Syntax: * byte[] Print( * FileStream _File // Any open FileStream * long _Offset // Absolute pointer to where string will be written * Encoding _Encoding // Encoding of printed string * string _String // String buffer * ) * byte[] Print( * FileStream _File // Any open FileStream * Encoding _Encoding // Encoding of printed string * string _String // String buffer * ) * byte[] Print( * FileStream _File // Any open FileStream * long _Offset // Absolute pointer to where string will be written * string _String // String buffer * ) * byte[] Print( * FileStream _File // Any open FileStream * string _String // String buffer * ) * * Explanation: * Write a formatted string to a binary file. Null-termination isn't provided. * * This function overwrites _String.Length bytes at _Offset. If the eof (end of file) is * reached, the file is expanded accordingly and the string is completely written. * * _String is always encoded using the _Encoding parameter, regardless of its original * formatting and encoding. * * The overloaded Print() functions without the _Offset parameter will simply print at * the eof (end of file). * * The two overloaded Print() functions without _Encoding always force ASCII printing. * * Return Value: * true on success; false otherwise */ public bool Print(FileStream _File, long _Offset, Encoding _Encoding, string _String) { if (ErrorExists(_File)) { return(false); } try { if (_Encoding == Encoding.ASCII) { ASCIIEncoding enc = new ASCIIEncoding(); byte[] _Buffer = enc.GetBytes(_String); Write(_File, _Offset, _Buffer, _Buffer.Length); } else if (_Encoding == Encoding.Unicode) { UnicodeEncoding enc = new UnicodeEncoding(); byte[] _Buffer = enc.GetBytes(_String); Write(_File, _Offset, _Buffer, _Buffer.Length); } else if (_Encoding == Encoding.UTF32) { UTF32Encoding enc = new UTF32Encoding(); byte[] _Buffer = enc.GetBytes(_String); Write(_File, _Offset, _Buffer, _Buffer.Length); } else if (_Encoding == Encoding.UTF7) { UTF7Encoding enc = new UTF7Encoding(); byte[] _Buffer = enc.GetBytes(_String); Write(_File, _Offset, _Buffer, _Buffer.Length); } else if (_Encoding == Encoding.UTF8) { UTF8Encoding enc = new UTF8Encoding(); byte[] _Buffer = enc.GetBytes(_String); Write(_File, _Offset, _Buffer, _Buffer.Length); } } catch (Exception Error) { Console.Write("{0}\r\n", Error.Message); return(false); } return(true); }
public void NegTest3() { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = UTF7.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; Assert.Throws <ArgumentOutOfRangeException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, -1, 2, bytes, 0); }); }
public void TestDirectlyEncoded1() { // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded. string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F"; byte[] UTF7Bytes = null; UTF7Encoding UTF7enc = new UTF7Encoding(); UTF7Bytes = UTF7enc.GetBytes(UniCodeString); Assert.AreEqual(0x61, UTF7Bytes [0], "UTF7 #1"); Assert.AreEqual(0x7A, UTF7Bytes [1], "UTF7 #2"); Assert.AreEqual(0x41, UTF7Bytes [2], "UTF7 #3"); Assert.AreEqual(0x5A, UTF7Bytes [3], "UTF7 #4"); Assert.AreEqual(0x30, UTF7Bytes [4], "UTF7 #5"); Assert.AreEqual(0x39, UTF7Bytes [5], "UTF7 #6"); Assert.AreEqual(0x27, UTF7Bytes [6], "UTF7 #7"); Assert.AreEqual(0x3F, UTF7Bytes [7], "UTF7 #8"); }
public static void Main() { // Create an instance of UTF7Encoding. UTF7Encoding u7 = new UTF7Encoding(true); // Create byte arrays from the same string containing the following characters: // Latin Small Letter Z (U+007A) // Latin Small Letter A (U+0061) // Combining Breve (U+0306) // Latin Small Letter AE With Acute (U+01FD) // Greek Small Letter Beta (U+03B2) String myStr = "za\u0306\u01FD\u03B2"; // Encode the string. byte[] myBArr = new byte[u7.GetByteCount(myStr)]; u7.GetBytes(myStr, 0, myStr.Length, myBArr, 0); // Decode the byte array. Console.WriteLine("The new string is: {0}", u7.GetString(myBArr, 0, myBArr.Length)); }
public static string PHPMd5Encode(string input) { string Hashpass = input; // UTF7 Version (probably won't work across languages) System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); UTF7Encoding encoder = new UTF7Encoding(); Byte[] bytes; bytes = encoder.GetBytes(Hashpass); bytes = md5.ComputeHash(bytes); string strHex = string.Empty; foreach (byte b in bytes) { strHex = string.Concat(strHex, String.Format("{0:x2}", b)); } return(strHex); }
public void TestEncodeUnicodeShifted2() { string UniCodeString = "\u0041\u2262\u0391\u002E"; byte[] UTF7Bytes = new byte [10]; int Length = UniCodeString.Length; UTF7Encoding UTF7enc = new UTF7Encoding(); int Cnt = UTF7enc.GetBytes(UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0); //"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642 Assert.AreEqual(0x41, UTF7Bytes [0], "UTF7 #1"); Assert.AreEqual(0x2B, UTF7Bytes [1], "UTF7 #2"); Assert.AreEqual(0x49, UTF7Bytes [2], "UTF7 #3"); Assert.AreEqual(0x6D, UTF7Bytes [3], "UTF7 #4"); Assert.AreEqual(0x49, UTF7Bytes [4], "UTF7 #5"); Assert.AreEqual(0x44, UTF7Bytes [5], "UTF7 #6"); Assert.AreEqual(0x6B, UTF7Bytes [6], "UTF7 #7"); Assert.AreEqual(0x51, UTF7Bytes [7], "UTF7 #8"); Assert.AreEqual(0x2D, UTF7Bytes [8], "UTF7 #9"); Assert.AreEqual(0x2E, UTF7Bytes [9], "UTF7 #10"); }
public void NegTest8() { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = UTF7.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; Assert.Throws<ArgumentOutOfRangeException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, chars.Length, 2, bytes, 1); }); }