Create a digital signature with C ++ Builder

zhaozj2021-02-11  188

Create a digital signature with C Builder

If you pass a data on the network, there is a unsafe factor, so that you can reach the destination for the data, and you can add a digital signature to the data. This allows the other party to check if the data you have passed is modified by others by verifying the signature.

I. The principle of program principles is still relatively simple. It is based on the original data you provide. After a complex algorithm, generate a specific data signature, the other party also generates a signature, if the data has been modified, Then it is impossible to get two models of exactly the same signature, so that the data has been modified by others. Programmaker uses Windows's CAPI interface to achieve encryption, decryption, and digital signatures of data.

Second, the program list

Let's take a look at its specific implementation process by following the statement of C Builder. First create a digital signature, assume that its data comes from a file.

// Variable declaration: HcryptProv HProv; // CSP Handle Hcrypthash Hhash; // Handle const amount = 4096; // Buffer size constant Byte PBuffer [buffer]; // Store read file content buffer BYTE Psignature [256]; // Store Signed Buffer Dword Dsignaturelen = 256; // Signed Length TFileStream * SourceFile; // A File Flow IF (! CryptacquiRecontext (& HPROV, NULL, NULL, PROV-RSA-FULL, 0) ) / / Connect the default CSP, accept its handle to put the HPROV {// error handling} if (! CryptCreateHash (HPROV, CALG-MD5, 0, & Hhash)) // Create a hash object, get it Handle is placed in hhash {// error handling} do {dreadlen = sourcefile-> read (pBuffer, buffer); if (! CryptHashData (Hhash, PBuffer, DREADLEN, 0)) // calculates the file according to the contents of the file {/ / Error handling}} WHILE (! (! "); If (! Cryptsignhash (hhash, at-signal, null, 0, psignature, & dsignatelen) // use private key to digitally sign the hash value // Signature data is placed in Psignature, and the length is placed in DSIGNATURELEN / / error handling}

转载请注明原文地址:https://www.9cbs.com/read-4913.html

New Post(0)