¿Cómo puedo descifrar un archivo con Crypto ++ que se cifra con C #
-
25-09-2019 - |
Pregunta
Hola, Me gustaría descifrar un archivo que previamente me cifrada con C # utilizando el TripleDESCryptoServiceProvider.
Aquí está mi código para cifrar:
private static void EncryptData(MemoryStream streamToEncrypt)
{
// initialize the encryption algorithm
TripleDES algorithm = new TripleDESCryptoServiceProvider();
byte[] desIV = new byte[8];
byte[] desKey = new byte[16];
for (int i = 0; i < 8; ++i)
{
desIV[i] = (byte)i;
}
for (int j = 0; j < 16; ++j)
{
desKey[j] = (byte)j;
}
FileStream outputStream = new FileStream(TheCryptedSettingsFilePath, FileMode.OpenOrCreate, FileAccess.Write);
outputStream.SetLength(0);
CryptoStream encStream = new CryptoStream(outputStream, algorithm.CreateEncryptor(desKey, desIV),
CryptoStreamMode.Write);
// write the encrypted data to the file
encStream.Write(streamToEncrypt.ToArray(), 0, (int)streamToEncrypt.Length);
encStream.Close();
outputStream.Close();
}
Ya encontró la biblioteca Crypto ++ y logró construir y vincularlo. Así que traté de descifrar el archivo que se almacena con C # después de que el cifrado con el código (nativo) C ++ siguiente:
FILE *fp;
long len;
char *buf;
if (_wfopen_s(&fp, _T("MyTest.bin"), _T("rb")) != 0)
{
return false;
}
fseek(fp ,0 ,SEEK_END); //go to end
len = ftell(fp); //get position at end (length)
fseek(fp, 0, SEEK_SET); //go to beg.
buf = (char *)malloc(len); //malloc buffer
fread(buf, len, 1, fp); //read into buffer
fclose(fp);
BYTE pIV[] = {0, 1, 2, 3, 4, 5, 6, 7};
BYTE pKey[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
const BYTE* lpData = (const BYTE*)(LPCTSTR)buf;
size_t bufferSize = strlen(buf);
BYTE* result = (BYTE *)malloc(bufferSize);
CFB_FIPS_Mode<DES_EDE2>::Decryption decryption_DES_EDE2_CFB;
decryption_DES_EDE2_CFB.SetKeyWithIV(pKey, sizeof(pKey), pIV, sizeof(pIV));
decryption_DES_EDE2_CFB.ProcessString(result, lpData, bufferSize);
Ese código no descifrará correctamente. El resultado después de la descodificación no coincide con el texto sin formato que se ha cifrado previamente. Cualquier idea acerca de mi código?
Solución 2
Me las arreglé para hacer esa tarea con Crypto API de Windows como se indica en mi otro post .
Otros consejos
¿Se puede cifrar y descifrar en C ++? Se puede cifrar y descifrar en c #?
¿Está seguro de que está utilizando el mismo modo, el relleno y cifrar, descifrar la secuencia?
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
Trate modo CBC (modo por defecto del TripleDESCryptoServiceProvider)