Sé que se han formulado otras preguntas al respecto, pero hasta ahora no se ha proporcionado ninguna solución o el problema es el que tengo.RijndaelManaged "El relleno no es válido y no se puede eliminar" que solo se produce al descifrar en producción
La clase a continuación maneja el cifrado y descifrado de cadenas, la clave y el vector pasado son SIEMPRE iguales.
Las cadenas que se cifran y se descifran son siempre números, la mayoría funciona, pero la falla ocasional al descifrar (pero solo en el servidor de producción). Debo mencionar que los entornos locales y de producción están en IIS6 en Windows Server 2003, el código que usa la clase se encuentra en un controlador .ashx. El ejemplo que se produce un error en el servidor de producción es "0000232668"
El mensaje de error es
System.Security.Cryptography.CryptographicException: El relleno es válido y no se puede quitar. en System.Security.Cryptography.RijndaelManagedTransform.DecryptData (byte [] INPUTBUFFER, Int32 inputOffset, Int32 inputCount, byte [] & OutputBuffer, Int32 outputOffset, paddingMode PaddingMode, Boolean flast)
Y para el código
public class Aes
{
private byte[] Key;
private byte[] Vector;
private ICryptoTransform EncryptorTransform, DecryptorTransform;
private System.Text.UTF8Encoding UTFEncoder;
public Aes(byte[] key, byte[] vector)
{
this.Key = key;
this.Vector = vector;
// our encyption method
RijndaelManaged rm = new RijndaelManaged();
rm.Padding = PaddingMode.PKCS7;
// create an encryptor and decyptor using encryption method. key and vector
EncryptorTransform = rm.CreateEncryptor(this.Key, this.Vector);
DecryptorTransform = rm.CreateDecryptor(this.Key, this.Vector);
// used to translate bytes to text and vice versa
UTFEncoder = new System.Text.UTF8Encoding();
}
/// Encrypt some text and return a string suitable for passing in a URL.
public string EncryptToString(string TextValue)
{
return ByteArrToString(Encrypt(TextValue));
}
/// Encrypt some text and return an encrypted byte array.
public byte[] Encrypt(string TextValue)
{
//Translates our text value into a byte array.
Byte[] bytes = UTFEncoder.GetBytes(TextValue);
Byte[] encrypted = null;
//Used to stream the data in and out of the CryptoStream.
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(memoryStream, EncryptorTransform, CryptoStreamMode.Write))
{
cs.Write(bytes, 0, bytes.Length);
}
encrypted = memoryStream.ToArray();
}
return encrypted;
}
/// The other side: Decryption methods
public string DecryptString(string EncryptedString)
{
return Decrypt(StrToByteArray(EncryptedString));
}
/// Decryption when working with byte arrays.
public string Decrypt(byte[] EncryptedValue)
{
Byte[] decryptedBytes = null;
using (MemoryStream encryptedStream = new MemoryStream())
{
using (CryptoStream decryptStream = new CryptoStream(encryptedStream, DecryptorTransform, CryptoStreamMode.Write))
{
decryptStream.Write(EncryptedValue, 0, EncryptedValue.Length);
}
decryptedBytes = encryptedStream.ToArray();
}
return UTFEncoder.GetString(decryptedBytes);
}
/// Convert a string to a byte array. NOTE: Normally we'd create a Byte Array from a string using an ASCII encoding (like so).
// System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
// return encoding.GetBytes(str);
// However, this results in character values that cannot be passed in a URL. So, instead, I just
// lay out all of the byte values in a long string of numbers (three per - must pad numbers less than 100).
public byte[] StrToByteArray(string str)
{
if (str.Length == 0)
throw new Exception("Invalid string value in StrToByteArray");
byte val;
byte[] byteArr = new byte[str.Length/3];
int i = 0;
int j = 0;
do
{
val = byte.Parse(str.Substring(i, 3));
byteArr[j++] = val;
i += 3;
}
while (i < str.Length);
return byteArr;
}
// Same comment as above. Normally the conversion would use an ASCII encoding in the other direction:
// System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
// return enc.GetString(byteArr);
public string ByteArrToString(byte[] byteArr)
{
byte val;
string tempStr = "";
for (int i = 0; i <= byteArr.GetUpperBound(0); i++)
{
val = byteArr[i];
if (val < (byte)10)
tempStr += "00" + val.ToString();
else if (val < (byte)100)
tempStr += "0" + val.ToString();
else
tempStr += val.ToString();
}
return tempStr;
}
EDIT: Gracias por toda su ayuda, sin embargo, sus respuestas no resuelven el problema, que resultó ser algo estúpidamente simple. Estaba generando una cadena encriptada en un servidor y entregándola a un controlador en otro servidor para su desclasificación y procesamiento, pero resulta que los resultados del cifrado difieren cuando se ejecutan en diferentes servidores, por lo tanto, el servidor receptor no pudo descifrarlo. Una de las respuestas tropezó con la sugerencia de esto por accidente, y es por eso que la acepté
Err, sí, lo siento por los comentarios, es "código prestado" de otra pregunta SO. "¿De dónde vino?" ¿La cadena o el código? –
La cadena. Me alegra que sea un código prestado, no tengo que pisar tan cuidadosamente ... –
¿Por qué funcionaría en el entorno local si ese es el caso? No cuestionar su respuesta, simplemente parece extraño –