📄 des3.txt
字号:
memcpy(ptext,IV,8);
for (i=0;i<=(length>>3);i++) { // length>>3 ==> length/8
memcpy(ctext,&Ciphertext[i*8],8);
for (j=0;j<8;j++) {
ptext[j]^=ctext[j];
}
memcpy(&Plaintext[i*8],ptext,8);
Encrypt(ctext,ptext);
}
return TRUE;
}
BOOL Des::OFB_Encrypt(const char IV[8],const char* Plaintext,char* Ciphertext,const long length){
long i;
BYTE j;
char ptext[8],ctext[8];
char tmp[8];
memcpy(tmp,IV,8);
for (i=0;i<=(length>>3);i++) { // length>>3 ==> length/8
Encrypt(tmp,tmp);
memcpy(ptext,&Plaintext[i*8],8);
for (j=0;j<8;j++) {
ctext[j]=ptext[j]^tmp[j];
}
memcpy(&Ciphertext[i*8],ctext,8);
}
return TRUE;
}
BOOL Des::OFB_Decrypt(const char IV[8],const char* Ciphertext,char* Plaintext,const long length){
long i;
BYTE j;
char ptext[8],ctext[8];
char tmp[8];
memcpy(tmp,IV,8);
for (i=0;i<=(length>>3);i++) { // length>>3 ==> length/8
Encrypt(tmp,tmp);
memcpy(ctext,&Ciphertext[i*8],8);
for (j=0;j<8;j++) {
ptext[j]=ctext[j]^tmp[j];
}
memcpy(&Plaintext[i*8],ptext,8);
}
return TRUE;
}
/************************************************************************/
/* Adapted DES: Three Turns DES */
/************************************************************************/
void Des::SetKey(const char key[8]){
static BIT K[64], *KL=&K[0], *KR=&K[28];
ByteToBit(K, key, 64);
Permute(K, K, 56,PC1_Table);
for(BYTE i=0; i<16; ++i) {
RotateL(KL, 28, LOOP_Table[i]);
RotateL(KR, 28, LOOP_Table[i]);
Permute(SubKey[i], K, 48,PC2_Table);
}
}
BOOL Des::Three_Encrypt(const char* Plaintext,char* Ciphertext,const long length,
const char key[16],const char IV[8],const BYTE MODE)
{
char K[2][8];
memcpy(K[0],&key[0],8);
memcpy(K[1],&key[8],8);
switch(MODE) {
case ECB:
SetKey(K[0]);
ECB_Encrypt(Plaintext,Ciphertext,length);
SetKey(K[1]);
ECB_Decrypt(Ciphertext,Ciphertext,length);
SetKey(K[0]);
ECB_Encrypt(Ciphertext,Ciphertext,length);
break;
case CBC:
SetKey(K[0]);
CBC_Encrypt(IV,Plaintext,Ciphertext,length);
SetKey(K[1]);
CBC_Decrypt(IV,Ciphertext,Ciphertext,length);
SetKey(K[0]);
CBC_Encrypt(IV,Ciphertext,Ciphertext,length);
break;
case CFB:
SetKey(K[0]);
CFB_Encrypt(IV,Plaintext,Ciphertext,length);
SetKey(K[1]);
CFB_Decrypt(IV,Ciphertext,Ciphertext,length);
SetKey(K[0]);
CFB_Encrypt(IV,Ciphertext,Ciphertext,length);
break;
case OFB:
SetKey(K[0]);
OFB_Encrypt(IV,Plaintext,Ciphertext,length);
SetKey(K[1]);
OFB_Decrypt(IV,Ciphertext,Ciphertext,length);
SetKey(K[0]);
OFB_Encrypt(IV,Ciphertext,Ciphertext,length);
break;
default:
break;
}
return TRUE;
}
BOOL Des::Three_Decrypt(const char* Ciphertext,char* Plaintext,const long length,
const char key[16],const char IV[8],const BYTE MODE){
char K[2][8];
memcpy(K[0],&key[0],8);
memcpy(K[1],&key[8],8);
switch(MODE) {
case ECB:
SetKey(K[0]);
ECB_Decrypt(Ciphertext,Plaintext,length);
SetKey(K[1]);
ECB_Encrypt(Plaintext,Plaintext,length);
SetKey(K[0]);
ECB_Decrypt(Plaintext,Plaintext,length);
break;
case CBC:
SetKey(K[0]);
CBC_Decrypt(IV,Ciphertext,Plaintext,length);
SetKey(K[1]);
CBC_Encrypt(IV,Plaintext,Plaintext,length);
SetKey(K[0]);
CBC_Decrypt(IV,Plaintext,Plaintext,length);
break;
case CFB:
SetKey(K[0]);
CFB_Decrypt(IV,Ciphertext,Plaintext,length);
SetKey(K[1]);
CFB_Encrypt(IV,Plaintext,Plaintext,length);
SetKey(K[0]);
CFB_Decrypt(IV,Plaintext,Plaintext,length);
break;
case OFB:
SetKey(K[0]);
OFB_Decrypt(IV,Ciphertext,Plaintext,length);
SetKey(K[1]);
OFB_Encrypt(IV,Plaintext,Plaintext,length);
SetKey(K[0]);
OFB_Decrypt(IV,Plaintext,Plaintext,length);
break;
default:
break;
}
return TRUE;
}
/************************************************************************/
/* Adapted DES: Independent Sub Key DES */
/************************************************************************/
void Des::CopySubKey(const char key[16*6]){
static BIT K[16*48];
BYTE i,j,index=0;
ByteToBit(K,key,16*48);
for (i=0;i<16;i++) {
for (j=0;j<48;j++) {
SubKey[i][j]=K[index];
index++;
}
}
}
BOOL Des::Subkey_Encrypt(const char* Plaintext,char* Ciphertext,const long length,
const char key[16*6],const char IV[8]/* ="" */,const BYTE MODE/* =OFB */)
{
CopySubKey(key);
switch(MODE) {
case ECB:
ECB_Encrypt(Plaintext,Ciphertext,length);
break;
case CBC:
CBC_Encrypt(IV,Plaintext,Ciphertext,length);
break;
case CFB:
CFB_Encrypt(IV,Plaintext,Ciphertext,length);
break;
case OFB:
OFB_Encrypt(IV,Plaintext,Ciphertext,length);
break;
default:
break;
}
return TRUE;
}
BOOL Des::Subkey_Decrypt(const char* Ciphertext,char* Plaintext,const long length,
const char key[16*6],const char IV[8]/* ="" */,const BYTE MODE/* =OFB */)
{
CopySubKey(key);
switch(MODE) {
case ECB:
ECB_Encrypt(Ciphertext,Plaintext,length);
break;
case CBC:
CBC_Encrypt(IV,Ciphertext,Plaintext,length);
break;
case CFB:
CFB_Encrypt(IV,Ciphertext,Plaintext,length);
break;
case OFB:
OFB_Encrypt(IV,Ciphertext,Plaintext,length);
break;
default:
break;
}
return TRUE;
}
/************************************************************************/
/* DES Basic Functions */
/************************************************************************/
void F(BIT right[32],const BIT subkey[48]){
static BIT r[48];
Permute(r,right,48,E_Table);
Xor(r,r,subkey, 48);
SBoxPermute(right,r);
Permute(r,right,32,P_Table);
}
void Xor(BIT* output,BIT *input_a,const BIT *input_b,const short length){
for(short i=0; i<length; i++)
output[i]=input_a[i] ^ input_b[i];
}
void Permute(BIT* output,const BIT *input,const short length,const char* table){
BIT *tmp=new BIT[length];
for(short i=0; i<length; ++i)
tmp[i] = input[ table[i]-1 ];
memcpy(output, tmp, length);
delete tmp;
}
void SBoxPermute(BIT output[32],const BIT input[48]){
for(char i=0,j,k; i<8; ++i,input+=6,output+=4) {
j = (input[0]<<1) + input[5];
k = (input[1]<<3) + (input[2]<<2) + (input[3]<<1) + input[4];
ByteToBit(output, &S_Box[i][j][k], 4);
}
}
void RotateL(BIT *bit_buffer,const short length,BYTE loop)
{
BIT *tmp=new BIT[length];
memcpy(tmp, bit_buffer, loop);
memcpy(bit_buffer, bit_buffer+loop, length-loop);
memcpy(bit_buffer+length-loop, tmp, loop);
delete tmp;
}
void ByteToBit(BIT *output, const char *input, const short bits)
{
for(short i=0; i<bits; ++i)
output[i] = (input[i>>3]>>(i&7)) & 1; //===> output[i]=( input[i/8]>>(i%8) ) % 2
}
void BitToByte(char *output, const BIT *input, const short bits)
{
memset(output, 0, bits>>3);
for(short i=0; i<bits; ++i)
output[i>>3] |= input[i]<<(i&7); //===> output[i/8] | = input[i] << (i%8)
}
test.cpp
/************************************************************************/
/* Test class Des */
/* Ahui Wang */
/* Nankai U. */
/* 2006-9-14 */
/************************************************************************/
#include <iostream.h>
#include "Des.h"
#include <time.h>
#include<stdlib.h>
#include <stdio.h>
int main(){
BYTE i;
char key[8]="*2%kdH5"; //64 bits key,last Byte is '\0'; Its' just for a test
Des test(key);
char bits_64[8]="dkaliec"; //One group Encrypt/Decrypt ( 64 bits plaintext)
test.Encrypt(bits_64,bits_64);
cout<<bits_64<<endl;
test.Decrypt(bits_64,bits_64);
cout<<bits_64<<endl;
cout<<endl;
char text[64]="Welcom to nkbbs cycq —— 欢迎来到南开BBS创远超群板";
test.ECB_Encrypt(text,text,64);
cout<<text<<endl;
test.ECB_Decrypt(text,text,64);
cout<<text<<endl;
srand(time(0));
char iv[8]; //initialize vector (here all random)
for (i=0;i<8;i++) {
iv[i]=rand()%128;
}
cout<<endl;
test.CBC_Encrypt(iv,text,text,64);
cout<<text<<endl;
test.CBC_Decrypt(iv,text,text,64);
cout<<text<<endl;
cout<<endl;
test.CFB_Encrypt(iv,text,text,64);
cout<<text<<endl;
test.CFB_Decrypt(iv,text,text,64);
cout<<text<<endl;
cout<<endl;
test.OFB_Encrypt(iv,text,text,64);
cout<<text<<endl;
test.OFB_Decrypt(iv,text,text,64);
cout<<text<<endl;
cout<<endl;
char K[16]="%ds$H#54djGg&75"; //two keys,last byte of second key is '\0';Its' just for a test
test.Three_Encrypt(text,text,64,K,iv,OFB);
cout<<text<<endl;
test.Three_Decrypt(text,text,64,K,iv,OFB);
cout<<text<<endl;
FILE* fp;
fp=fopen("Des_Subkey.dat","r");
char sub_key[6*16]; //16*48 bits sub key
fread(sub_key,96,sizeof(char),fp);
test.Subkey_Encrypt(text,text,64,sub_key,iv,OFB);
cout<<endl<<text<<endl;
test.Subkey_Decrypt(text,text,64,sub_key,iv,OFB);
cout<<text<<endl;
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -