2009-04-01 3 views
1

다음 코드에서 생성 일치하지 않습니다관리 SIMPLEBLOB의 덩어리가 관리되지 않는 SIMPLEBLOB (RSA -Cryptography)

CryptExportKey(hKey, hPublicKey, SIMPLEBLOB, 0, lpData, &nSize); 

를 사용하여 생성 된 간단한 방울과 일치하지 않는 (즉, client.key가 알기주의 http://www.codeproject.com/KB/security/plaintextsessionkey.aspx을 사용하여 hKey의 텍스트 키 값을 찾음)

CspParameters cspParams = new CspParameters();    
cspParams.KeyContainerName = "Container Name";    
cspParams.KeyNumber = (int)KeyNumber.Exchange; 
cspParams.ProviderType = 1;    
cspParams.ProviderName = "Microsoft Enhanced Cryptographic Provider v1.0";   cspParams.Flags = CspProviderFlags.UseMachineKeyStore;    

RSACryptoServiceProvider rsaClient = new RSACryptoServiceProvid(cspParams);    

    rsaClient.ImportCspBlob(File.ReadAllBytes(@"C:\client.key"));//Generate a SIMPLEBLOB session key 

byte[] session = GetRC4SessionBlobFromKey(keyMaterial, rsaClient);//Encrypt a key using public key and write it in a SIMPLEBLOB format 


    public byte[] GetRC4SessionBlobFromKey(byte[] keyData, RSACryptoServiceProvider publicKey)   
{    
using(MemoryStream ms = new MemoryStream())    
using(BinaryWriter w = new BinaryWriter(ms))    
{     
w.Write((byte) 0x01); // SIMPLEBLOB      
w.Write((byte) 0x02); // Version 2      
w.Write((byte) 0x00); // Reserved      
w.Write(0x00006801); // ALG_ID = RC4 for the encrypted key.     
w.Write(0x0000a400); // CALG_RSA_KEYX      
w.Write(publicKey.Encrypt(keyData, false));     
w.Flush();     

return ms.ToArray();    
}   
} 

왜 그런가요?

답변

0
  1. original answer의 코드가 올바르지 않습니다. 암호화 된 바이트를 되돌려 야합니다. 문제를 해결할 수있는 대답을 업데이트했습니다.

  2. 위의 코드에는 "w.Write((byte) 0x00); // Reserved"이 누락되었습니다. 원래의 대답에 그 라인이 반복되는 것은 실수가 아니 었습니다.

  3. RSA PKCS # 1 암호화 (SIMPLEBLOB 내에서 사용됨)는 결정적이지 않습니다. I.E. 동일한 데이터를 두 번 암호화하면 동일한 결과를 얻지 못할 것입니다.

그래서 정리해 : (

public byte[] GetRC4SessionBlobFromKey(byte[] keyData, RSACryptoServiceProvider publicKey)   
{    
    using(MemoryStream ms = new MemoryStream())    
    using(BinaryWriter w = new BinaryWriter(ms))    
    {     
    w.Write((byte) 0x01); // SIMPLEBLOB      
    w.Write((byte) 0x02); // Version 2      
    w.Write((byte) 0x00); // Reserved      
    w.Write((byte) 0x00); // Reserved      
    w.Write(0x00006801); // ALG_ID = RC4 for the encrypted key.     
    w.Write(0x0000a400); // CALG_RSA_KEYX      
    byte[] encryptedKey = publicKey.Encrypt(key.Key); 
    byte[] reversedEncryptedKey = new byte[encryptedKey.Length]; 
    for(int i=0;i<encryptedKey.Length;i++){ 
     reversedEncryptedKey[i] = encryptedKey[encryptedKey.Length - 1 - i]; 
    } 
    w.Write(reversedEncryptedKey); // encrypted key in LSB byte order 

    w.Flush();     

    return ms.ToArray();    
    }   
} 

하고는 SIMPLEBLOB을 가져 CryptImportKey()를 사용하는 대신 CryptExportKey과 결과를 비교하는 시도하여 작동하는지 확인 : 위의 코드를 변경하려면).

+0

실제 SIMPLEBLOB 콘텐츠를 첨부하거나 복사 하시겠습니까? 나는 그걸 할 수있어. –

+0

예, CryptExportKey() - blob 및 GetRC4SessionBlobFromKey() - blob의 내용을 포함하십시오. (16 진수 또는 64 진수 또는 기타). –

+0

안녕 Rasmus, 지연된 응답을 보내 주셔서 감사 드리며 답장을 보내 주셔서 감사합니다. 조금만 벗어 났으며 오늘까지 사용자의 의견을 검토하지 않았습니다. 설명 텍스트 상자에 300자를 초과하거나 파일을 업로드 할 수 없습니다.다른 방법 (이메일 등)이 있으면 알려 주시기 바랍니다. 감사합니다 –

0

나는 내 자신의 질문을 편집 할 수 없으므로이 답변에 여기에 첨부하고 싶은 콘텐츠를두고 있습니다. Visual Studio의 이진 편집기/바로 가기 창 아래에있는 이미지 캡처 화면이 있습니다.

CryptExportKey - 디버그 창에 SIMPLEBLOB

CryptExportKey - SIMPLEBLOB http://img14.imageshack.us/img14/1926/cryptoexportkeysimplebl.jpg

KeyMaterial 값

Key Material value in debug window http://img19.imageshack.us/img19/4138/keymaterialdebugwindow.jpg

코드 프로젝트 기사 GetRC4SessionBlobFromKey()

Session key value from GetRC4SessionBlobFromKey http://img206.imageshack.us/img206/5620/sessionvaluefromgetrc4s.jpg

(쉼표로 구분하여 개별 바이트의 정수 값)에서

Key Material value that is saved in file using the code project article http://img243.imageshack.us/img243/7936/keymaterialfile.jpg

세션 키 값을 사용하여 파일에 저장됩니다

주요 재료 값

이 문제를 조사해 주셔서 감사 드리며 제공 할 수있는 추가 정보가 있으면 알려 주시기 바랍니다.

+0

정말 사이트에 등록해야합니다. 반 익명 프로필을 사용하면 많은 문제가 발생합니다. 즉, 나는 나의 대답을 편집했다. 희망이 도움이됩니다. –

관련 문제