private void ExceptionTest() { Assert.Throws <UTF8Exception>( delegate { UTF8.Decode("\uFFFF"); }); Assert.Throws <UTF8Exception>( delegate { UTF8.Decode("\xE9\x00\x00"); }); Assert.Throws <UTF8Exception>( delegate { UTF8.Decode("\xC2\uFFFF"); }); Assert.Throws <UTF8Exception>( delegate { UTF8.Decode("\xF0\x9D"); }); }
internal static Packet DecodePacket(string data) { if (data.StartsWith("b")) { return(DecodeBase64Packet(data.Substring(1))); } int type; var s = data.Substring(0, 1); if (!int.TryParse(s, out type)) { type = -1; } try { data = UTF8.Decode(data); } catch (Exception) { return(_err); } if (type < 0 || type >= _packetsList.Count) { return(_err); } if (data.Length > 1) { return(new Packet(_packetsList[(byte)type], data.Substring(1))); } return(new Packet(_packetsList[(byte)type], null)); }
internal static Packet DecodePacket(string data, bool utf8decode = false) { if (data.StartsWith("b")) { return(Packet.DecodeBase64Packet(data.Substring(1))); } int result; if (!int.TryParse(data.Substring(0, 1), out result)) { result = -1; } if (utf8decode) { try { data = UTF8.Decode(data); } catch (Exception ex) { return(Packet._err); } } if (result < 0 || result >= Packet._packetsList.Count) { return(Packet._err); } if (data.Length > 1) { return(new Packet(Packet._packetsList[(byte)result], (object)data.Substring(1))); } return(new Packet(Packet._packetsList[(byte)result], (object)null)); }
public static string Decode(string str, bool utf8decode) { int o1, o2, o3, bits, h1, h2, h3, h4; string[] d; int c; string plain; string coded; string b64 = code; coded = utf8decode ? UTF8.Decode(str) : str; d = new string[coded.Length / 3]; for (c = 0; c < coded.Length; c += 4) { // unpack four hexets into three octets h1 = b64.IndexOf((char)coded[c]); h2 = b64.IndexOf((char)coded[c + 1]); h3 = b64.IndexOf((char)coded[c + 2]); h4 = b64.IndexOf((char)coded[c + 3]); bits = h1 << 18 | h2 << 12 | h3 << 6 | h4; o1 = bits >> 16 & 0xff; o2 = bits >> 8 & 0xff; o3 = bits & 0xff; d[c / 4] = ((char)o1).ToString() + ((char)o2).ToString() + ((char)o3).ToString(); // check for padding if (h4 == 0x40) { d[c / 4] = ((char)o1).ToString() + ((char)o2).ToString(); } if (h3 == 0x40) { d[c / 4] = ((char)o1).ToString(); } } plain = string.Join("", d); // join() is far faster than repeated string concatenation in IE return(utf8decode ? UTF8.Decode(plain) : plain); }
private void DecodingTest() { Assert.Equal(Decoded, UTF8.Decode(Encoded)); }
static public string Decrypt(string ciphertext, string password, int nBits) { int blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES if (!(nBits == 128 || nBits == 192 || nBits == 256)) { return(""); // standard allows 128/192/256 bit keys } ciphertext = Base64.Decode(ciphertext); password = UTF8.Encode(password); // use AES to encrypt password (mirroring encrypt routine) var nBytes = nBits / 8; // no bytes in key UInt64[] pwBytes = new UInt64[nBytes]; for (var i = 0; i < nBytes; i++) { pwBytes[i] = double.IsNaN((int)password[i]) ? 0 : (UInt64)password[i]; } UInt64[] key = cipher(pwBytes, keyExpansion(pwBytes)); key = key.Concat(Slice(key, 0, nBytes - 16)).ToArray <UInt64>(); // expand key to 16/24/32 bytes long // recover nonce from 1st 8 bytes of ciphertext UInt64[] counterBlock = new UInt64[blockSize]; string ctrTxt = ciphertext.Substring(0, 8); for (var i = 0; i < 8; i++) { counterBlock[i] = (UInt64)ctrTxt[i]; } // generate key schedule var keySchedule = keyExpansion(key); // separate ciphertext into blocks (skipping past initial 8 bytes) int nBlocks = (int)Math.Ceiling((decimal)(ciphertext.Length - 8) / (decimal)blockSize); string[] ct = new string[nBlocks]; for (var b = 0; b < nBlocks; b++) { ct[b] = Slice(ciphertext, 8 + b * blockSize, 8 + b * blockSize + blockSize <= ciphertext.Length ? 8 + b * blockSize + blockSize : ciphertext.Length); } // plaintext will get generated block-by-block into array of block-length strings string[] plaintxt = new string[ct.Length]; for (var b = 0; b < nBlocks; b++) { // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) for (var c = 0; c < 4; c++) { counterBlock[15 - c] = (UInt64)((b) >> c * 8) & 0xff; } for (var c = 0; c < 4; c++) { counterBlock[15 - c - 4] = (UInt64)(((int)((b + 1) / (decimal)(((UInt64)0x100000000)) - 1)) >> c * 8) & 0xff; } var cipherCntr = cipher(counterBlock, keySchedule); // encrypt counter block UInt64[] plaintxtByte = new UInt64[ct[b].Length]; string[] plaintxtString = new string[ct[b].Length]; for (var i = 0; i < ct[b].Length; i++) { // -- xor plaintxt with ciphered counter byte-by-byte -- plaintxtByte[i] = cipherCntr[i] ^ (UInt64)ct[b][i]; plaintxtString[i] = ((char)plaintxtByte[i]).ToString(); } plaintxt[b] = string.Join("", plaintxtString); } // join array of blocks into single plaintext string var plaintext = string.Join("", plaintxt); plaintext = UTF8.Decode(plaintext); // decode from UTF8 back to Unicode multi-byte chars return(plaintext); }