VB进行AES加密

dipolo 2016-11-14 10:02:54
因项目需要,需要对字符串进行AES加密,AES加密应用于VB相对比较麻烦,网上的案例也少,下面是网上唯一找到的加密案例。
但对比下面网上正确的AES加密,结果却不一样,AES的各种加密模式都试过了,就是没找到跟这个VB案例里一样的结果,跪求大神指教。。。





VB源代码:http://download.csdn.net/download/swack_ot/4434194
...全文
952 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
舉杯邀明月 2016-11-15
  • 打赏
  • 举报
回复
你有C代码,可以把它编译成dll调用, 也可以把它转换成VB代码啊。 
dipolo 2016-11-15
  • 打赏
  • 举报
回复
引用 6 楼 of123 的回复:
补充一点,AES 属于分组加密算法,无论输入输出是明文还是密文,都必须是分组长度(16字节)的整数倍。因此,它的 ECB 模式并不适合对不定长度的字符串或文件进行加密。 一个解决之道,就是采用 OFB 模式将分组加密变成流加密: 事先约定一个 16 字节的密钥和一个 16 字节的初始值 IV。 用密钥对初始值以 ECB 模式加密,得到的第一个分组密文。这就是第一个分组的“流密钥”。如果长度小于要加密的串,将输出作为输入继续产生后续的流密钥:第一个分组密文作为输入,加密得到第二个分组密文;第二个分组密文作为输入,加密得到第三个分组密文,以此类推。 加密时,截取与明文等长的流密钥,相互异或,即得密文,且与明文等长。 解密时,按同样方法获得流密钥。将流密钥与密文异或,即得明文。
我有AES-128bit/ECB/PKCS5Padding加密的C代码,我做这个是因为我需要上位机跟单片机STM32通讯,单片机里用的就是AES-128bit/ECB/PKCS5Padding的解密程序,我需要在上位机上加密并做出一个软件,然后将密文发送出去。(该软件的电脑放在公司总部远程发送的,所以先不考虑软件会不会被破解的风险) 可惜我只熟VB和单片机C语言,不会VC++,只能用VB写了,大神您有AES-128bit/ECB/PKCS5Padding的VB代码吗?
dipolo 2016-11-15
  • 打赏
  • 举报
回复
我有AES-128bit/ECB/PKCS5Padding加密的C代码,我做这个是因为我需要上位机跟单片机STM32通讯,单片机里用的就是AES-128bit/ECB/PKCS5Padding的解密程序,我需要在上位机上加密并做出一个软件,然后将密文发送出去。(该软件的电脑放在公司总部远程发送的,所以先不考虑软件会不会被破解的风险) 可惜我只熟VB和单片机C语言,不会VC++,只能用VB写了,大神您有AES-128bit/ECB/PKCS5Padding的VB代码吗?
of123 2016-11-15
  • 打赏
  • 举报
回复
补充一点,AES 属于分组加密算法,无论输入输出是明文还是密文,都必须是分组长度(16字节)的整数倍。因此,它的 ECB 模式并不适合对不定长度的字符串或文件进行加密。 一个解决之道,就是采用 OFB 模式将分组加密变成流加密: 事先约定一个 16 字节的密钥和一个 16 字节的初始值 IV。 用密钥对初始值以 ECB 模式加密,得到的第一个分组密文。这就是第一个分组的“流密钥”。如果长度小于要加密的串,将输出作为输入继续产生后续的流密钥:第一个分组密文作为输入,加密得到第二个分组密文;第二个分组密文作为输入,加密得到第三个分组密文,以此类推。 加密时,截取与明文等长的流密钥,相互异或,即得密文,且与明文等长。 解密时,按同样方法获得流密钥。将流密钥与密文异或,即得明文。
赵4老师 2016-11-15
  • 打赏
  • 举报
回复
仅供参考:
#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

of123 2016-11-15
  • 打赏
  • 举报
回复
你的加密结果是正确的,解密后是 68656C6C6F0000000000000000000000。 可以看到,它是缺省填充 0x00 到一个分组的。 AES 在线的结果解密后是 68656C6C6F0B0B0B0B0B0B0B0B0B0B0B。也就是说,PKCS5 填充,是用 0x0B 来填充的。 如果你希望与它的结果一致,改成它的填充方式即可。 至于 Base64 编解码换转,你愿意的话,转一下即可。解密前解码还原就是了。
of123 2016-11-15
  • 打赏
  • 举报
回复
我有 AES 的 C 代码,可以移植到 VB 或打包成 DLL 库使用。 但从专业的角度,不建议在 PC 端实现 AES,因为密钥和密文都很容易泄露。如果是真正有安全需求的项目,建议采用加密芯片。
of123 2016-11-15
  • 打赏
  • 举报
回复
从原理上将,AES 加密后无法保证密文依然是由可打印字符构成的“字符串”。其中必须进行其他补充运算。 标准的做法是,无论明文还是密文,都看作十六进制(或二进制)串。 其次,明文、密文,以及 IV,都必须是分组的倍数,即 16 字节(128-bit)的倍数)。 密钥,根据使用的是 AES-128/192/256 应该是 16/24/32 字节。
dipolo 2016-11-15
  • 打赏
  • 举报
回复
引用 9 楼 Chen8013 的回复:
你有C代码,可以把它编译成dll调用, 也可以把它转换成VB代码啊。 
对耶,忘记可以这样用了
dipolo 2016-11-15
  • 打赏
  • 举报
回复
引用 10 楼 of123 的回复:
既然如此,为何不用单片机做成一个可挂接在 PC 上的加密模块。PC 应用调用它加密就可以了。这样做的好处是,密钥不会泄露。
哈哈,666,好主意耶!大哥真有才!
Tiger_Zhao 2016-11-15
  • 打赏
  • 举报
回复
不知道楼主在纠结什么?
4楼 of123 已经给你分析出来,是填充的不同导致结果不同。你的算法没问题。

找供应商要技术支持啊!
他们会告诉你填充的方式;
甚至还有测试用例——什么样的明文对应什么样的密文。
用你的结果比对一下不更直接。

不和下位机的运算结果比,却去和所谓的“标准”算法比。
想什么呢?
of123 2016-11-15
  • 打赏
  • 举报
回复
既然如此,为何不用单片机做成一个可挂接在 PC 上的加密模块。PC 应用调用它加密就可以了。这样做的好处是,密钥不会泄露。
舉杯邀明月 2016-11-15
  • 打赏
  • 举报
回复
有可能他的AES算法代码有问题呢? 按理说,这个AES是一个“标准算法”,源数据、密钥、加密方案确定后,结果就是“唯一”的。 就象以前网上“广为流传”的MD5算法代码一样,都有不少错误的。 源头一错,那些 Ctrl+C、Ctrl+V党、转发党,把那些错误代码搞得“漫山遍野”的, 在网上找去找来,都是那些千篇一律的错误代码。 还有那些“翻译”的代码,真叫一个渣,看着都恶心…………

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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