كيف يمكنني فك تشفير ملف مع Crypto ++ تم تشفيره بـ C#

StackOverflow https://stackoverflow.com/questions/2192584

  •  25-09-2019
  •  | 
  •  

سؤال


أرغب في فك تشفير ملف قمت بتشفيره مسبقًا مع C# باستخدام TripledesCryptoserviceProvider.
هذا هو رمز التشفير:

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

لقد وجدت بالفعل مكتبة Crypto ++ وتمكنت من إنشاءها وربطها. لذلك حاولت فك تشفير الملف الذي تم تخزينه باستخدام C# بعد التشفير باستخدام رمز C ++ التالي:

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

هذا الرمز لن فك تشفير بشكل صحيح. النتيجة بعد فك التشفير لا تتطابق مع النص العادي الذي تم تشفيره مسبقًا. أي فكرة عن الكود الخاص بي؟

هل كانت مفيدة؟

المحلول 2

تمكنت من القيام بهذه المهمة مع Windows Crypto API كما هو مذكور في منشوري الآخر.

نصائح أخرى

هل يمكنك تشفير وفك التشفير في C ++؟ هل يمكنك تشفير وفك التشفير في C#؟

هل أنت متأكد من أنك تستخدم نفس الوضع والحشو والتشفير ، فك تشفير التسلسل؟

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

جرب وضع CBC (الوضع الافتراضي لـ TripledesCryptoserviceProvider)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top