Bagikan melalui


Mengimpor Kunci Umum Driver

[Fitur yang terkait dengan halaman ini, DirectShow, adalah fitur warisan. Ini telah digantikan oleh MediaPlayer, IMFMediaEngine, dan Tangkapan Audio/Video di Media Foundation. Fitur-fitur tersebut telah dioptimalkan untuk Windows 10 dan Windows 11. Microsoft sangat menyarankan agar kode baru menggunakan MediaPlayer, IMFMediaEngine dan Audio/Video Capture di Media Foundation alih-alih DirectShow, jika memungkinkan. Microsoft menyarankan agar kode yang ada yang menggunakan API warisan ditulis ulang untuk menggunakan API baru jika memungkinkan.]

Kunci umum RSA driver terkandung dalam tag Modulus dan Eksponen dari node daun sertifikat. Kedua nilai dikodekan base64 dan harus didekodekan. Jika Anda menggunakan CryptoAPI Microsoft, Anda harus mengimpor kunci ke penyedia layanan kriptografi (CSP), yang merupakan modul yang mengimplementasikan algoritma kriptografi.

Untuk mengonversi modulus dan eksponen dari pengodean base64 ke array biner, gunakan fungsi CryptStringToBinary , seperti yang ditunjukkan dalam kode berikut. Panggil fungsi sekali untuk mendapatkan ukuran array byte. Kemudian alokasikan buffer dan panggil fungsi lagi.

DWORD cbLen = 0, dwSkip = 0, dwFlags = 0;
::CryptStringToBinary(
   pszModulus,  // String that contains the Base64-encoded modulus.
   cchModulus,  // Length of the string, not including the trailing NULL.
   CRYPT_STRING_BASE64,  // Base64 encoding.
   NULL,     // Do not convert yet. Just calculate the length.
   &cbLen,   // Receives the length of the buffer that is required.
   &dwSkip,  // Receives the number of skipped characters.
   &dwFlags  // Receives flags.
);

// Allocate a new buffer.
BYTE *pbBuffer = new BYTE [cbLen];
::CryptStringToBinary(pszModulus, cchModulus, CRYPT_STRING_BASE64, 
    pbBuffer, &cbLen, &dwSkip, &dwFlags);

// (Repeat these steps for the exponent.)

Array yang dikodekan base64 berada dalam urutan big-endian, sedangkan CryptoAPI mengharapkan jumlah dalam urutan little-endian, jadi Anda perlu menukar urutan byte array yang dikembalikan dari CryptStringToBinary. Modulus adalah 256 byte, tetapi array byte yang didekodekan mungkin kurang dari 256 byte. Jika demikian, Anda harus mengalokasikan array baru yaitu 256 byte, menyalin data ke dalam array baru, dan mengalokasikan bagian depan array dengan nol. Eksponen adalah nilai DWORD (4-byte).

Setelah Anda memiliki nilai modulus dan eksponen, Anda dapat mengimpor kunci ke penyedia layanan kriptografi default (CSP), seperti yang ditunjukkan dalam kode berikut:

// Assume the following values exist:
BYTE *pModulus;     // Byte array that contains the modulus.
DWORD cbModulus;    // Size of the modulus in bytes.
DWORD dwExponent;   // Exponent.

// Create a new key container to hold the key. 
::CryptAcquireContext(
    &hCSP,         // Receives a handle to the CSP.
    NULL,          // Use the default key container.
    NULL,          // Use the default CSP.
    PROV_RSA_AES,  // Use the AES provider (public-key algorithm).
    CRYPT_SILENT | CRYPT_NEWKEYSET 
);

// Move the key into the key container. 
// The data format is: PUBLICKEYSTRUC + RSAPUBKEY + key
DWORD cbKeyBlob = cbModulus + sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY)
BYTE *pBlob = new BYTE[cbKeyBlob];

// Fill in the data.
PUBLICKEYSTRUC *pPublicKey = (PUBLICKEYSTRUC*)pBlob;
pPublicKey->bType = PUBLICKEYBLOB; 
pPublicKey->bVersion = CUR_BLOB_VERSION;  // Always use this value.
pPublicKey->reserved = 0;                 // Must be zero.
pPublicKey->aiKeyAlg = CALG_RSA_KEYX;     // RSA public-key key exchange. 

// The next block of data is the RSAPUBKEY structure.
RSAPUBKEY *pRsaPubKey = (RSAPUBKEY*)(pBlob + sizeof(PUBLICKEYSTRUC));
pRsaPubKey->magic = RSA1;            // Public key.
pRsaPubKey->bitlen = cbModulus * 8;  // Number of bits in the modulus.
pRsaPubKey->pubexp = dwExponent;     // Exponent.

// Copy the modulus into the blob. Put the modulus directly after the
// RSAPUBKEY structure in the blob.
BYTE *pKey = (BYTE*)(pRsaPubkey + sizeof(RSAPUBKEY));
CopyMemory(pKey, pModulus, cbModulus);

// Now import the key.
HCRYPTKEY hRSAKey;  // Receives a handle to the key.
CryptImportKey(hCSP, pBlob, cbKeyBlob, 0, 0, &hRSAKey) 

Sekarang Anda dapat menggunakan CryptoAPI untuk mengenkripsi perintah dan permintaan status dengan kunci umum driver.

Menggunakan Certified Output Protection Protocol (COPP)