linux下AES-CBC128的加密程序测试源码  

http://idsips.blog.163.com/blog/static/480012722012398048394/



AES加密和解密——使用openssl编程

http://www.lovelucy.info/openssl-aes-encryption.html


AES_cbc_encrypt

 

http://blog.sina.com.cn/s/blog_608a9fa70100dk6x.html


AES_cbc_encrypt and Perl Crypt::CBC Incompatibility

hex2bin bin2hex

http://www.opensubscriber.com/message/openssl-users@openssl.org/10974596.html


openssl  1.0.1

http://fossies.org/dox/openssl-1.0.1c/aes__misc_8c_source.html




AES  加密解密实例

http://blog.chinaunix.net/uid-26553782-id-3087173.html

 

#include<stdio.h>
#include <string.h>
#include "stdafx.h"
#include <openssl/aes.h>
#define INTERFACE"eth0" 
#define PAD_SIZE 32
/*
 *需要密钥key[16+1],向量iv1[16+1],明文pt1[]
*/
void hextoasii(unsignedchar *bytes,int byteNum,char *strAsii);
//----------------------------------------------------------
int aes_test(void)
{ 
    
    unsigned char key[]= ")*^&$*kdjfkdjfjdsjfska!*&^%$#@";// 128bits key (应该是真正的随机数才好)

    char pt1[]= "aaaaaaaaaaaaaaaaab";// 明文, 16位的时候 成功。

    pt1[5] = 0x0;
    
    char ct[1000]= {0};// 密文

    char pt2[1000]={0};// 解密后的明文

    AES_KEY k; 
    
    int ilen=sizeof(pt1);
    int ctLen =(ilen%16==0)?ilen:(ilen/16+1)*16;
    //|bc~!f947j*$m_op

    unsigned char iv1[16+1]= {"|bc~!f947j*$m_op"};// 16+1,加密用 

    unsigned char iv2[16+1]= {"|bc~!f947j*$m_op"};// 16+1,解密用

    
    
    int i,j;
    AES_set_encrypt_key(key, 16*16,&k);
 
 /*
 void AES_cbc_encrypt(
 const unsigned char *in, 
 unsigned char *out,
 const unsigned long length, //in 的长度
 const AES_KEY *key, //it will be changed during the encrypt&decrypt process, so it required to be reset each time
 unsigned char *ivec, //向量
 const int enc); 
 */
     AES_cbc_encrypt((unsignedchar*)pt1,(unsigned char*)ct, ilen,&k, (unsigned char*)iv1, AES_ENCRYPT);
 
     printf("encrypt:%s\n", ct);
     FILE *fp1= fopen("aes_encrypt.txt","w");
     fwrite(ct,sizeof(unsignedchar),ctLen, fp1);
     fflush(fp1);
 
 
     char strAsii[1000]={0};
     hextoasii((unsignedchar*)ct, ctLen, strAsii);
     printf("\n%s\n", strAsii);
 
 //memset(pt2, 0, 33);

 
     AES_set_decrypt_key(key, 16*16,&k);
     AES_cbc_encrypt((unsignedchar*)ct,(unsigned char*)pt2, ctLen,&k, (unsigned char*)iv2, AES_DECRYPT);
 
     printf("before: %s\n", pt1);
     printf("after : %s\n", pt2);
     if (memcmp(pt1, pt2, ilen)==0)
         puts("AES CBC mode ok");
     else puts("AES CBC mode err");
    
    return 0;
}
//-----------------------------------------------------------
char* AESDecrypt( 
      char* strEncryptDes,
      const char* strUnEncryptSrc
      )
{
     unsigned char cipher[10000]= {0};// 密文

     unsigned char iv1[16+1]= {"|bc~!f947j*$m_op"};// 16+1,向量 

     unsigned char strEncryptKey[]= ")*^&$*kdjfkdjfjdsjfska!*&^%$#@";// 128bits key (应该是真正的随机数才好)

     AES_KEY k;
     int ilen=strlen(strUnEncryptSrc);
     int ctLen = ilen/2;

     int i=0, nCipherHex=0;
     char strAsii[5];
     for(i=0; i<ctLen; i++)
     {
         memset(strAsii, 0, 5);
         memcpy(strAsii, strUnEncryptSrc, 2);
         sscanf(strAsii,"%X",&nCipherHex);
         cipher[i]= nCipherHex;
         strUnEncryptSrc += 2;
     }

     AES_set_decrypt_key(strEncryptKey, 16*16,&k);
     AES_cbc_encrypt((unsignedchar*)cipher,(unsigned char*)  strEncryptDes, ctLen,&k, (unsigned char*)iv1, AES_DECRYPT);
 //hextoasii(cipher, ctLen, strEncryptDes);

 return strEncryptDes;
}
//-----------------------------------------------------------
char* AESEncrypt( unsigned char* strUnEncryptSrc,unsigned char* strEncryptDes,int ilen)
{
     //string strEncryptDes;

      unsignedchar cipher[10000]= {0};// 密文

      unsignedchar iv1[16+1]= {"|bc~!f947j*$m_op"};// 16+1,向量 

      unsignedchar strEncryptKey[]= ")*^&$*kdjfkdjfjdsjfska!*&^%$#@";// 128bits key (应该是真正的随机数才好)

      AES_KEY k;
      int i=0;
      char strAsii[5];
 
      //int ilen=strUnEncryptSrc.length(); 

      int ctLen= (ilen%16==0)?ilen:(ilen/16+1)*16; 
 
      AES_set_encrypt_key((unsignedchar*)strEncryptKey, 16*16,&k);
 
 /*
 void AES_cbc_encrypt(
 const unsigned char *in, //明文
 unsigned char *out, //密文
 const unsigned long length, //in 的长度
 const AES_KEY *key, //it will be changed during the encrypt&decrypt process, so it required to be reset each time
 unsigned char *ivec, //向量
 const int enc); 
 */
      AES_cbc_encrypt(strUnEncryptSrc,(unsigned char*)cipher, ilen,&k, (unsigned char*)iv1, AES_ENCRYPT);
 
 
      for(i=0; i<ctLen; i++)
      {
            memset(strAsii, 0, 5);
            sprintf(strAsii,"%02X", cipher[i]);
            memcpy(&strEncryptDes[i*2], strAsii, 2);
      }     
      //hextoasii(cipher, ctLen, strEncryptDes);

      return(char*)strEncryptDes;
}
//-----------------------------------------------------------
void aes_hex(void)
{
      char str[]= "hello world\n";
      char strAes[10000]= {0};
      char strUnAes[10000]={0};
      AESEncrypt ((unsignedchar*)str,(unsigned char*)strAes,sizeof(str));
      AESDecrypt (strUnAes,strAes);
      printf("\n");
      printf(str);
      printf(strUnAes);
}
//----------------------------------------------------------
void hextoasii(unsignedchar *bytes,int byteNum,char *strAsii)
{
    int i=0;
    for(i=0; i<byteNum; i++)
    {
        sprintf(strAsii,"%02X", bytes[i]);
        strAsii += 2;
    }
    *strAsii ='\n';
}
 
AES_cbc_encrypt源码
http://bbs.chinaunix.net/thread-948004-1-1.html
void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
                     const unsigned long length, const AES_KEY *key,
                     unsigned char *ivec, const int enc) {
        unsigned long n;
        unsigned long len = length;
        unsigned char tmp[AES_BLOCK_SIZE];
        const unsigned char *iv = ivec;
        assert(in && out && key && ivec);
        assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc));
        if (AES_ENCRYPT == enc) {
                while (len >= AES_BLOCK_SIZE) {
                        for(n=0; n < AES_BLOCK_SIZE; ++n)
                                out[n] = in[n] ^ iv[n];
                        AES_encrypt(out, out, key);
                        iv = out;
                        len -= AES_BLOCK_SIZE;
                        in += AES_BLOCK_SIZE;
                        out += AES_BLOCK_SIZE;
                }
                if (len) {
                        for(n=0; n < len; ++n)
                                out[n] = in[n] ^ iv[n];
                        for(n=len; n < AES_BLOCK_SIZE; ++n)
                                out[n] = iv[n];
                        AES_encrypt(out, out, key);
                        iv = out;
                }
                memcpy(ivec,iv,AES_BLOCK_SIZE);
        } else if (in != out) {
                while (len >= AES_BLOCK_SIZE) {
                        AES_decrypt(in, out, key);
                        for(n=0; n < AES_BLOCK_SIZE; ++n)
                                out[n] ^= iv[n];
                        iv = in;
                        len -= AES_BLOCK_SIZE;
                        in  += AES_BLOCK_SIZE;
                        out += AES_BLOCK_SIZE;
                }
                if (len) {
                        AES_decrypt(in,tmp,key);
                        for(n=0; n < len; ++n)
                                out[n] = tmp[n] ^ iv[n];
                        iv = in;
                }
                memcpy(ivec,iv,AES_BLOCK_SIZE);
        } else {
                while (len >= AES_BLOCK_SIZE) {
                        memcpy(tmp, in, AES_BLOCK_SIZE);
                        AES_decrypt(in, out, key);
                        for(n=0; n < AES_BLOCK_SIZE; ++n)
                                out[n] ^= ivec[n];
                        memcpy(ivec, tmp, AES_BLOCK_SIZE);
                        len -= AES_BLOCK_SIZE;
                        in += AES_BLOCK_SIZE;
                        out += AES_BLOCK_SIZE;
                }
                if (len) {
                        memcpy(tmp, in, AES_BLOCK_SIZE);
                        AES_decrypt(tmp, out, key);
                        for(n=0; n < len; ++n)
                                out[n] ^= ivec[n];
                        for(n=len; n < AES_BLOCK_SIZE; ++n)
                                out[n] = tmp[n];
                        memcpy(ivec, tmp, AES_BLOCK_SIZE);
                }
        }
}
 




Logo

更多推荐