求助关于linux系统C语言使用opensll进行RSA分段加密

nuaamajia 2015-03-13 10:16:02

使用RSA_PKCS1_PADDING,传117字节进行加密,但是返回的密文长度不一定是128。

将完整的原始数据分段加密再拼成一个加密串以后,如何进行分段解密呢。。。。



...全文
347 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
iTTot_Wang 2015-11-04
  • 打赏
  • 举报
回复
楼主,能把你的分段加解密的代码粘出来学习一下吗?
nuaamajia 2015-03-16
  • 打赏
  • 举报
回复
自己搞定了,加密结果其实是固定的128位,但是我很2的使用sprintf()来拼接密文~~应该使用memcpy()。
060 2015-03-15
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
又来一个找不到OpenSSL合适例子代码的。
OpenSLL的文档、例子太差劲了,而且没有中文的。
nuaamajia 2015-03-13
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
又来一个找不到OpenSSL合适例子代码的。
ORZ~
nuaamajia 2015-03-13
  • 打赏
  • 举报
回复
我直接较长的字符串进行加密的,不用读写文件。用的RSA_private_decrypt(),RSA_public_encrypt()。老师有类似的例子么。
赵4老师 2015-03-13
  • 打赏
  • 举报
回复
仅供参考:
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "advapi32.lib")
#define _WIN32_WINNT 0x0400
#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
#define KEYLENGTH  0x00800000
void HandleError(char *s);
//--------------------------------------------------------------------
//  These additional #define statements are required.
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 8
//   Declare the function EncryptFile. The function definition
//   follows main.
BOOL EncryptFile(
    PCHAR szSource,
    PCHAR szDestination,
    PCHAR szPassword);
//--------------------------------------------------------------------
//   Begin main.
void main(void) {
    CHAR szSource[100];
    CHAR szDestination[100];
    CHAR szPassword[100];
    printf("Encrypt a file. \n\n");
    printf("Enter the name of the file to be encrypted: ");
    scanf("%s",szSource);
    printf("Enter the name of the output file: ");
    scanf("%s",szDestination);
    printf("Enter the password:");
    scanf("%s",szPassword);
    //--------------------------------------------------------------------
    // Call EncryptFile to do the actual encryption.
    if(EncryptFile(szSource, szDestination, szPassword)) {
        printf("Encryption of the file %s was a success. \n", szSource);
        printf("The encrypted data is in file %s.\n",szDestination);
    } else {
        HandleError("Error encrypting file!");
    }
} // End of main
//--------------------------------------------------------------------
//   Code for the function EncryptFile called by main.
static BOOL EncryptFile(
    PCHAR szSource,
    PCHAR szDestination,
    PCHAR szPassword)
//--------------------------------------------------------------------
//   Parameters passed are:
//     szSource, the name of the input, a plaintext file.
//     szDestination, the name of the output, an encrypted file to be
//         created.
//     szPassword, the password.
{
    //--------------------------------------------------------------------
    //   Declare and initialize local variables.
    FILE *hSource;
    FILE *hDestination;
    HCRYPTPROV hCryptProv;
    HCRYPTKEY hKey;
    HCRYPTHASH hHash;
    PBYTE pbBuffer;
    DWORD dwBlockLen;
    DWORD dwBufferLen;
    DWORD dwCount;
    //--------------------------------------------------------------------
    // Open source file.
    if(hSource = fopen(szSource,"rb")) {
        printf("The source plaintext file, %s, is open. \n", szSource);
    } else {
        HandleError("Error opening source plaintext file!");
    }
    //--------------------------------------------------------------------
    // Open destination file.
    if(hDestination = fopen(szDestination,"wb")) {
        printf("Destination file %s is open. \n", szDestination);
    } else {
        HandleError("Error opening destination ciphertext file!");
    }
    //以下获得一个CSP句柄
    if(CryptAcquireContext(
                &hCryptProv,
                NULL,               //NULL表示使用默认密钥容器,默认密钥容器名
                //为用户登陆名
                NULL,
                PROV_RSA_FULL,
                0)) {
        printf("A cryptographic provider has been acquired. \n");
    } else {
        if(CryptAcquireContext(
                    &hCryptProv,
                    NULL,
                    NULL,
                    PROV_RSA_FULL,
                    CRYPT_NEWKEYSET))//创建密钥容器
        {
            //创建密钥容器成功,并得到CSP句柄
            printf("A new key container has been created.\n");
        } else {
            HandleError("Could not create a new key container.\n");
        }
    }
    //--------------------------------------------------------------------
    // 创建一个会话密钥(session key)
    // 会话密钥也叫对称密钥,用于对称加密算法。
    // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
    //   CryptReleaseContext 期间的阶段。会话密钥只能存在于一个会话过程)
    //--------------------------------------------------------------------
    // Create a hash object.
    if(CryptCreateHash(
                hCryptProv,
                CALG_MD5,
                0,
                0,
                &hHash)) {
        printf("A hash object has been created. \n");
    } else {
        HandleError("Error during CryptCreateHash!\n");
    }
    //--------------------------------------------------------------------
    // 用输入的密码产生一个散列
    if(CryptHashData(
                hHash,
                (BYTE *)szPassword,
                strlen(szPassword),
                0)) {
        printf("The password has been added to the hash. \n");
    } else {
        HandleError("Error during CryptHashData. \n");
    }
    //--------------------------------------------------------------------
    // 通过散列生成会话密钥
    if(CryptDeriveKey(
                hCryptProv,
                ENCRYPT_ALGORITHM,
                hHash,
                KEYLENGTH,
                &hKey)) {
        printf("An encryption key is derived from the password hash. \n");
    } else {
        HandleError("Error during CryptDeriveKey!\n");
    }
    //--------------------------------------------------------------------
    // Destroy the hash object.
    CryptDestroyHash(hHash);
    hHash = NULL;
    //--------------------------------------------------------------------
    //  The session key is now ready.
    //--------------------------------------------------------------------
    // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
    // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
    // 数据长度。
    dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
    //--------------------------------------------------------------------
    // Determine the block size. If a block cipher is used,
    // it must have room for an extra block.
    if(ENCRYPT_BLOCK_SIZE > 1)
        dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
    else
        dwBufferLen = dwBlockLen;
    //--------------------------------------------------------------------
    // Allocate memory.
    if(pbBuffer = (BYTE *)malloc(dwBufferLen)) {
        printf("Memory has been allocated for the buffer. \n");
    } else {
        HandleError("Out of memory. \n");
    }
    //--------------------------------------------------------------------
    // In a do loop, encrypt the source file and write to the destination file.
    do {
        //--------------------------------------------------------------------
        // Read up to dwBlockLen bytes from the source file.
        dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
        if(ferror(hSource)) {
            HandleError("Error reading plaintext!\n");
        }
        //--------------------------------------------------------------------
        // 加密数据
        if(!CryptEncrypt(
                    hKey,           //密钥
                    0,              //如果数据同时进行散列和加密,这里传入一个
                    //散列对象
                    feof(hSource),  //如果是最后一个被加密的块,输入TRUE.如果不是输.
                    //入FALSE这里通过判断是否到文件尾来决定是否为
                    //最后一块。
                    0,              //保留
                    pbBuffer,       //输入被加密数据,输出加密后的数据
                    &dwCount,       //输入被加密数据实际长度,输出加密后数据长度
                    dwBufferLen))   //pbBuffer的大小。
        {
            HandleError("Error during CryptEncrypt. \n");
        }
        //--------------------------------------------------------------------
        // Write data to the destination file.
        fwrite(pbBuffer, 1, dwCount, hDestination);
        if(ferror(hDestination)) {
            HandleError("Error writing ciphertext.");
        }
    } while(!feof(hSource));
    //--------------------------------------------------------------------
    //  End the do loop when the last block of the source file has been
    //  read, encrypted, and written to the destination file.
    //--------------------------------------------------------------------
    // Close files.
    if(hSource)
        fclose(hSource);
    if(hDestination)
        fclose(hDestination);
    //--------------------------------------------------------------------
    // Free memory.
    if(pbBuffer)
        free(pbBuffer);
    //--------------------------------------------------------------------
    // Destroy session key.
    if(hKey)
        CryptDestroyKey(hKey);
    //--------------------------------------------------------------------
    // Destroy hash object.
    if(hHash)
        CryptDestroyHash(hHash);
    //--------------------------------------------------------------------
    // Release provider handle.
    if(hCryptProv)
        CryptReleaseContext(hCryptProv, 0);
    return(TRUE);
} // End of Encryptfile
//--------------------------------------------------------------------
//  This example uses the function HandleError, a simple error
//  handling function, to print an error message to the standard error
//  (stderr) file and exit the program.
//  For most applications, replace this function with one
//  that does more extensive error reporting.
void HandleError(char *s) {
    fprintf(stderr,"An error occurred in running the program. \n");
    fprintf(stderr,"%s\n",s);
    fprintf(stderr, "Error number %x.\n", GetLastError());
    fprintf(stderr, "Program terminating. \n");
    exit(1);
} // End of HandleError

赵4老师 2015-03-13
  • 打赏
  • 举报
回复
又来一个找不到OpenSSL合适例子代码的。
阿发你好 2015-03-13
  • 打赏
  • 举报
回复
你这种应该使用对称加密,如AES, DES,而不适合非对称加密。 对称加密的特点,以DES为例:每次加密8字节,输出也是8字节;以 AES为例,每次加密16字节,输出16这节。反过来解密也是这样,每次解密8字节或16字节。 因为算法可逆,且加密和解密使用的同一个密钥,故称为对称加密。 在我的博客里找吧 blog.csdn.net/iamshaofa http://www.afanihao.cn/kbase/
课程简介    随着”新基建“的推行,其中涉及到的工业互联网、物联网、人工智能、云计算、区块链,无一不是与安全相关,所有数据的存储、传输、签名认证都涉及到密码学技术,所以在这样的大环境下再结合我多年安全开发经验,设计出这门课程。    因为密码学技术在新基建中的重要性,所以使其成为底层开发人员所必备的技能。特别是现在的区块链技术是全面应用密码学,大数据技术和人工智能技术也要解决隐私安全问题。所以现在学习相关技术是非常必要的技术储备,并且可以改造现有的系统,提升其安全性。课程学习目标了解DES算法原理VS2019创建C++项目,并导入openssl库学会OpenSSL DES算法加解密接口加密文件并做PKCS7 Padding 数据填充解密数据并做数据填充解析课程特点    面向工程应用    市面上的一些密码学课程和密码学的书籍,很多都是从考证出发,讲解算法原理并不面向工程应用,而我们现在缺少的是工程应用相关的知识,本课程从工程应用出发,每种技术都主要讲解其在工程中的使用,并演示工程应用的代码。    从零实现部分算法    课程中实现了base16编解码 ,XOR对称加解密算法,PKCS7 pading数据填充算法,通过对一些简单算法的实现,从而加深对密码学的理解。    理论与实践结合    课程如果只是讲代码,同学并不能理解接口背后的原理,在项目设计中就会留下隐患,出现错误也不容易排查出问题。    如果只讲理论,比如对密码学的一些研究,对于大部分从事工程应用的同学并没有必要,而是理论与实践结合,一切为了工程实践。    代码现场打出    代码不放在ppt而是现场打出,更好的让学员理解代码编写的逻辑,老师现场敲出代码正是展示出了工程项目的思考,每个步骤为什么要这么做,考虑了哪些异常,    易学不枯燥    课程为了确保大部分人开发者都学得会,理解算法原理(才能真正理解算法特性),学会工程应用(接口调用,但不局限接口调用,理解接口背后的机制,并能解决工程中会出现的问题),阅读算法源码但不实现密码算法,,并能将密码学投入到实际工程中,如果是想学习具体的加密算法实现,请关注我后面的课程。课程用到的技术    课程主要演示基于 VS2019 C++,部分演示基于ubuntu 18.04 GCC makefile    如果没有装linux系统,对本课程的学习也没有影响    课程中的OpenSSL基于最新的3.0版本,如果是openss 1.1.1版本也支持,再低的版本不支持国密算法。 课程常见问题课程讲解用的什么平台和工具?    课程演示主要在windows,基于VS2019 ,一些项目会移植到Linux在ubuntu18.04上我不会Linux能否学习本门课程?    可以的,课程主要在Windows上,Linux部分只是移植,可以暂时跳过,熟悉了Linux再过来看我不会C/C++ 语言是否能学习本门课程?    至少要会C语言,C++特性用得不多,但做了一个封装,可以预习一些C++基础。会不会讲算法实现,会不会太难学不会?    课程偏工程应用,具体的AES,椭圆曲线、RSA等算法只通过图示讲原理,一些简单hash算法会读一些源码,并不去实现,课程中会单独实现简洁的XOR对称加密和base16算法(代码量不大易懂)。其他的应用我们都基于OpenSSL3.0的SDK调用算法。课程提供源码和PPT吗?    课程中所有讲解的源码都提供,课程的上课的ppt也提供,PPT提供pdf版,只可以用于学习,不得商用,代码可以用于商用软件项目,涉及到开源系统部分,需要遵守开源的协议,但不得用于网络教学。要观看全部内容请点击c++实战区块链核心密码学-基于opensslhttps://edu.csdn.net/course/play/29593

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧