Domanda

vorrei per decifrare un file che ho precedentemente crittografati con C # utilizzando il TripleDESCryptoServiceProvider.
Ecco il mio codice per la crittografia:

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();
    }

Ho già trovato la biblioteca Crypto ++ ed è riuscito a costruire e collegarlo. Così ho provato a decifrare il file che è stato memorizzato con C # dopo la crittografia con il seguente codice C ++ (nativo):

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);

Questo codice non sarà decifrare correttamente. Il risultato dopo la decrittazione non corrisponde al testo che è stato cifrato in precedenza. Qualche idea per il mio codice?

È stato utile?

Soluzione 2

Sono riuscito a fare questo compito con Windows Crypto API come indicato in mio post .

Altri suggerimenti

Si può cifrare e decifrare in C ++? Si può cifrare e decifrare in C #?

Sei sicuro che si sta utilizzando la stessa modalità, imbottitura e cifrare, decifrare la sequenza?

tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

Prova modalità CBC (modalità predefinita del TripleDESCryptoServiceProvider)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top