Click here to Skip to main content
15,921,989 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to access application's extern variable inside a dll Pin
Richard MacCutchan16-Dec-11 22:42
mveRichard MacCutchan16-Dec-11 22:42 
QuestionWhich function can I use instead of ShowControlBar Pin
adityarao3115-Dec-11 19:42
adityarao3115-Dec-11 19:42 
AnswerRe: Which function can I use instead of ShowControlBar Pin
Resmi Anna15-Dec-11 21:45
Resmi Anna15-Dec-11 21:45 
QuestionDES Encryption Pin
jkirkerx15-Dec-11 19:12
professionaljkirkerx15-Dec-11 19:12 
SuggestionRe: DES Encryption Pin
Code-o-mat15-Dec-11 22:20
Code-o-mat15-Dec-11 22:20 
GeneralRe: DES Encryption Pin
jkirkerx16-Dec-11 6:40
professionaljkirkerx16-Dec-11 6:40 
GeneralRe: DES Encryption Pin
Code-o-mat16-Dec-11 7:01
Code-o-mat16-Dec-11 7:01 
AnswerRe: DES Encryption Pin
Richard MacCutchan15-Dec-11 23:40
mveRichard MacCutchan15-Dec-11 23:40 
AnswerRe: DES Encryption Pin
Richard MacCutchan16-Dec-11 1:16
mveRichard MacCutchan16-Dec-11 1:16 
GeneralRe: DES Encryption Pin
jkirkerx16-Dec-11 7:19
professionaljkirkerx16-Dec-11 7:19 
GeneralRe: DES Encryption Pin
Richard MacCutchan16-Dec-11 7:40
mveRichard MacCutchan16-Dec-11 7:40 
GeneralRe: DES Encryption Pin
Randor 16-Dec-11 7:59
professional Randor 16-Dec-11 7:59 
GeneralRe: DES Encryption Pin
jkirkerx16-Dec-11 8:12
professionaljkirkerx16-Dec-11 8:12 
GeneralRe: DES Encryption Pin
jkirkerx16-Dec-11 9:00
professionaljkirkerx16-Dec-11 9:00 
GeneralRe: DES Encryption Pin
Randor 16-Dec-11 9:41
professional Randor 16-Dec-11 9:41 
GeneralRe: DES Encryption Pin
jkirkerx16-Dec-11 10:31
professionaljkirkerx16-Dec-11 10:31 
GeneralRe: DES Encryption Pin
Randor 16-Dec-11 11:04
professional Randor 16-Dec-11 11:04 
GeneralRe: DES Encryption Pin
jkirkerx16-Dec-11 12:06
professionaljkirkerx16-Dec-11 12:06 
GeneralRe: DES Encryption Pin
Randor 16-Dec-11 13:25
professional Randor 16-Dec-11 13:25 
GeneralRe: DES Encryption Pin
jkirkerx17-Dec-11 8:05
professionaljkirkerx17-Dec-11 8:05 
GeneralRe: DES Encryption Pin
Randor 17-Dec-11 9:47
professional Randor 17-Dec-11 9:47 
GeneralRe: DES Encryption Pin
jkirkerx17-Dec-11 20:49
professionaljkirkerx17-Dec-11 20:49 
GeneralRe: DES Encryption Pin
Randor 18-Dec-11 3:55
professional Randor 18-Dec-11 3:55 
GeneralRe: DES Encryption Pin
jkirkerx18-Dec-11 8:32
professionaljkirkerx18-Dec-11 8:32 
GeneralRe: DES Encryption Pin
Randor 18-Dec-11 11:29
professional Randor 18-Dec-11 11:29 
Sorry for the late reply, I was watching the New Orleans Saints defeat the Minnesota Vikings.

jkirkerx wrote:
.Padding = PaddingMode.PKCS7


This is one of the reasons why you are getting a different result in your C++ code. I believe the C++ code is defaulting to PKCS5. Your .NET code is using PKCS7.

You can confirm this by querying your key parameters in your C++ project before you perform the encryption:

C++
DWORD dwPadding = 0;
DWORD dwLength = sizeof(DWORD);
bResult = CryptGetKeyParam(hKey,KP_PADDING,(BYTE *)&dwPadding,&dwLength,0);

Yes... the wierd casts to BYTE * are necessary... these Microsoft cryptography functions are designed to accept BYTE * even though some calls want DWORD *
The value of dwPadding after this call will be 0x1 which == PKCS5_PADDING confirming that your C++ code defaults to PKCS5.


Everything in here must match your C++ code and vice-versa.
Your VB Code:
VB
Dim cryptoProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider()
With cryptoProvider
    .BlockSize = 64
    .FeedbackSize = 8
    .Key = KEY_64
    .IV = IV_64
    .Mode = CipherMode.CBC
    .Padding = PaddingMode.PKCS7
End With


In your C++ code you need to set these parameters to exactly match your .NET implementation.... here are a few:

Set mode to CBC:
C++
DWORD dwMode = CRYPT_MODE_CBC;
bResult = CryptSetKeyParam(hKey, KP_MODE,(BYTE *)&dwMode, 0);


Set the Padding:
C++
DWORD dwPadding = PKCS5_PADDING;
bResult = CryptSetKeyParam(hKey, KP_PADDING,(BYTE *)&dwPadding, 0);


Set the initialization vector:
C++
BYTE IV_64[8] = {1, 2, 3, 4, 5, 6, 7, 8}; //Replace my magic numbers with your own
CryptSetKeyParam(hKey, KP_IV, IV_64, 0);


Set the feedback:
C++
DWORD dwFeedback = 8;
bResult = CryptSetKeyParam(hKey, KP_MODE_BITS,(BYTE *)&dwFeedback, 0);

By the way... I am fairly certain that setting the feedback value has no effect when using the CBC mode...

Set the Block length:
C++
DWORD dwBlockLength = 64;
bResult = CryptSetKeyParam(hKey, KP_BLOCKLEN,(BYTE *)&dwBlockLength, 0);


If you get all of the settings to match... the values you get out of the C++ code should perfectly match the values you get from the .NET code.

Btw, I don't think the default Microsoft provider supports PKCS7 padding... I believe the .NET framework uses its own provider. You will probably need to change your .NET padding to something both providers support.

Thats all I can think of for now.

Best Wishes,
-David Delaune

modified 18-Dec-11 17:51pm.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.