❶ 誰能給我一個c語言寫的AES CBC加解密源碼

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/aes.h>
#include "encode.h"

int encode(char *content,int way)
{
AES_KEY aes;
unsigned char key[AES_BLOCK_SIZE];
unsigned char iv[AES_BLOCK_SIZE];
unsigned char *encrypt_string;
unsigned char *input_string;
int len,i;

/*Set Input string*/
if((strlen(content)+1)%AES_BLOCK_SIZE==0)
len=strlen(content)+1;
else
len=((strlen(content)+1)/AES_BLOCK_SIZE+1)*AES_BLOCK_SIZE;
input_string=(unsigned char *)calloc(len,sizeof(unsigned char));
if(input_string==NULL)
return -1;
strncpy(input_string,content,strlen(content));

for(i=0;i<16;++i)
key[i]=i+12;
for(i=0;i<AES_BLOCK_SIZE;++i)
iv[i]=i;
if(way==0)
{
if(AES_set_encrypt_key(key,128,&aes)<0)
return -1;
}
else
{
if(AES_set_decrypt_key(key,128,&aes)<0)
return -1;
}

encrypt_string=(unsigned char *)calloc(len,sizeof(unsigned char));
if(encrypt_string==NULL)
return -1;
if(way==0)
AES_cbc_encrypt(input_string,encrypt_string,len,&aes,iv,AES_ENCRYPT);
else
AES_cbc_encrypt(input_string,encrypt_string,len,&aes,iv,AES_DECRYPT);
strcpy(content,(char *)encrypt_string);

free(input_string);
free(encrypt_string);

return 0;
}

❷ 誰知道哪裡有AES演算法加密,解密c++/C語言代碼

我有寫好的,腫么給你?貼上來吧。

#ifndef aes_h_
#define aes_h_

#include <iostream>
#include <string>

using namespace std;

typedef unsigned char uint8;

class aes
{
public:
/// 構造函數
aes();

/// 析構函數
~aes();

/// 加密,默認256位密碼
///
/// @param input 要加密的字元串
/// @param output 加密後字元串
/// @return 無
/// @see
/// @note (note描述需要注意的問題)
void encrypt(const string& input, string& output);

/// 解密 默認密碼
///
/// @param input 要解密字元串
/// @param output 解密後字元串
/// @return 無
/// @see
/// @note (note描述需要注意的問題)
void decrypt(const string& input, string& output);

/// 加密 256位
///
/// @param key 密碼
/// @param input 要加密的字元串
/// @param output 加密後字元串
/// @return 無
/// @see
/// @note (note描述需要注意的問題)
void encrypt(uint8 key[32], const string& input, string& output);

/// 解密 256位
///
/// @param key 密碼
/// @param input 要解密字元串
/// @param output 解密後字元串
/// @return 無
/// @see
/// @note (note描述需要注意的問題)
void decrypt(uint8 key[32],const string& input, string& output);

private:
typedef struct
{
uint32 erk[64]; /* encryption round keys */
uint32 drk[64]; /* decryption round keys */
int nr; /* number of rounds */
}aes_context;

int aes_set_key( aes_context* ctx, uint8* key, int nbits );
void aes_encrypt( aes_context* ctx, uint8 input[16], uint8 output[16] );
void aes_decrypt( aes_context* ctx, uint8 input[16], uint8 output[16] );

};

#endif // aes_h_

我暈,太長貼不上來啊?

❸ 用C語言編寫AES加密演算法,遇到問題。

一行行列印語句,問題的核心沒有講出來,大家怎麼幫你呢,我們只能保證這個列印過程是正確的。

❹ AES演算法C語言實現,C++不要,求幫助

/*128bits密鑰長度及分組長度AES加解密代碼
*作者:Jeffrey.zhu
*/

#include <stdlib.h>
#include <stdio.h>
//#include <stdarg.h>
//#include <string.h>
//#include <signal.h>
//#include <ctype.h>

typedef unsigned long u32;
typedef unsigned u16;
typedef unsigned char u8;

static const u32 Te0[256] = {
0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U,
0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU,
0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU,
0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U,
0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU,
0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU,
0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU,
0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU,
0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU,
0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U,
0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU,
0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU,
0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U,
0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU,
0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU,
0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU,
0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU,
0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU,
0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U,
0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU,
0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU,
0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU,
0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU,
0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U,
0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U,
0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U,
0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U,
0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU,
0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U,
0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U,
0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU,
0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU,
0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U,
0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U,
0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U,
0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU,
0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U,
0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU,
0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U,
0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU,
0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U,
0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U,
0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU,
0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U,
0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U,
0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U,
0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U,
0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U,
0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U,
0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U,
0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U,
0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU,
0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U,
0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U,
0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U,
0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U,
0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U,
0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U,
0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU,
0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U,
0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U,
0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U,
0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU,
};

static const u32 Td0[256] = {
0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U,
0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U,
0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U,
0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU,
0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U,
0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U,
0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU,
0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U,
0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU,
0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U,
0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U,
0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U,
0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U,
0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU,
0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U,
0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU,
0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U,
0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU,
0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U,
0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U,
0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U,
0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU,
0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U,
0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU,
0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U,
0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU,
0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U,
0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU,
0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU,
0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U,
0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU,
0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U,
0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU,
0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U,
0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U,
0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U,
0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU,
0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U,
0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U,
0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU,
0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U,
0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U,
0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U,
0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U,
0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U,
0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU,
0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U,
0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U,
0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U,
0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U,
0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U,
0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU,
0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU,
0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU,
0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU,
0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U,
0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U,
0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU,
0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU,
0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U,
0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU,
0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U,
0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U,
0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U,
};

❺ aes演算法c語言實現

http://wenku..com/link?url=__-4vlYsYV62kNnt7l9SF3 這里很多,如果不是專門弄這個的很少會去研究的。

專門弄這個的人自專己的代碼很屬少給出來的。。

❻ 誰會用C語言實現AES演算法,謝謝

1 AES加密、解密演算法原理和AVR實現

AES是分組密鑰,演算法輸入128位數據,密鑰長度也是128位。用Nr表示對一個數據分組加密的輪數(加密輪數與密鑰長度的關系如表1所列)。每一輪都需要一個與輸入分組具有相同長度的擴展密鑰Expandedkey(i)的參與。由於外部輸入的加密密鑰K長度有限,所以在演算法中要用一個密鑰擴展程序(Keyexpansion)把外部密鑰K擴展成更長的比特串,以生成各輪的加密和解密密鑰。

1.1圈變化
AES每一個圈變換由以下三個層組成:
非線性層——進行Subbyte變換;
線行混合層——進行ShiftRow和MixColumn運算;
密鑰加層——進行AddRoundKey運算。

① Subbyte變換是作用在狀態中每個位元組上的一種非線性位元組轉換,可以通過計算出來的S盒進行映射。
Schange:
ldi zh,$01;將指針指向S盒的首地址
mov zl,r2;將要查找的數據作為指針低地址
ldtemp,z+;取出這個對應的數據
mov r2,temp;交換數據完成查表
.
.
.
ret

② ShiftRow是一個位元組換位。它將狀態中的行按照不同的偏移量進行循環移位,而這個偏移量也是根據Nb的不同而選擇的[3]。
shiftrow:;這是一個位元組換位的子程序
mov temp,r3;因為是4×4
mov r3,r7; r2 r6 r10 r14 r2 r6 r10 r14
mov r7,r11; r3 r7 r11 r15---r7 r11 r15 r3
mov r11,r15; r4 r8 r12 r17 r12 r17 r4 r8
mov r15,temp; r5 r9 r13 r18 r18 r5 r9 r13
mov temp,r4
mov temp1,r8
mov r4,r12
mov r8,r17
mov r12,temp
mov r17,temp1
mov temp,r18
mov r18,r13
mov r13,r9
mov r9,r5
mov r5,temp
ret

③ 在MixColumn變換中,把狀態中的每一列看作GF(28)上的多項式a(x)與固定多項式c(x)相乘的結果。b(x)=c(x)*a(x)的系數這樣計算:*運算不是普通的乘法運算,而是特殊的運算,即
b(x)=c(x)·a(x)(mod x4+1)
對於這個運算
b0=02。a0+03。a1+a2+a3
令xtime(a0)=02。a0
其中,符號「。」表示模一個八次不可約多項式的同餘乘法[3]。
mov temp,a0;這是一個mixcolimn子程序
rcall xtime;調用xtime程序
mov a0,temp
mov temp,a1
rcall xtime
eor a0,a1
eor a0,temp
eor a0,a2
eor a0,a3;完成b(x)的計算
.
.
.
xtime:;這是一個子程序
ldi temp1,$1b
lsl temp
brcs next1;如果最高位是1,則轉移
next: ret;否則什麼也不變化
next1:eor temp,temp1
rjmp next

對於逆變化,其矩陣C要改變成相應的D,即b(x)=d(x)*a(x)。

④ 密鑰加層運算(addround)是將圈密鑰狀態中的對應位元組按位「異或」。

⑤ 根據線性變化的性質[1],解密運算是加密變化的逆變化。這里不再詳細敘述。

1.2輪變化

對不同的分組長度,其對應的輪變化次數是不同的,如表1所列。

1.3密鑰擴展
AES演算法利用外部輸入密鑰K(密鑰串的字數為Nk),通過密鑰的擴展程序得到共計4(Nr+1)字的擴展密鑰。它涉及如下三個模塊:

① 位置變換(rotword)——把一個4位元組的序列[A,B,C,D]變化成[B,C,D,A];

② S盒變換(subword)——對一個4位元組進行S盒代替;

③ 變換Rcon[i]——Rcon[i]表示32位比特字[xi-1,00,00,00]。這里的x是(02),如
Rcon[1]=[01000000];Rcon[2]=[02000000];Rcon[3]=[04000000]……

擴展密鑰的生成:擴展密鑰的前Nk個字就是外部密鑰K;以後的字W[[i]]等於它前一個字W[[i-1]]與前第Nk個字W[[i-Nk]]的「異或」,即W[[i]]=W[[i-1]]

❼ 求C語言的AES加密代碼 能用的貼出來吧 給滿分

#include "stdio.h"
#include "memory.h"
#include "time.h"
#include "stdlib.h"

#define PLAIN_FILE_OPEN_ERROR -1
#define KEY_FILE_OPEN_ERROR -2
#define CIPHER_FILE_OPEN_ERROR -3
#define OK 1

typedef char ElemType;

/*初始置換表IP*/
int IP_Table[64] = { 57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7,
56,48,40,32,24,16,8,0,
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6};
/*逆初始置換表IP^-1*/
int IP_1_Table[64] = {39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25,
32,0,40,8,48,16,56,24};

/*擴充置換表E*/
int E_Table[48] = {31, 0, 1, 2, 3, 4,
3, 4, 5, 6, 7, 8,
7, 8,9,10,11,12,
11,12,13,14,15,16,
15,16,17,18,19,20,
19,20,21,22,23,24,
23,24,25,26,27,28,
27,28,29,30,31, 0};

/*置換函數P*/
int P_Table[32] = {15,6,19,20,28,11,27,16,
0,14,22,25,4,17,30,9,
1,7,23,13,31,26,2,8,
18,12,29,5,21,10,3,24};

/*S盒*/
int S[8][4][16] =
/*S1*/
{{{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
{0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}},
/*S2*/
{{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}},
/*S3*/
{{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}},
/*S4*/
{{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}},
/*S5*/
{{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},
{14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},
{4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},
{11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}},
/*S6*/
{{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}},
/*S7*/
{{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
{1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}},
/*S8*/
{{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}}};
/*置換選擇1*/
int PC_1[56] = {56,48,40,32,24,16,8,
0,57,49,41,33,25,17,
9,1,58,50,42,34,26,
18,10,2,59,51,43,35,
62,54,46,38,30,22,14,
6,61,53,45,37,29,21,
13,5,60,52,44,36,28,
20,12,4,27,19,11,3};

/*置換選擇2*/
int PC_2[48] = {13,16,10,23,0,4,2,27,
14,5,20,9,22,18,11,3,
25,7,15,6,26,19,12,1,
40,51,30,36,46,54,29,39,
50,44,32,46,43,48,38,55,
33,52,45,41,49,35,28,31};

/*對左移次數的規定*/
int MOVE_TIMES[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};

int ByteToBit(ElemType ch,ElemType bit[8]);
int BitToByte(ElemType bit[8],ElemType *ch);
int Char8ToBit64(ElemType ch[8],ElemType bit[64]);
int Bit64ToChar8(ElemType bit[64],ElemType ch[8]);
int DES_MakeSubKeys(ElemType key[64],ElemType subKeys[16][48]);
int DES_PC1_Transform(ElemType key[64], ElemType tempbts[56]);
int DES_PC2_Transform(ElemType key[56], ElemType tempbts[48]);
int DES_ROL(ElemType data[56], int time);
int DES_IP_Transform(ElemType data[64]);
int DES_IP_1_Transform(ElemType data[64]);
int DES_E_Transform(ElemType data[48]);
int DES_P_Transform(ElemType data[32]);
int DES_SBOX(ElemType data[48]);
int DES_XOR(ElemType R[48], ElemType L[48],int count);
int DES_Swap(ElemType left[32],ElemType right[32]);
int DES_EncryptBlock(ElemType plainBlock[8], ElemType subKeys[16][48], ElemType cipherBlock[8]);
int DES_DecryptBlock(ElemType cipherBlock[8], ElemType subKeys[16][48], ElemType plainBlock[8]);
int DES_Encrypt(char *plainFile, char *keyStr,char *cipherFile);
int DES_Decrypt(char *cipherFile, char *keyStr,char *plainFile);

/*位元組轉換成二進制*/
int ByteToBit(ElemType ch, ElemType bit[8]){
int cnt;
for(cnt = 0;cnt < 8; cnt++){
*(bit+cnt) = (ch>>cnt)&1;
}
return 0;
}

/*二進制轉換成位元組*/
int BitToByte(ElemType bit[8],ElemType *ch){
int cnt;
for(cnt = 0;cnt < 8; cnt++){
*ch |= *(bit + cnt)<<cnt;
}
return 0;
}

/*將長度為8的字元串轉為二進制位串*/
int Char8ToBit64(ElemType ch[8],ElemType bit[64]){
int cnt;
for(cnt = 0; cnt < 8; cnt++){
ByteToBit(*(ch+cnt),bit+(cnt<<3));
}
return 0;
}

/*將二進制位串轉為長度為8的字元串*/
int Bit64ToChar8(ElemType bit[64],ElemType ch[8]){
int cnt;
memset(ch,0,8);
for(cnt = 0; cnt < 8; cnt++){
BitToByte(bit+(cnt<<3),ch+cnt);
}
return 0;
}

/*生成子密鑰*/
int DES_MakeSubKeys(ElemType key[64],ElemType subKeys[16][48]){
ElemType temp[56];
int cnt;
DES_PC1_Transform(key,temp);/*PC1置換*/
for(cnt = 0; cnt < 16; cnt++){/*16輪跌代,產生16個子密鑰*/
DES_ROL(temp,MOVE_TIMES[cnt]);/*循環左移*/
DES_PC2_Transform(temp,subKeys[cnt]);/*PC2置換,產生子密鑰*/
}
return 0;
}

/*密鑰置換1*/
int DES_PC1_Transform(ElemType key[64], ElemType tempbts[56]){
int cnt;
for(cnt = 0; cnt < 56; cnt++){
tempbts[cnt] = key[PC_1[cnt]];
}
return 0;
}

/*密鑰置換2*/
int DES_PC2_Transform(ElemType key[56], ElemType tempbts[48]){
int cnt;
for(cnt = 0; cnt < 48; cnt++){
tempbts[cnt] = key[PC_2[cnt]];
}
return 0;
}

/*循環左移*/
int DES_ROL(ElemType data[56], int time){
ElemType temp[56];

/*保存將要循環移動到右邊的位*/
memcpy(temp,data,time);
memcpy(temp+time,data+28,time);

/*前28位移動*/
memcpy(data,data+time,28-time);
memcpy(data+28-time,temp,time);

/*後28位移動*/
memcpy(data+28,data+28+time,28-time);
memcpy(data+56-time,temp+time,time);

return 0;
}

/*IP置換*/
int DES_IP_Transform(ElemType data[64]){
int cnt;
ElemType temp[64];
for(cnt = 0; cnt < 64; cnt++){
temp[cnt] = data[IP_Table[cnt]];
}
memcpy(data,temp,64);
return 0;
}

/*IP逆置換*/
int DES_IP_1_Transform(ElemType data[64]){
int cnt;
ElemType temp[64];
for(cnt = 0; cnt < 64; cnt++){
temp[cnt] = data[IP_1_Table[cnt]];
}
memcpy(data,temp,64);
return 0;
}

/*擴展置換*/
int DES_E_Transform(ElemType data[48]){
int cnt;
ElemType temp[48];
for(cnt = 0; cnt < 48; cnt++){
temp[cnt] = data[E_Table[cnt]];
}
memcpy(data,temp,48);
return 0;
}

/*P置換*/
int DES_P_Transform(ElemType data[32]){
int cnt;
ElemType temp[32];
for(cnt = 0; cnt < 32; cnt++){
temp[cnt] = data[P_Table[cnt]];
}
memcpy(data,temp,32);
return 0;
}

/*異或*/
int DES_XOR(ElemType R[48], ElemType L[48] ,int count){
int cnt;
for(cnt = 0; cnt < count; cnt++){
R[cnt] ^= L[cnt];
}
return 0;
}

/*S盒置換*/
int DES_SBOX(ElemType data[48]){
int cnt;
int line,row,output;
int cur1,cur2;
for(cnt = 0; cnt < 8; cnt++){
cur1 = cnt*6;
cur2 = cnt<<2;

/*計算在S盒中的行與列*/
line = (data[cur1]<<1) + data[cur1+5];
row = (data[cur1+1]<<3) + (data[cur1+2]<<2)
+ (data[cur1+3]<<1) + data[cur1+4];
output = S[cnt][line][row];

/*化為2進制*/
data[cur2] = (output&0X08)>>3;
data[cur2+1] = (output&0X04)>>2;
data[cur2+2] = (output&0X02)>>1;
data[cur2+3] = output&0x01;
}
return 0;
}

/*交換*/
int DES_Swap(ElemType left[32], ElemType right[32]){
ElemType temp[32];
memcpy(temp,left,32);
memcpy(left,right,32);
memcpy(right,temp,32);
return 0;
}

/*加密單個分組*/
int DES_EncryptBlock(ElemType plainBlock[8], ElemType subKeys[16][48], ElemType cipherBlock[8]){
ElemType plainBits[64];
ElemType Right[48];
int cnt;

Char8ToBit64(plainBlock,plainBits);
/*初始置換(IP置換)*/
DES_IP_Transform(plainBits);

/*16輪迭代*/
for(cnt = 0; cnt < 16; cnt++){
memcpy(Right,plainBits+32,32);
/*將右半部分進行擴展置換,從32位擴展到48位*/
DES_E_Transform(Right);
/*將右半部分與子密鑰進行異或操作*/
DES_XOR(Right,subKeys[cnt],48);
/*異或結果進入S盒,輸出32位結果*/
DES_SBOX(Right);
/*P置換*/
DES_P_Transform(Right);
/*將明文左半部分與右半部分進行異或*/
DES_XOR(plainBits,Right,32);
if(cnt != 15){
/*最終完成左右部的交換*/
DES_Swap(plainBits,plainBits+32);
}
}
/*逆初始置換(IP^1置換)*/
DES_IP_1_Transform(plainBits);
Bit64ToChar8(plainBits,cipherBlock);
return 0;
}

/*解密單個分組*/
int DES_DecryptBlock(ElemType cipherBlock[8], ElemType subKeys[16][48],ElemType plainBlock[8]){
ElemType cipherBits[64];
ElemType Right[48];
int cnt;

Char8ToBit64(cipherBlock,cipherBits);
/*初始置換(IP置換)*/
DES_IP_Transform(cipherBits);

/*16輪迭代*/
for(cnt = 15; cnt >= 0; cnt--){
memcpy(Right,cipherBits+32,32);
/*將右半部分進行擴展置換,從32位擴展到48位*/
DES_E_Transform(Right);
/*將右半部分與子密鑰進行異或操作*/
DES_XOR(Right,subKeys[cnt],48);
/*異或結果進入S盒,輸出32位結果*/
DES_SBOX(Right);
/*P置換*/
DES_P_Transform(Right);
/*將明文左半部分與右半部分進行異或*/
DES_XOR(cipherBits,Right,32);
if(cnt != 0){
/*最終完成左右部的交換*/
DES_Swap(cipherBits,cipherBits+32);
}
}
/*逆初始置換(IP^1置換)*/
DES_IP_1_Transform(cipherBits);
Bit64ToChar8(cipherBits,plainBlock);
return 0;
}

/*加密文件*/
int DES_Encrypt(char *plainFile, char *keyStr,char *cipherFile){
FILE *plain,*cipher;
int count;
ElemType plainBlock[8],cipherBlock[8],keyBlock[8];
ElemType bKey[64];
ElemType subKeys[16][48];
if((plain = fopen(plainFile,"rb")) == NULL){
return PLAIN_FILE_OPEN_ERROR;
}
if((cipher = fopen(cipherFile,"wb")) == NULL){
return CIPHER_FILE_OPEN_ERROR;
}
/*設置密鑰*/
memcpy(keyBlock,keyStr,8);
/*將密鑰轉換為二進制流*/
Char8ToBit64(keyBlock,bKey);
/*生成子密鑰*/
DES_MakeSubKeys(bKey,subKeys);

while(!feof(plain)){
/*每次讀8個位元組,並返回成功讀取的位元組數*/
if((count = fread(plainBlock,sizeof(char),8,plain)) == 8){
DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
fwrite(cipherBlock,sizeof(char),8,cipher);
}
}
if(count){
/*填充*/
memset(plainBlock + count,'\0',7 - count);
/*最後一個字元保存包括最後一個字元在內的所填充的字元數量*/
plainBlock[7] = 8 - count;
DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
fwrite(cipherBlock,sizeof(char),8,cipher);
}
fclose(plain);
fclose(cipher);
return OK;
}

/*解密文件*/
int DES_Decrypt(char *cipherFile, char *keyStr,char *plainFile){
FILE *plain, *cipher;
int count,times = 0;
long fileLen;
ElemType plainBlock[8],cipherBlock[8],keyBlock[8];
ElemType bKey[64];
ElemType subKeys[16][48];
if((cipher = fopen(cipherFile,"rb")) == NULL){
return CIPHER_FILE_OPEN_ERROR;
}
if((plain = fopen(plainFile,"wb")) == NULL){
return PLAIN_FILE_OPEN_ERROR;
}

/*設置密鑰*/
memcpy(keyBlock,keyStr,8);
/*將密鑰轉換為二進制流*/
Char8ToBit64(keyBlock,bKey);
/*生成子密鑰*/
DES_MakeSubKeys(bKey,subKeys);

/*取文件長度 */
fseek(cipher,0,SEEK_END);/*將文件指針置尾*/
fileLen = ftell(cipher); /*取文件指針當前位置*/
rewind(cipher); /*將文件指針重指向文件頭*/
while(1){
/*密文的位元組數一定是8的整數倍*/
fread(cipherBlock,sizeof(char),8,cipher);
DES_DecryptBlock(cipherBlock,subKeys,plainBlock);
times += 8;
if(times < fileLen){
fwrite(plainBlock,sizeof(char),8,plain);
}
else{
break;
}
}
/*判斷末尾是否被填充*/
if(plainBlock[7] < 8){
for(count = 8 - plainBlock[7]; count < 7; count++){
if(plainBlock[count] != '\0'){
break;
}
}
}
if(count == 7){/*有填充*/
fwrite(plainBlock,sizeof(char),8 - plainBlock[7],plain);
}
else{/*無填充*/
fwrite(plainBlock,sizeof(char),8,plain);
}

fclose(plain);
fclose(cipher);
return OK;
}

int main()
{
clock_t a,b;
a = clock();
DES_Encrypt("1.txt","key.txt","2.txt");
b = clock();
printf("加密消耗%d毫秒\n",b-a);

system("pause");
a = clock();
DES_Decrypt("2.txt","key.txt","3.txt");
b = clock();
printf("解密消耗%d毫秒\n",b-a);
getchar();
return 0;
}

❽ 求AES加密中MixColumn函數的C語言寫法

1 AES加密、解密演算法原理和AVR實現

AES是分組密鑰,演算法輸入128位數據,密鑰長度也是128位。用Nr表示對一個數據分組加密的輪數(加密輪數與密鑰長度的關系如表1所列)。每一輪都需要一個與輸入分組具有相同長度的擴展密鑰Expandedkey(i)的參與。由於外部輸入的加密密鑰K長度有限,所以在演算法中要用一個密鑰擴展程序(Keyexpansion)把外部密鑰K擴展成更長的比特串,以生成各輪的加密和解密密鑰。

1.1圈變化
AES每一個圈變換由以下三個層組成:
非線性層——進行Subbyte變換;
線行混合層——進行ShiftRow和MixColumn運算;
密鑰加層——進行AddRoundKey運算。

① Subbyte變換是作用在狀態中每個位元組上的一種非線性位元組轉換,可以通過計算出來的S盒進行映射。
Schange:
ldi zh,$01;將指針指向S盒的首地址
mov zl,r2;將要查找的數據作為指針低地址
ldtemp,z+;取出這個對應的數據
mov r2,temp;交換數據完成查表
.
.
.
ret

② ShiftRow是一個位元組換位。它將狀態中的行按照不同的偏移量進行循環移位,而這個偏移量也是根據Nb的不同而選擇的[3]。
shiftrow:;這是一個位元組換位的子程序
mov temp,r3;因為是4×4
mov r3,r7; r2 r6 r10 r14 r2 r6 r10 r14
mov r7,r11; r3 r7 r11 r15---r7 r11 r15 r3
mov r11,r15; r4 r8 r12 r17 r12 r17 r4 r8
mov r15,temp; r5 r9 r13 r18 r18 r5 r9 r13
mov temp,r4
mov temp1,r8
mov r4,r12
mov r8,r17
mov r12,temp
mov r17,temp1
mov temp,r18
mov r18,r13
mov r13,r9
mov r9,r5
mov r5,temp
ret

③ 在MixColumn變換中,把狀態中的每一列看作GF(28)上的多項式a(x)與固定多項式c(x)相乘的結果。b(x)=c(x)*a(x)的系數這樣計算:*運算不是普通的乘法運算,而是特殊的運算,即
b(x)=c(x)·a(x)(mod x4+1)
對於這個運算
b0=02。a0+03。a1+a2+a3
令xtime(a0)=02。a0
其中,符號「。」表示模一個八次不可約多項式的同餘乘法[3]。
mov temp,a0;這是一個mixcolimn子程序
rcall xtime;調用xtime程序
mov a0,temp
mov temp,a1
rcall xtime
eor a0,a1
eor a0,temp
eor a0,a2
eor a0,a3;完成b(x)的計算
.
.
.
xtime:;這是一個子程序
ldi temp1,$1b
lsl temp
brcs next1;如果最高位是1,則轉移
next: ret;否則什麼也不變化
next1:eor temp,temp1
rjmp next

對於逆變化,其矩陣C要改變成相應的D,即b(x)=d(x)*a(x)。

④ 密鑰加層運算(addround)是將圈密鑰狀態中的對應位元組按位「異或」。

⑤ 根據線性變化的性質[1],解密運算是加密變化的逆變化。這里不再詳細敘述。

1.2輪變化

對不同的分組長度,其對應的輪變化次數是不同的,如表1所列。

1.3密鑰擴展
AES演算法利用外部輸入密鑰K(密鑰串的字數為Nk),通過密鑰的擴展程序得到共計4(Nr+1)字的擴展密鑰。它涉及如下三個模塊:

① 位置變換(rotword)——把一個4位元組的序列[A,B,C,D]變化成[B,C,D,A];

② S盒變換(subword)——對一個4位元組進行S盒代替;

③ 變換Rcon[i]——Rcon[i]表示32位比特字[xi-1,00,00,00]。這里的x是(02),如
Rcon[1]=[01000000];Rcon[2]=[02000000];Rcon[3]=[04000000]……

擴展密鑰的生成:擴展密鑰的前Nk個字就是外部密鑰K;以後的字W[[i]]等於它前一個字W[[i-1]]與前第Nk個字W[[i-Nk]]的「異或」,即W[[i]]=W[[i-1]]