打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用Microsoft CryptoAPI进行加密、解密、签名及验证(代码)
来源: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
              
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
21天学通C语言(第六版)
一个计算星期几的简单程序
Delphi 如何给printf和scanf类型的函数传递可变参数
RC4算法
142.抛物样条曲线
实验六 文件存储空间管理模拟--位示图
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服