資源簡介
利用C語言編寫的3DES加密算法,能夠?qū)θ我忾L度的字符數(shù)組進行加解密。通過編寫相應(yīng)的JNI接口文件能夠很容易移植到Android平臺,從而解決不同平臺DES加解密結(jié)果不一致的問題。
代碼片段和文件信息
#include?“memory.h“
#include?“3des.h“
#include?“stdio.h“
#include?
/*********************************************************/
static?void?F_func(bool?In[32]?const?bool?Ki[48]);//?f?函數(shù)
static?void?S_func(bool?Out[32]?const?bool?In[48]);//?S?盒代替
static?void?Transform(bool?*Out?bool?*In?const?char?*Table?int?len);//?變換
static?void?Xor(bool?*InA?const?bool?*InB?int?len);//?異或
static?void?RotateL(bool?*In?int?len?int?loop);//?循環(huán)左移
static?void?ByteToBit(bool?*Out?const?char?*In?int?bits);//?字節(jié)組轉(zhuǎn)換成位組
static?void?BitToByte(char?*Out?const?bool?*In?int?bits);//?位組轉(zhuǎn)換成字節(jié)組
static?bool?SubKey[16][48];//?16圈子密鑰
/*********************************************************/
/************************加密*********************************/??????
int?Encrypt(char?*Msg??char?*Key?char?*Cipherint?length)?//如果消息長度不是8的倍數(shù),末位補0湊成8的倍數(shù)。
{
if(length?<=0)
{
return?-1;
}
char?keyarray1[8];
char?keyarray2[8];
char?keyarray3[8];
memcpy(&keyarray1?&Key[0]?8);
memcpy(&keyarray2?&Key[8]?8);
memcpy(&keyarray3?&Key[16]?8);
if?(length%8==0)
{
int?dst?=?length/8;
char?out[8];
char?in[8];
for?(int?j=0;j {
memcpy(&in[0]?&Msg[8*j]?8);
encrypt(in?keyarray1?out);///////////////////
decrypt(out?keyarray2?in);//////////////////
encrypt(in?keyarray3?out);//////////////////
memcpy(&Cipher[8*j]?&out?8);
}
return?1;
}else
{
int?ext?=?length/8;
int?dst?=?length%8;
char?*?temp_in?;
char?*?temp_add;
temp_in?=?(char?*)?malloc((ext+1)*8);
temp_add?=?(char?*)malloc(8-dst);
memcpy(&temp_in[0]?&Msg[0]?length);
memset(temp_add?0?8-dst);
memcpy(&temp_in[length]&temp_add[0]?8-dst);
int?round?=?ext+1;
char?out?[8];
char?in[8];
for?(int?j=0;j {
memcpy(&in[0]?&temp_in[8*j]?8);
encrypt(in?keyarray1?out);
decrypt(out?keyarray2?in);
encrypt(in?keyarray3?out);
memcpy(&Cipher[8*j]?&out?8);
}
return?1;
}
}
void?encrypt(char?In[8]?const?char?Key[8]?char?Out[8])
{
Des_SetKey(Key);
????static?bool?M[64]?Tmp[32]?*Li?=?&M[0]?*Ri?=?&M[32];
????ByteToBit(M?In?64);
????Transform(M?M?IP_Table?64);
for(int?i=0;?i<16;?i++)
{
memcpy(Tmp?Ri?32);
F_func(Ri?SubKey[i]);
Xor(Ri?Li?32);
memcpy(Li?Tmp?32);
}
????Transform(M?M?IPR_Table?64);
????BitToByte(Out?M?64);
}
/************************解密*********************************/?????
int?Decrypt(char?*Msg??char?*Key?char?*Cipherint?length)//如果消息長度不是8的倍數(shù),末位補0湊成8的倍數(shù)。
{
if(length?<=0)
{
return?-1;
}
char?keyarray1[8];
char?keyarray2[8];
char?keyarray3[8];
memcpy(&keyarray1?&Key[0]?8);
memcpy(&keyarray2?&Key[8]?8);
memcpy(&keyarray3?&Key[16]?8);
if?(length%8==0)
{
int?dst?=?length/8;
char?out?[8];
char?in[8];
for?(int?j=0;j {
memcpy(&in[0]?&Msg[8*j]?8);
decrypt(in?keyarray3?out);
encrypt(out?keyarray2?in);
decryp
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????5862??2013-09-26?18:17??My_3DES\3DES\3des.cpp
?????文件???????4380??2013-09-26?18:17??My_3DES\3DES\3DES.dsp
?????文件????????533??2013-09-26?18:15??My_3DES\3DES\3DES.dsw
?????文件???????4764??2013-09-16?18:24??My_3DES\3DES\3des.h
?????文件??????33792??2013-09-26?18:43??My_3DES\3DES\3DES.ncb
?????文件??????48640??2013-09-26?18:43??My_3DES\3DES\3DES.opt
?????文件???????1285??2013-09-26?18:36??My_3DES\3DES\3DES.plg
?????文件?????159808??2013-09-26?18:36??My_3DES\3DES\Debug\3DES.exe
?????文件?????172184??2013-09-26?18:36??My_3DES\3DES\Debug\3DES.ilk
?????文件??????15795??2013-09-26?18:17??My_3DES\3DES\Debug\3des.obj
?????文件?????204776??2013-09-26?18:18??My_3DES\3DES\Debug\3DES.pch
?????文件?????410624??2013-09-26?18:36??My_3DES\3DES\Debug\3DES.pdb
?????文件???????4309??2013-09-26?18:36??My_3DES\3DES\Debug\main.obj
?????文件??????33792??2013-09-26?18:36??My_3DES\3DES\Debug\vc60.idb
?????文件??????53248??2013-09-26?18:36??My_3DES\3DES\Debug\vc60.pdb
?????文件???????1454??2013-09-26?18:36??My_3DES\3DES\main.cpp
?????目錄??????????0??2013-09-26?18:36??My_3DES\3DES\Debug
?????目錄??????????0??2013-09-26?18:43??My_3DES\3DES
?????目錄??????????0??2013-09-26?18:15??My_3DES
-----------?---------??----------?-----??----
??????????????1155246????????????????????19
- 上一篇:簡單的C語言點對點聊天程序
- 下一篇:C流程圖生成器,用C語言代碼 生成C語言流程圖
評論
共有 條評論