當前位置:首頁 » 密碼管理 » aes加密代碼

aes加密代碼

發布時間: 2022-09-23 04:11:51

① 求AES加密演算法 C代碼

以前編過的,c++可以用的
#include <iostream>
using namespace std;
long gcd(long a, long b)
{
if(b>a) //a中存放較大的數,b中存放較小的數
{
int temp;
temp=a;
a=b;
b=temp;
}
long n;
while((n=a%b)!=0)
{
a=b;
b=n;
}
return b;
}
//---------------------------------------
long cheng_niyuan(long a, long b)
{
for(long i=1; (i*a)%b!=1; i++);
return i;
}
//---------------------------------------
int mi_mo(int a, int b, int n)
{
int K[100];
int top=-1;
while(b)
{
top++;
K[top]=(b%2);
b/=2;
}
int c=0, f=1;
for(; top>=0; top--)
{
c=2*c;
f=(f*f)%n;
if(K[top]==1)
{
c+=1;
f=(f*a)%n;
}
}
return f;
}
//---------------------------------------
int main()
{
int p=5,q=11;

cout<<"p="<<p<<endl;
cout<<"q="<<q<<endl;
long int n=p*q;
cout<<"n="<<n<<endl;
long int fi_n=(p-1)*(q-1);
cout<<"fi_n="<<fi_n<<endl;
int e=3;
cout<<"e="<<e<<endl;
long d=cheng_niyuan(e,fi_n);
int M, C;
cout<<"請輸入明文:"<<endl;
cin>>M;
C=mi_mo(M, e, n);
cout<<"對應的密文為:"<<endl;
cout<<C<<endl;
cout<<"請輸入密文:"<<endl;
cin>>C;
M=mi_mo(C, d, n);
cout<<"對應的明文為:"<<endl;
cout<<M<<endl;
return 0;
}

② 求解釋這段安卓代碼中的AES加密流程

AES加密過程涉及到 4 種操作,分別是位元組替代、行移位、列混淆和輪密鑰加。

1.位元組替換:位元組代替的主要功能是通過S盒完成一個位元組到另外一個位元組的映射。

2.行移位:行移位的功能是實現一個4x4矩陣內部位元組之間的置換。


4.輪密鑰加:加密過程中,每輪的輸入與輪密鑰異或一次(當前分組和擴展密鑰的一部分進行按位異或);因為二進制數連續異或一個數結果是不變的,所以在解密時再異或上該輪的密鑰即可恢復輸入。

5.密鑰擴展:其復雜性是確保演算法安全性的重要部分。當分組長度和密鑰長度都是128位時,AES的加密演算法共迭代10輪,需要10個子密鑰。AES的密鑰擴展的目的是將輸入的128位密鑰擴展成11個128位的子密鑰。AES的密鑰擴展演算法是以字為一個基本單位(一個字為4個位元組),剛好是密鑰矩陣的一列。因此4個字(128位)密鑰需要擴展成11個子密鑰,共44個字。

③ 求php aes加密代碼,編碼是UTF-8


$key=pack('H*',"");

//顯示AES-128,192,256對應的密鑰長度:
//16,24,32位元組。
$key_size=strlen($key);
echo"Keysize:".$key_size." ";

$plaintext="ThisstringwasAES-256/CBC/ZeroBytePaddingencrypted.";


$iv_size=mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC);
$iv=mcrypt_create_iv($iv_size,MCRYPT_RAND);$ciphertext=mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$key,
$plaintext,MCRYPT_MODE_CBC,$iv);

④ 如何使用java對密碼加密 加密方式aes

Java有相關的實現類:具體原理如下
對於任意長度的明文,AES首先對其進行分組,每組的長度為128位。分組之後將分別對每個128位的明文分組進行加密。
對於每個128位長度的明文分組的加密過程如下:
(1)將128位AES明文分組放入狀態矩陣中。
(2)AddRoundKey變換:對狀態矩陣進行AddRoundKey變換,與膨脹後的密鑰進行異或操作(密鑰膨脹將在實驗原理七中詳細討論)。
(3)10輪循環:AES對狀態矩陣進行了10輪類似的子加密過程。前9輪子加密過程中,每一輪子加密過程包括4種不同的變換,而最後一輪只有3種變換,前9輪的子加密步驟如下:
● SubBytes變換:SubBytes變換是一個對狀態矩陣非線性的變換;
● ShiftRows變換:ShiftRows變換對狀態矩陣的行進行循環移位;
● MixColumns變換:MixColumns變換對狀態矩陣的列進行變換;
● AddRoundKey變換:AddRoundKey變換對狀態矩陣和膨脹後的密鑰進行異或操作。
最後一輪的子加密步驟如下:
● SubBytes變換:SubBytes變換是一個對狀態矩陣非線性的變換;
● ShiftRows變換:ShiftRows變換對狀態矩陣的行進行循環移位;
● AddRoundKey變換:AddRoundKey變換對狀態矩陣和膨脹後的密鑰進行異或操作;
(4)經過10輪循環的狀態矩陣中的內容就是加密後的密文。
AES的加密演算法的偽代碼如下。

在AES演算法中,AddRoundKey變換需要使用膨脹後的密鑰,原始的128位密鑰經過膨脹會產生44個字(每個字為32位)的膨脹後的密鑰,這44個字的膨脹後的密鑰供11次AddRoundKey變換使用,一次AddRoundKey使用4個字(128位)的膨脹後的密鑰。
三.AES的分組過程
對於任意長度的明文,AES首先對其進行分組,分組的方法與DES相同,即對長度不足的明文分組後面補充0即可,只是每一組的長度為128位。
AES的密鑰長度有128比特,192比特和256比特三種標准,其他長度的密鑰並沒有列入到AES聯邦標准中,在下面的介紹中,我們將以128位密鑰為例。
四.狀態矩陣
狀態矩陣是一個4行、4列的位元組矩陣,所謂位元組矩陣就是指矩陣中的每個元素都是一個1位元組長度的數據。我們將狀態矩陣記為State,State中的元素記為Sij,表示狀態矩陣中第i行第j列的元素。128比特的明文分組按位元組分成16塊,第一塊記為「塊0」,第二塊記為「塊1」,依此類推,最後一塊記為「塊15」,然後將這16塊明文數據放入到狀態矩陣中,將這16塊明文數據放入到狀態矩陣中的方法如圖2-2-1所示。

塊0

塊4

塊8

塊12

塊1

塊5

塊9

塊13

塊2

塊6

塊10

塊14

塊3

塊7

塊11

塊15

圖2-2-1 將明文塊放入狀態矩陣中
五.AddRoundKey變換
狀態矩陣生成以後,首先要進行AddRoundKey變換,AddRoundKey變換將狀態矩陣與膨脹後的密鑰進行按位異或運算,如下所示。

其中,c表示列數,數組W為膨脹後的密鑰,round為加密輪數,Nb為狀態矩陣的列數。
它的過程如圖2-2-2所示。

圖2-2-2 AES演算法AddRoundKey變換
六.10輪循環
經過AddRoundKey的狀態矩陣要繼續進行10輪類似的子加密過程。前9輪子加密過程中,每一輪要經過4種不同的變換,即SubBytes變換、ShiftRows變換、MixColumns變換和AddRoundKey變換,而最後一輪只有3種變換,即SubBytes變換、ShiftRows變換和AddRoundKey變換。AddRoundKey變換已經討論過,下面分別討論餘下的三種變換。
1.SubBytes變換
SubBytes是一個獨立作用於狀態位元組的非線性變換,它由以下兩個步驟組成:
(1)在GF(28)域,求乘法的逆運算,即對於α∈GF(28)求β∈GF(28),使αβ =βα = 1mod(x8 + x4 + x3 + x + 1)。
(2)在GF(28)域做變換,變換使用矩陣乘法,如下所示:

由於所有的運算都在GF(28)域上進行,所以最後的結果都在GF(28)上。若g∈GF(28)是GF(28)的本原元素,則對於α∈GF(28),α≠0,則存在
β ∈ GF(28),使得:
β = gαmod(x8 + x4 + x3 + x + 1)
由於g255 = 1mod(x8 + x4 + x3 + x + 1)
所以g255-α = β-1mod(x8 + x4 + x3 + x + 1)
根據SubBytes變換演算法,可以得出SubBytes的置換表,如表2-2-1所示,這個表也叫做AES的S盒。該表的使用方法如下:狀態矩陣中每個元素都要經過該表替換,每個元素為8比特,前4比特決定了行號,後4比特決定了列號,例如求SubBytes(0C)查表的0行C列得FE。
表2-2-1 AES的SubBytes置換表

它的變換過程如圖2-2-3所示。

圖2-2-3 SubBytes變換
AES加密過程需要用到一些數學基礎,其中包括GF(2)域上的多項式、GF(28)域上的多項式的計算和矩陣乘法運算等,有興趣的同學請參考相關的數學書籍。
2.ShiftRows變換
ShiftRows變換比較簡單,狀態矩陣的第1行不發生改變,第2行循環左移1位元組,第3行循環左移2位元組,第4行循環左移3位元組。ShiftRows變換的過程如圖2-2-4所示。

圖2-2-4 AES的ShiftRows變換
3.MixColumns變換
在MixColumns變換中,狀態矩陣的列看作是域GF(28)的多項式,模(x4+1)乘以c(x)的結果:
c(x)=(03)x3+(01)x2+(01)x+(02)
這里(03)為十六進製表示,依此類推。c(x)與x4+1互質,故存在逆:
d(x)=(0B)x3+(0D)x2+(0G)x+(0E)使c(x)•d(x) = (D1)mod(x4+1)。
設有:

它的過程如圖2-2-5所示。

圖2-2-5 AES演算法MixColumns變換
七.密鑰膨脹
在AES演算法中,AddRoundKey變換需要使用膨脹後的密鑰,膨脹後的密鑰記為子密鑰,原始的128位密鑰經過膨脹會產生44個字(每個字為32位)的子密鑰,這44個字的子密鑰供11次AddRoundKey變換使用,一次AddRoundKey使用4個字(128位)的膨脹後的密鑰。
密鑰膨脹演算法是以字為基礎的(一個字由4個位元組組成,即32比特)。128比特的原始密鑰經過膨脹後將產生44個字的子密鑰,我們將這44個密鑰保存在一個字數組中,記為W[44]。128比特的原始密鑰分成16份,存放在一個位元組的數組:Key[0],Key[1]……Key[15]中。
在密鑰膨脹演算法中,Rcon是一個10個字的數組,在數組中保存著演算法定義的常數,分別為:
Rcon[0] = 0x01000000
Rcon[1] = 0x02000000
Rcon[2] = 0x04000000
Rcon[3] = 0x08000000
Rcon[4] = 0x10000000
Rcon[5] = 0x20000000
Rcon[6] = 0x40000000
Rcon[7] = 0x80000000
Rcon[8] = 0x1b000000
Rcon[9] = 0x36000000
另外,在密鑰膨脹中包括其他兩個操作RotWord和SubWord,下面對這兩個操作做說明:
RotWord( B0,B1,B2,B3 )對4個位元組B0,B1,B2,B3進行循環移位,即
RotWord( B0,B1,B2,B3 ) = ( B1,B2,B3,B0 )
SubWord( B0,B1,B2,B3 )對4個位元組B0,B1,B2,B3使用AES的S盒,即
SubWord( B0,B1,B2,B3 ) = ( B』0,B』1,B』2,B』3 )
其中,B』i = SubBytes(Bi),i = 0,1,2,3。
密鑰膨脹的演算法如下:

八.解密過程
AES的加密和解密過程並不相同,首先密文按128位分組,分組方法和加密時的分組方法相同,然後進行輪變換。
AES的解密過程可以看成是加密過程的逆過程,它也由10輪循環組成,每一輪循環包括四個變換分別為InvShiftRows變換、InvSubBytes變換、InvMixColumns變換和AddRoundKey變換;
這個過程可以描述為如下代碼片段所示:

九.InvShiftRows變換
InvShiftRows變換是ShiftRows變換的逆過程,十分簡單,指定InvShiftRows的變換如下。
Sr,(c+shift(r,Nb))modNb= Sr,c for 0 < r< 4 and 0 ≤ c < Nb
圖2-2-6演示了這個過程。

圖2-2-6 AES演算法InvShiftRows變換
十.InvSubBytes變換
InvSubBytes變換是SubBytes變換的逆變換,利用AES的S盒的逆作位元組置換,表2-2-2為InvSubBytes變換的置換表。
表2-2-2 InvSubBytes置換表

十一.InvMixColumns變換
InvMixColumns變換與MixColumns變換類似,每列乘以d(x)
d(x) = (OB)x3 + (0D)x2 + (0G)x + (0E)
下列等式成立:
( (03)x3 + (01)x2 + (01)x + (02) )⊙d(x) = (01)
上面的內容可以描述為以下的矩陣乘法:

十二.AddRoundKey變換
AES解密過程的AddRoundKey變換與加密過程中的AddRoundKey變換一樣,都是按位與子密鑰做異或操作。解密過程的密鑰膨脹演算法也與加密的密鑰膨脹演算法相同。最後狀態矩陣中的數據就是明文。

⑤ JAVA AES加密

使用AES加密時,當密鑰大於128時,代碼會拋出java.security.InvalidKeyException: Illegal key size or default parameters

Illegal key size or default parameters是指密鑰長度是受限制的,java運行時環境讀到的是受限的policy文件。文件位於${java_home}/jre/lib/security

這種限制是因為美國對軟體出口的控制。

解決辦法:
去掉這種限制需要下載Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.網址如下。

下載包的readme.txt 有安裝說明。就是替換${java_home}/jre/lib/security/ 下面的local_policy.jar和US_export_policy.jar
jdk 5: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#jce_policy-1.5.0-oth-JPR

⑥ 各位大俠,急求AES圖像加密的MATLAB代碼!!!

首先說定義的函數可能在調用時出現問題,原因是函數的輸入部分plot在其中並沒有使用,再者輸出參數應包括m和n,因此函數定義部分應該改為function
[hist,
rgbt,m,n]
=
getimagehists(imagename)。
在執行完hist
=
hist
/
(m*n),執行如下語句完成數據保存
save
hists.mat
hist
接著可以執行
clear
all
並執行
load
hists.mat
hist
及whos
以驗證數據存儲是否成功

⑦ java aes加密 如何用php 進行解密,以下是java代碼,求php 代碼如何寫 (主要是createkey 那個方法)

先看用什麼方式的加密,拿AES來說,你需要問java要到混淆值、初始化向量與AES加密的方式如AES-192-CFB.然後直接調用openssl_decrypt方法進行解密.

openssl_decrypt('需要解密的字元串','AES-192-CFB','混淆值',0,'初始化向量'),true)

⑧ aes加密演算法C代碼

完整的!
#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;
}

⑨ 求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;
}

⑩ 如何用php做AES加密解密,編碼是UTF-8,跪謝求代碼

class CryptAES
{
protected $cipher = MCRYPT_RIJNDAEL_128;
protected $mode = MCRYPT_MODE_ECB;
protected $pad_method = NULL;
protected $secret_key = '';
protected $iv = '';

public function set_cipher($cipher)
{
$this->cipher = $cipher;
}

public function set_mode($mode)
{
$this->mode = $mode;
}

public function set_iv($iv)
{
$this->iv = $iv;
}

public function set_key($key)
{
$this->secret_key = $key;
}

public function require_pkcs5()
{
$this->pad_method = 'pkcs5';
}

protected function pad_or_unpad($str, $ext)
{
if ( is_null($this->pad_method) )
{
return $str;
}
else
{
$func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
if ( is_callable($func_name) )
{
$size = mcrypt_get_block_size($this->cipher, $this->mode);
return call_user_func($func_name, $str, $size);
}
}
return $str;
}

protected function pad($str)
{
return $this->pad_or_unpad($str, '');
}

protected function unpad($str)
{
return $this->pad_or_unpad($str, 'un');
}

public function encrypt($str)
{
$str = $this->pad($str);
$td = mcrypt_mole_open($this->cipher, '', $this->mode, '');

if ( empty($this->iv) )
{
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this->iv;
}

mcrypt_generic_init($td, $this->secret_key, $iv);
$cyper_text = mcrypt_generic($td, $str);
$rt=base64_encode($cyper_text);
//$rt = bin2hex($cyper_text);
mcrypt_generic_deinit($td);
mcrypt_mole_close($td);

return $rt;
}

public function decrypt($str){
$td = mcrypt_mole_open($this->cipher, '', $this->mode, '');

if ( empty($this->iv) )
{
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this->iv;
}

mcrypt_generic_init($td, $this->secret_key, $iv);
//$decrypted_text = mdecrypt_generic($td, self::hex2bin($str));
$decrypted_text = mdecrypt_generic($td, base64_decode($str));
$rt = $decrypted_text;
mcrypt_generic_deinit($td);
mcrypt_mole_close($td);

return $this->unpad($rt);
}

public static function hex2bin($hexdata) {
$bindata = '';
$length = strlen($hexdata);
for ($i=0; $i< $length; $i += 2)
{
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}

public static function pkcs5_pad($text, $blocksize)
{
$pad = $blocksize - (@strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}

public static function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text) - 1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
}

/*$keyStr = 'UITN25LMUQC436IM';
$plainText = 'this is a string will be AES_Encrypt';

$aes = new CryptAES();
$aes->set_key($keyStr);
$aes->require_pkcs5();
$encText = $aes->encrypt($plainText);
$decString = $aes->decrypt($encText);

echo $encText,"n",$decString;*/

熱點內容
機架式伺服器怎麼操作 發布:2024-04-27 05:19:02 瀏覽:815
我的世界minez網易伺服器 發布:2024-04-27 05:09:26 瀏覽:384
易網頁源碼 發布:2024-04-27 04:51:06 瀏覽:864
攜程伺服器是什麼牌子 發布:2024-04-27 04:31:50 瀏覽:745
醫院新冠肺炎疫情防控演練腳本 發布:2024-04-27 04:04:45 瀏覽:652
天津智慧網關伺服器雲伺服器 發布:2024-04-27 03:56:51 瀏覽:422
移門製作下料尺寸演算法 發布:2024-04-27 03:15:02 瀏覽:641
c語言5常量 發布:2024-04-27 02:38:49 瀏覽:991
源碼怎麼搭建 發布:2024-04-27 02:33:44 瀏覽:97
java獲取參數 發布:2024-04-27 02:22:21 瀏覽:501