来源:https://blog.csdn.net/mmpire/article/details/1640670 标签:Windows,crypto API,加密,解密 收藏:株野 作者:kamaliang 日期:2007年06月06日 14:30:00 #include <stdio.h> #include <windows.h> #include <wincrypt.h> #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING) #define KEYLENGTH 0x00800000 void HandleError(char *s); //-------------------------------------------------------------------- // These additional #define statements are required. #define ENCRYPT_ALGORITHM CALG_RC4 #define ENCRYPT_BLOCK_SIZE 8 #define MAX_FILE_SIZE 4000000 #define SIGNATURE_SIZE 500 BYTE *pbKeyBlob; //用来保存导出的公钥 DWORD dwBlobLen; // Declare the functions. The function definition // follows main. BOOL VerifyFile ( PCHAR szSource, PCHAR szDestination); BOOL SignFile ( PCHAR szSource, PCHAR szDestination); BOOL DecryptFile( PCHAR szSource, PCHAR szDestination, PCHAR szPassword); BOOL EncryptFile( PCHAR szSource, PCHAR szDestination, PCHAR szPassword); //-------------------------------------------------------------------- // Begin main. void main(void) { CHAR szSource[100]; CHAR szDestination[100]; CHAR szPassword[100]; //-------------------------------------------------------------------- // Call EncryptFile to do the actual encryption. 加密文件 printf("/n------------------------------------------------------------/n"); printf("/n/n1.Encrypt a file. /n/n"); printf("/nEnter the name of the file to be encrypted: "); scanf("%s",szSource); printf("/nEnter the name of the output file: "); scanf("%s",szDestination); printf("/nEnter the password:"); scanf("%s",szPassword); if(EncryptFile(szSource, szDestination, szPassword)) { printf("/nEncryption of the file %s was a success. /n", szSource); printf("/nThe encrypted data is in file %s./n",szDestination); } else { HandleError("/nError encrypting file!"); } //-------------------------------------------------------------------- // Call Decryptfile to do the actual decryption. 解密文件 printf("/n------------------------------------------------------------/n"); printf("/n/n2.Decrypt a file. /n/n"); printf("/nEnter the name of the file to be decrypted: "); scanf("%s",szSource); printf("/nEnter the name of the output file: "); scanf("%s",szDestination); printf("/nEnter the password:"); scanf("%s",szPassword); if(DecryptFile(szSource, szDestination, szPassword)) { printf("/nDecryption of the file %s was a success. /n", szSource); printf("/nThe decrypted data is in file %s./n",szDestination); } else { HandleError("/nError decrypting file!"); } //-------------------------------------------------------------------- // Call SignFile to do the actual signature 签名文件 printf("/n------------------------------------------------------------/n"); printf("/n/n3.Sign a file. /n/n"); printf("/nEnter the name of the file to be signed: "); scanf("%s",szSource); printf("/nEnter the name of the signature file: "); scanf("%s",szDestination); if(SignFile(szSource, szDestination)) { printf("/nSignature of the file %s was a success. /n", szSource); printf("/nThe signature data is in file %s./n",szDestination); } else { HandleError("/nError while signing the file!"); } //--------------------------------------------------------------------- // Call VerifyFile to do the actual verification 验证签名 printf("/n------------------------------------------------------------/n"); printf("/n/n4.Verify a file and its signature. /n/n"); printf("/nEnter the name of the file to be verified: "); scanf("%s",szSource); printf("/nEnter the name of the signature file: "); scanf("%s",szDestination); //printf("/nEnter the name of the public key file: "); //scanf("%s",szDestination); if(VerifyFile(szSource, szDestination)) { printf("/nVerification of the file %s was a success. /n", szSource); } else { HandleError("/nVerification failed. Error file!"); } } // End of main
|