⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aes_cbc.c

📁 该文件属于c++运行环境下AES加密程序源码。
💻 C
字号:
/* key all block all (a_128bit b_192bit c_256bit) with cbc mode*/
/* aes_cbc.c */
/*
 * Copyright (C) 2005
 * Akira Iwata & Masayuki Sato
 * Akira Iwata Laboratory,
 * Nagoya Institute of Technology in Japan.
 *
 * All rights reserved.
 *
 * This software is written by Masayuki Sato.
 * And if you want to contact us, send an email to Kimitake Wakayama
 * (wakayama@elcom.nitech.ac.jp)
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. All advertising materials mentioning features or use of this software must
 *    display the following acknowledgment:
 *    "This product includes software developed by Akira Iwata Laboratory,
 *    Nagoya Institute of Technology in Japan (http://mars.elcom.nitech.ac.jp/)."
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Akira Iwata Laboratory,
 *     Nagoya Institute of Technology in Japan (http://mars.elcom.nitech.ac.jp/)."
 *
 *   THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
 *   AKIRA IWATA LABORATORY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 *   SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
 *   IN NO EVENT SHALL AKIRA IWATA LABORATORY BE LIABLE FOR ANY SPECIAL,
 *   INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 *   FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 *   NEGLIGENCE OR OTHER TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION
 *   WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "aes.h"

#define MAXLEN 2000
int plainstr[MAXLEN]={0,1,0,1,1,1,0,1,0,1,1,0,1,0
		      ,1,1,0,1,0,1,
		      0,1,1,1,0,0,0,1,0,1,0,1,1,0,
		      1,0,1,0,1,0,
		       1,1,0,0,0,1,0,
		      1,0,1,0,1,0,0,0,0,1,1,1,0,1,
		      0,0,1,1,1,0,1,0,1,0,1,
		      0,1,1,0,1,1,1,0,0,1,0,1,0,1
		       ,0,1,
		      0,0,1,1,1,0,1,0 ,1,1,0,1,
		       0,0,1,1,1,0,1,0,1,0,0,1,1,0,
		      1,0,1,0,1,0,
		      1,0,1,0,1,1,0,1,
		      0,1,0,1,1,1,0,1,0,1,1,0,1,0
		      ,1,1,0,1,0,1,
		      0,1,1,1,0,0,0,1,0,1,0,1,1,0,
		      1,0,1,0,1,0,
		       1,1,0,0,0,1,0,
		      1,0,1,0,1,0,0,0,0,1,1,1,0,1,
		      0,0,1,1,1,0,1,0,1,0,1,
		      0,1,1,0,1,1,1,0,0,1,0,1,0,1,0,1,
		      0,0,1,1,1,0,1,0 ,1,1,0,1,
		       0,0,1,1,1,0,1,0,1,0,0,1,1,0,
		      1,0,1,0,1,0,
		      1,0,1,0,1,1,0,1,
		      1,1,0,0,0,1,0,
		      1,0,1,0,1,0,0,0,0,1,1,1,0,1,
		      0,1,1,1,0,1,0,0,1,
		      0,1,1,0,1,1,1,1,0,1,1,0,
		      1,0,1,0,1,1,0,1,0,1,0,1,1,0,
		      1,0,1,0,1,0,
		      1,1,0,0,0,1,0,1,0,1,1,0,
		      1,0,1,0,1,0,-1
};
int IV[32] = {12,145,2,64,234,34,0,3,24,21,
	      15,23,34,55,63,21,51,167,74,23,
	      89,96,43,86,94,91,25,48,82,4,75,75};
int MAXcount;
int oput[32][1000];
int de_oput[32][1000];


/* **********plain statement input ********* */
int  TestData(int statemt[32],int type){
    int i=0;int ret;int ini[32]={3,4,2,6,3,7,6,4,2,5,1,2,3,2,5,4,2,1,4,3,5,2,8,3,1,3,5,3,7,4,3,6};
    switch (type){
    case 128128:case 192128:case 256128:
    srand(time(NULL));
    printf("message                \t");
    while (i < 16) {
	statemt[i]=( ret=rand()) % 256;
	srand(ret >> ini[i]);
	++i;
    }i=0;
    while (i < 16) {
	if (statemt[i] < 16) printf("0");
	printf("%x",statemt[i]);
	++i; }
    printf("\n");break;
    case 128192:case 192192:case 256192:
	i=0;
    srand(time(NULL));
    printf("message                \t");
    while (i < 24) {
	statemt[i]=( ret=rand()) % 256;
	srand(ret >> ini[i]);
	++i;
    }i=0;
    while (i < 24) {
	if (statemt[i] < 16) printf("0");
	printf("%x",statemt[i]);
	++i;
    }
    printf("\n"); break;
    case 128256:case 192256:case 256256:
        i=0;
    srand(time(NULL));
    printf("message                \t");
    while (i < 32) {
	statemt[i]=( ret=rand()) % 256;
	srand(ret >> ini[i]);
	++i;
    }i=0;
    while (i < 32) {
	if (statemt[i] < 16) printf("0");
	printf("%x",statemt[i]);
	++i;}
    printf("\n"); break;
    }
    return 0;
}


/* ***************** main **************************** */
    int main(void){
	int i;int n=0;
	int loop[9]= {
	    128128,128192,128256,192128,192192,192256,256128,256192,256256};

	while (n<9){
	for(i=0;i<32;++i)  statemt[i]=0;

/* key generate & key display */
	MakeKey();

/* plain data */
	TestData(statemt,loop[n]);

/* encrypto */
	encrypto(statemt,key,loop[n]);

/* decrypto */
	decrypto(statemt,key,loop[n]);
	printf("\n");
	n++;
	}
printf ("********************* cbc test ***********************\n");
/* cbc mode */ 
	cbc_encrypto(plainstr,128128,IV);
	cbc_decrypto(128128);

	return 0;
}  

void cbc_encrypto(int plainstr[MAXLEN],int type,int IV[32]){
    int i=0;int j=0;int k=0;int l;int count=0;int dummy;
    int num;int byt[8];int sum=0;int box[32];
    switch(type){
    case 128128:case 192128:case 256128:
	num = 16; break;
    case 128192:case 192192:case 256192:
	num = 24; break;
    case 128256:case 192256:case 256256:
	num = 32; break;}
  
    while (i < MAXLEN){
	k=0;
	while (k < num){
	j=0;
	    while (j < 8) {
		if (plainstr[i] == 1) byt[j++] = 1;
		else if (plainstr[i] < 0) i += MAXLEN;
		else byt[j++] = 0;
		++i;
	    }
	    j=0;sum = byt[j++];
	    while (j < 8) sum = sum * 2 + byt[j++]; 
	    
	    box[k++] = sum;
    }
	for (l=0;l<num;++l)
	    statemt[l]=box[l];
       
	printf("\ncbc number (plain) %d \t",count+1);
	for (l=0;l<num;++l){
	if (statemt[l]<16) printf("0");
	printf("%x",statemt[l]);}printf("\n");
	if (count == 0)
	for (l=0;l<num;++l)
	    statemt[l] ^= IV[l];
	else for (l=0;l<num;++l)
	    statemt[l] ^= oput[l][count-1];
	printf("cbc number (add rand)\t");
	for (l=0;l<num;++l){
	if (statemt[l]<16) printf("0");
	printf("%x",statemt[l]);}printf("\n");
	encrypto(statemt,key,type);
	for (l=0;l<num;++l)
	    {dummy = statemt[l];
	    oput[l][count] = dummy ;}
	count++;
    }
    MAXcount = count;
    printf("\n");
}


void cbc_decrypto(int type){
    int i=0;int j;int count=0;int dummy;
    int num;int sum=0;
   
    switch(type){
    case 128128:case 192128:case 256128:
	num = 16; break;
    case 128192:case 192192:case 256192:
	num = 24; break;
    case 128256:case 192256:case 256256:
	num = 32; break;}

    while (count < MAXcount){
	printf("\n");
	printf("cbc number (given) %d \t",count+1);
	for (j=0;j<num;++j){
	if (oput[j][count]<16) printf("0");
	printf("%x",oput[j][count]);}printf("\n");
	for(j=0;j<num;j++)
	    statemt[j]=oput[j][count];
	decrypto(statemt,key,type);
	for (j=0;j<num;++j)
	    {dummy = statemt[j];
	    de_oput[j][count] = dummy ;}
	if (count == 0)
	for (j=0;j<num;++j)
	    de_oput[j][count] ^= IV[j];
	else for (j=0;j<num;++j)
	    de_oput[j][count] ^= oput[j][count-1];
	
	printf("decrypto message (rand)\t");
	for (j=0;j<num;++j){
	if (de_oput[j][count] < 16) printf("0");
	printf("%x",de_oput[j][count]);
    }
    printf("\n");
	count++;
    }
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -