public static string Encode (byte[] data, MachineKeyProtection protectionOption) { if (data == null) throw new ArgumentNullException ("data"); var config = WebConfigurationManager.GetWebApplicationSection ("system.web/machineKey") as MachineKeySection; byte[] result; switch (protectionOption) { case MachineKeyProtection.All: result = MachineKeySectionUtils.EncryptSign (config, data); break; case MachineKeyProtection.Encryption: result = MachineKeySectionUtils.Encrypt (config, data); break; case MachineKeyProtection.Validation: result = MachineKeySectionUtils.Sign (config, data); break; default: return String.Empty; } return MachineKeySectionUtils.GetHexString (result); }
internal static object Deserialize(string serializedValue, SerializationMode mode, IMachineKey machineKey) { if (String.IsNullOrEmpty(serializedValue)) { throw new ArgumentException(MvcResources.Common_NullOrEmpty, "serializedValue"); } MachineKeyProtection protectionMode = GetMachineKeyProtectionMode(mode); try { // First, need to decrypt / verify data byte[] rawBytes = machineKey.Decode(serializedValue, protectionMode); // Next, verify magic header if (!ArrayContainsMagicHeader(rawBytes)) { throw new SerializationException(MvcResources.MvcSerializer_MagicHeaderCheckFailed); } // Finally, deserialize the object graph using (MemoryStream ms = new MemoryStream(rawBytes, _magicHeader.Length, rawBytes.Length - _magicHeader.Length)) { return(DeserializeGraph(ms)); } } catch (Exception ex) { throw CreateSerializationException(ex); } }
public static string Encode(byte[] data, MachineKeyProtection protectionOption) { if (data == null) { throw new ArgumentNullException("data"); } var config = WebConfigurationManager.GetWebApplicationSection("system.web/machineKey") as MachineKeySection; byte[] result; switch (protectionOption) { case MachineKeyProtection.All: result = MachineKeySectionUtils.EncryptSign(config, data); break; case MachineKeyProtection.Encryption: result = MachineKeySectionUtils.Encrypt(config, data); break; case MachineKeyProtection.Validation: result = MachineKeySectionUtils.Sign(config, data); break; default: return(String.Empty); } return(MachineKeySectionUtils.GetHexString(result)); }
public static string Encode(byte[] data, MachineKeyProtection protectionOption) { if (data == null) { throw new ArgumentNullException("data"); } ////////////////////////////////////////////////////////////////////// // Step 1: Get the MAC and add to the blob if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation) { byte[] bHash = MachineKeySection.HashData(data, null, 0, data.Length); byte[] bAll = new byte[bHash.Length + data.Length]; Buffer.BlockCopy(data, 0, bAll, 0, data.Length); Buffer.BlockCopy(bHash, 0, bAll, data.Length, bHash.Length); data = bAll; } if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption) { ////////////////////////////////////////////////////////////////////// // Step 2: Encryption data = MachineKeySection.EncryptOrDecryptData(true, data, null, 0, data.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption); } ////////////////////////////////////////////////////////////////////// // Step 3: Covert the buffer to HEX string and return it return(CryptoUtil.BinaryToHex(data)); }
public static byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { if (encodedData == null) { throw new ArgumentNullException("encodedData"); } int dlen = encodedData.Length; if (dlen == 0 || dlen % 2 == 1) { throw new ArgumentException("encodedData"); } byte[] data = MachineKeySectionUtils.GetBytes(encodedData, dlen); if (data == null || data.Length == 0) { throw new ArgumentException("encodedData"); } var config = WebConfigurationManager.GetWebApplicationSection("system.web/machineKey") as MachineKeySection; byte[] result = null; Exception ex = null; try { switch (protectionOption) { case MachineKeyProtection.All: result = MachineKeySectionUtils.VerifyDecrypt(config, data); break; case MachineKeyProtection.Encryption: result = MachineKeySectionUtils.Decrypt(config, data); break; case MachineKeyProtection.Validation: result = MachineKeySectionUtils.Verify(config, data); break; default: return(MachineKeySectionUtils.GetBytes(encodedData, dlen)); } } catch (Exception e) { ex = e; } if (result == null || ex != null) { throw new HttpException("Unable to verify passed data.", ex); } return(result); }
public static byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { if (encodedData == null) { throw new ArgumentNullException("encodedData"); } if ((encodedData.Length % 2) != 0) { throw new ArgumentException(null, "encodedData"); } byte[] buf = null; try { buf = MachineKeySection.HexStringToByteArray(encodedData); } catch { throw new ArgumentException(null, "encodedData"); } if ((buf == null) || (buf.Length < 1)) { throw new ArgumentException(null, "encodedData"); } if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Encryption)) { buf = MachineKeySection.EncryptOrDecryptData(false, buf, null, 0, buf.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption); if (buf == null) { return(null); } } if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Validation)) { if (buf.Length < MachineKeySection.HashSize) { return(null); } byte[] src = buf; buf = new byte[src.Length - MachineKeySection.HashSize]; Buffer.BlockCopy(src, 0, buf, 0, buf.Length); byte[] buffer3 = MachineKeySection.HashData(buf, null, 0, buf.Length); if ((buffer3 == null) || (buffer3.Length != MachineKeySection.HashSize)) { return(null); } for (int i = 0; i < buffer3.Length; i++) { if (buffer3[i] != src[buf.Length + i]) { return(null); } } } return(buf); }
public static byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { if (encodedData == null) { throw new ArgumentNullException("encodedData"); } if ((encodedData.Length % 2) != 0) { throw new ArgumentException(null, "encodedData"); } byte[] buf = null; try { buf = MachineKeySection.HexStringToByteArray(encodedData); } catch { throw new ArgumentException(null, "encodedData"); } if ((buf == null) || (buf.Length < 1)) { throw new ArgumentException(null, "encodedData"); } if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Encryption)) { buf = MachineKeySection.EncryptOrDecryptData(false, buf, null, 0, buf.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption); if (buf == null) { return null; } } if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Validation)) { if (buf.Length < MachineKeySection.HashSize) { return null; } byte[] src = buf; buf = new byte[src.Length - MachineKeySection.HashSize]; Buffer.BlockCopy(src, 0, buf, 0, buf.Length); byte[] buffer3 = MachineKeySection.HashData(buf, null, 0, buf.Length); if ((buffer3 == null) || (buffer3.Length != MachineKeySection.HashSize)) { return null; } for (int i = 0; i < buffer3.Length; i++) { if (buffer3[i] != src[buf.Length + i]) { return null; } } } return buf; }
public byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { string optionString = protectionOption.ToString(); if (encodedData.StartsWith(optionString, StringComparison.Ordinal)) { encodedData = encodedData.Substring(optionString.Length + 1); } else { throw new Exception("Corrupted data."); } return(Convert.FromBase64String(encodedData)); }
public static byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { if (encodedData == null) throw new ArgumentNullException("encodedData"); if ((encodedData.Length % 2) != 0) throw new ArgumentException(null, "encodedData"); byte[] data = null; try { ////////////////////////////////////////////////////////////////////// // Step 1: Covert the HEX string to byte array data = CryptoUtil.HexToBinary(encodedData); } catch { throw new ArgumentException(null, "encodedData"); } if (data == null || data.Length < 1) throw new ArgumentException(null, "encodedData"); if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption) { ////////////////////////////////////////////////////////////////// // Step 2: Decrypt the data data = MachineKeySection.EncryptOrDecryptData(false, data, null, 0, data.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption); if (data == null) return null; } if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation) { ////////////////////////////////////////////////////////////////// // Step 3a: Remove the hash from the end of the data if (data.Length < MachineKeySection.HashSize) return null; byte[] originalData = data; data = new byte[originalData.Length - MachineKeySection.HashSize]; Buffer.BlockCopy(originalData, 0, data, 0, data.Length); ////////////////////////////////////////////////////////////////// // Step 3b: Calculate the hash and make sure it matches byte[] bHash = MachineKeySection.HashData(data, null, 0, data.Length); if (bHash == null || bHash.Length != MachineKeySection.HashSize) return null; // Sizes don't match for (int iter = 0; iter < bHash.Length; iter++) { if (bHash[iter] != originalData[data.Length + iter]) return null; // Mis-match found } } return data; }
public static byte[] Decode (string encodedData, MachineKeyProtection protectionOption) { if (encodedData == null) throw new ArgumentNullException ("encodedData"); int dlen = encodedData.Length; if (dlen == 0 || dlen % 2 == 1) throw new ArgumentException ("encodedData"); byte[] data = MachineKeySectionUtils.GetBytes (encodedData, dlen); if (data == null || data.Length == 0) throw new ArgumentException ("encodedData"); var config = WebConfigurationManager.GetWebApplicationSection ("system.web/machineKey") as MachineKeySection; byte[] result = null; Exception ex = null; try { switch (protectionOption) { case MachineKeyProtection.All: result = MachineKeySectionUtils.VerifyDecrypt (config, data); break; case MachineKeyProtection.Encryption: result = MachineKeySectionUtils.Decrypt (config, data); break; case MachineKeyProtection.Validation: result = MachineKeySectionUtils.Verify (config, data); break; default: return MachineKeySectionUtils.GetBytes (encodedData, dlen); } } catch (Exception e) { ex = e; } if (result == null || ex != null) throw new HttpException ("Unable to verify passed data.", ex); return result; }
public static string Encode(byte[] data, MachineKeyProtection protectionOption) { if (data == null) { throw new ArgumentNullException("data"); } if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Validation)) { byte[] src = MachineKeySection.HashData(data, null, 0, data.Length); byte[] dst = new byte[src.Length + data.Length]; Buffer.BlockCopy(data, 0, dst, 0, data.Length); Buffer.BlockCopy(src, 0, dst, data.Length, src.Length); data = dst; } if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Encryption)) { data = MachineKeySection.EncryptOrDecryptData(true, data, null, 0, data.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption); } return MachineKeySection.ByteArrayToHexString(data, 0); }
internal static string Serialize(object state, SerializationMode mode, IMachineKey machineKey) { MachineKeyProtection protectionMode = GetMachineKeyProtectionMode(mode); try { // First, need to append the magic header and serialize the object graph byte[] rawBytes; using (MemoryStream ms = new MemoryStream()) { ms.Write(_magicHeader, 0, _magicHeader.Length); SerializeGraph(ms, state); rawBytes = ms.ToArray(); } // Then, encrypt / sign data return(machineKey.Encode(rawBytes, protectionMode)); } catch (Exception ex) { throw CreateSerializationException(ex); } }
public static string Encode(byte[] data, MachineKeyProtection protectionOption) { if (data == null) { throw new ArgumentNullException("data"); } if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Validation)) { byte[] src = MachineKeySection.HashData(data, null, 0, data.Length); byte[] dst = new byte[src.Length + data.Length]; Buffer.BlockCopy(data, 0, dst, 0, data.Length); Buffer.BlockCopy(src, 0, dst, data.Length, src.Length); data = dst; } if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Encryption)) { data = MachineKeySection.EncryptOrDecryptData(true, data, null, 0, data.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption); } return(MachineKeySection.ByteArrayToHexString(data, 0)); }
public static string Encode(byte[] data, MachineKeyProtection protectionOption) { if (data == null) throw new ArgumentNullException("data"); ////////////////////////////////////////////////////////////////////// // Step 1: Get the MAC and add to the blob if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation) { byte[] bHash = MachineKeySection.HashData(data, null, 0, data.Length); byte[] bAll = new byte[bHash.Length + data.Length]; Buffer.BlockCopy(data, 0, bAll, 0, data.Length); Buffer.BlockCopy(bHash, 0, bAll, data.Length, bHash.Length); data = bAll; } if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption) { ////////////////////////////////////////////////////////////////////// // Step 2: Encryption data = MachineKeySection.EncryptOrDecryptData(true, data, null, 0, data.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption); } ////////////////////////////////////////////////////////////////////// // Step 3: Covert the buffer to HEX string and return it return CryptoUtil.BinaryToHex(data); }
public byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { string optionString = protectionOption.ToString(); if (encodedData.StartsWith(optionString, StringComparison.Ordinal)) { encodedData = encodedData.Substring(optionString.Length + 1); } else { throw new Exception("Corrupted data."); } return Convert.FromBase64String(encodedData); }
public byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { return MachineKey.Decode(encodedData, protectionOption); }
public string Encode(byte[] data, MachineKeyProtection protectionOption) { return MachineKey.Encode(data, protectionOption); }
public byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { return(MachineKey.Decode(encodedData, protectionOption)); }
private static byte[] HexDecoder(string input, MachineKeyProtection protection) { Assert.Equal(MachineKeyProtection.All, protection); return HexUtil.HexDecode(input); }
private static string HexEncoder(byte[] data, MachineKeyProtection protection) { Assert.Equal(MachineKeyProtection.All, protection); return HexUtil.HexEncode(data); }
public static string Encode(byte[] data, MachineKeyProtection protectionOption) { return default(string); }
public string Encode(byte[] data, MachineKeyProtection protectionOption) { return protectionOption.ToString() + "-" + Convert.ToBase64String(data); }
public string Encode(byte[] data, MachineKeyProtection protectionOption) { return(MachineKey.Encode(data, protectionOption)); }
public string Encode(byte[] data, MachineKeyProtection protectionOption) { return(protectionOption.ToString() + "-" + Convert.ToBase64String(data)); }
private static byte[] HexDecoder(string input, MachineKeyProtection protection) { Assert.Equal(MachineKeyProtection.All, protection); return(HexUtil.HexDecode(input)); }
private static string HexEncoder(byte[] data, MachineKeyProtection protection) { Assert.Equal(MachineKeyProtection.All, protection); return(HexUtil.HexEncode(data)); }
public string Encode(byte[] data, MachineKeyProtection protectionOption) { #pragma warning disable 0618 // Encode is [Obsolete] in 4.5 return MachineKey.Encode(data, protectionOption); #pragma warning restore 0618 }
public static byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { return default(byte[]); }
public static string Encode(byte[] data, MachineKeyProtection protectionOption) { return(default(string)); }
public byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { #pragma warning disable 0618 // Decode is [Obsolete] in 4.5 return MachineKey.Decode(encodedData, protectionOption); #pragma warning restore 0618 }
public static byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { if (encodedData == null) { throw new ArgumentNullException("encodedData"); } if ((encodedData.Length % 2) != 0) { throw new ArgumentException(null, "encodedData"); } byte[] data = null; try { ////////////////////////////////////////////////////////////////////// // Step 1: Covert the HEX string to byte array data = CryptoUtil.HexToBinary(encodedData); } catch { throw new ArgumentException(null, "encodedData"); } if (data == null || data.Length < 1) { throw new ArgumentException(null, "encodedData"); } if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption) { ////////////////////////////////////////////////////////////////// // Step 2: Decrypt the data data = MachineKeySection.EncryptOrDecryptData(false, data, null, 0, data.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption); if (data == null) { return(null); } } if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation) { ////////////////////////////////////////////////////////////////// // Step 3a: Remove the hash from the end of the data if (data.Length < MachineKeySection.HashSize) { return(null); } byte[] originalData = data; data = new byte[originalData.Length - MachineKeySection.HashSize]; Buffer.BlockCopy(originalData, 0, data, 0, data.Length); ////////////////////////////////////////////////////////////////// // Step 3b: Calculate the hash and make sure it matches byte[] bHash = MachineKeySection.HashData(data, null, 0, data.Length); if (bHash == null || bHash.Length != MachineKeySection.HashSize) { return(null); // Sizes don't match } for (int iter = 0; iter < bHash.Length; iter++) { if (bHash[iter] != originalData[data.Length + iter]) { return(null); // Mis-match found } } } return(data); }
public static byte[] Decode(string encodedData, MachineKeyProtection protectionOption) { return(default(byte[])); }