Back to "Más sobre mi viaje al maravilloso mundo de ASP.NET 1.x ViewState"

This is a viewer only at the moment see the article on how this works.

To update the preview hit Ctrl-Alt-R (or ⌘-Alt-R on Mac) or Enter to refresh. The Save icon lets you save the markdown file to disk

This is a preview from the server running through my markdig pipeline

ASP.NET Imported mostlylucidcouk Security

Más sobre mi viaje al maravilloso mundo de ASP.NET 1.x ViewState

Sunday, 29 February 2004

Escribí en un Puesto anterior que yo estaba planeando tener un husmear alrededor con Estado de vista, extendiendo las cosas que he hecho en la compresión en la encriptación y firma, etc... bueno hay buenas y malas noticias para este plan. Parece (oddly) que el MAC y material de encriptación se maneja realmente en la clase LosWriter; una clase interna utilizada por LosFormatter para hacer realmente la serialización de objetos... utiliza el siguiente código para hacer esto (gracias una vez más a la indispensable Reflector) :

internal void CompleteTransforms(TextWriter output, bool enableMac, byte[] macKey)
{ int num1;
byte[] array1;
string text1;
char[] array2;
string text2;
num1 = 0;
if (this._recyclable)
{
 this._byteBuffer = ((byte[]) LosWriter._byteBufferAllocator.GetBuffer());
if (this._freePos > 0)
{
 num1 = Encoding.UTF8.GetBytes(this._charBuffer, 0, this._freePos, this._byteBuffer, 0);
 
}
if (enableMac)
{
 array1 = MachineKey.GetEncodedData(this._byteBuffer, macKey, 0, &(num1));
text1 = Convert.ToBase64String(array1, 0, num1);
output.Write(text1);
return; 
}
array2 = ((char[]) LosWriter._charBufferAllocatorBase64.GetBuffer());
num1 = Convert.ToBase64CharArray(this._byteBuffer, 0, num1, array2, 0);
output.Write(array2, 0, num1);
LosWriter._charBufferAllocatorBase64.ReuseBuffer(array2);
return; 
}
this._byteBuffer = Encoding.UTF8.GetBytes(this._charBuffer, 0, this._freePos);
num1 = this._byteBuffer.Length;
if (enableMac)
{
 this._byteBuffer = MachineKey.GetEncodedData(this._byteBuffer, macKey, 0, &(num1));
 
}
text2 = Convert.ToBase64String(this._byteBuffer);
output.Write(text2);
 
}

Como usted puede ver probablemente, este código maneja la aplicación de la clave MAC y el cifrado a los datos seriados. Este es el código utilizado por la 'MachineKey' para realizar el cifrado:

internal static byte[] EncryptOrDecryptData(bool fEncrypt, byte[] buf, byte[] modifier, int start, int length)
{ MemoryStream stream1;
ICryptoTransform transform1;
CryptoStream stream2;
byte[] array1;
byte[] array2;
MachineKey.EnsureConfig();
stream1 = new MemoryStream();
transform1 = MachineKey.GetCryptoTransform(fEncrypt);
stream2 = new CryptoStream(stream1, transform1, 1);
stream2.Write(buf, start, length);
if (fEncrypt && (modifier != null))
{
 stream2.Write(modifier, 0, modifier.Length);
 
}
stream2.FlushFinalBlock();
array1 = stream1.ToArray();
stream2.Close();
MachineKey.ReturnCryptoTransform(fEncrypt, transform1);
if (!fEncrypt && (modifier != null))
{
 array2 = new byte[((uint) (array1.Length - modifier.Length))];
Buffer.BlockCopy(array1, 0, array2, 0, array2.Length);
array1 = array2;
 
}
return array1; 
} 

Desafortunadamente, esto significa que voy a tener que jugar con la reflexión / volcar la fuente de Refelector / completamente reinventar cómo funciona esto para ir mucho más lejos con mi toqueteo ViewState, Dios habría sido bueno si estas cosas se hicieron con una fábrica originalmente ... supone que no pensaban que nadie estaría lo suficientemente loco como para querer jugar con él ...:-)

Con el tiempo voy a tener que ceder y realmente escribir un artículo uno de estos días ... el problema para mí es estructurar la cosa por lo que es legible para los humanos no-obsesivos ...

logo

© 2026 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.