Red de conocimiento informático - Conocimiento de la instalación - ¡Buscando código fuente de cifrado y descifrado DES escrito en C!

¡Buscando código fuente de cifrado y descifrado DES escrito en C!

Copiado de otro lugar

t;

for(cnt = 0;cnt < 8; cnt++){

*(bit+cnt) = (ch> >cnt)&1;

}

return 0;

}

/* Conversión de binario a byte*/

int BitToByte(ElemType bit[8],ElemType *ch){

int cnt;

for(cnt = 0;cnt < 8; cnt++){

*ch |= *(bit + cnt) <

}

Devuelve 0; >/* Convertir una cadena de longitud 8 en una cadena de bits binarios*/

int Char8ToBit64(ElemType ch[8],ElemType bit[64]){

int cnt ;

for(cnt = 0; cnt < 8; cnt++){

ByteToBit(*(ch+cnt),bit+(cnt<<3));

}

return 0;

}

/* Convierte la cadena binaria en una cadena de longitud 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);

}

Devuelve 0

}

/* Generar subclaves*/

int DES_MakeSubKeys( ElemType key[64],ElemType subKeys[16][ 48]){

ElemType temp[56];

int cnt;

DES_PC1_Transform(key,temp);/* Reemplazo de PC1*/

for( cnt = 0; cnt < 16; cnt++){/* 16 rondas de descarte, generando 16 subclaves*/

DES_ROL(temp,MOVE_TIMES[cnt]);/* Loop Shift left*/

DES_PC2_Transform(temp,subKeys[cnt]);/*Sustitución de PC2, generar subclaves*/

}

devuelve 0;

}

/* Reemplazo de clave 1 */

int DES_PC1_Transform(ElemType key[64], ElemType tempbts[56]){

int

for(cnt = 0; cnt < 56; cnt++){

tempbts[cnt] = clave[PC_1[cnt]];

}

return 0;

}

/* Reemplazo de clave 2 */

int DES_PC2_Transfor

m(ElemType key[56], ElemType tempbts[48]){

int cnt;

for(cnt = 0; cnt < 48; cnt++){

tempbts [cnt] = clave[PC_2[cnt]];

}

devuelve 0;

}

/* Girar a la izquierda */

int DES_ROL(ElemType data[56], int time){

ElemType temp [56]

/* Guardar se repetirá en; el bit correcto*/

memcpy(temp,data,time);

memcpy(temp+time,data+28,time);

/ * Los primeros 28 bits del cambio*/

memcpy( data,data+time,28-time);

memcpy( data+28-time,temp,time); /p>

/* Mover los últimos 28 bits*/

memcpy( data+28,data+28+time,28-time

memcpy( data+); 56 -time,temp+time,time);

return 0;

}

/* Reemplazo de IP*/

int DES_IP_Transform (ElemType datos[64]){

int cnt;

ElemType temp[64];

for(cnt = 0; cnt < 64; cnt++ ) {

temp[cnt] = datos[IP_Table[cnt]];

}

memcpy(datos,temp ,64);

return 0;

}

/* Disposición inversa de IP*/

int DES_IP_1_Transform(ElemType data[64]){

int cnt;

ElemType temp[64];

for(cnt = 0; cnt < 64; cnt++){

temp [ cnt] = datos[IP_1_Table[cnt]];

}

memcpy(datos,temp,64);

devuelve 0;

}

/* Permutación extendida */

int DES_E_Transform(ElemType data[48]){

int cnt;

ElemType temp[48];

for(cnt = 0; cnt < 48; cnt++ ){

temp[cnt] = datos[E_Table[cnt]];

}

memcpy(data,temp,48);

devuelve 0;

}

/* P reemplazo * /

int DES_P_ Transform(ElemType data[32]){

int cnt;

ElemType temp[32];

para (nt

= 0; cnt < 32; cnt++){

temp[cnt] = datos[P_Table[ cnt]];

}

memcpy(datos,temp ,32);

return 0;

}

/* Hetero-Or *

int DES_XOR(ElemType R[48] , ElemType L[48] ,int count){

int cnt;

for(cnt = 0; cnt < count; cnt++){

R[ cnt] ^= L[cnt];

}

devuelve 0;

}

/* Reemplazo de S-box*/

int DES_SBOX(ElemType data[48]){

int cnt;

int línea,fila,salida;

int cur1 ,cur2;

for(cnt = 0; cnt < 8; cnt++){

cur1 = cnt*6;

cur2 = cnt<<2;

/* Calcular filas y columnas en S-box*/

línea = (data[cur1]<<1) + data[cur1+5];

fila = (datos[cur1+1 ] <<3) + (datos[cur1+2]<<2)

+ (datos[cur1+3]<<1) + datos [ cur1+4];

salida = S[cnt][línea][fila];

/* Convertir a binario*/

datos[cur2 ] = (salida&0X08)>>3;

datos[cur2+1] = (salida&0X04)>>2;

datos[cur2+2] = (salida&0X02)>>1 ;

datos[cur2+3] = salida&0x01;

}

retorno 0;

}

/ * Intercambiar */

int DES_Swap(ElemType izquierda[32], ElemType derecha[32]){

ElemType temp[32];

memcpy( temp ,izquierda,32);

memcpy(izquierda,derecha,32);

memcpy(derecha,temp,32);

retorno. 0;

}

/* Cifrar un solo grupo*/

int DES_EncryptBlock(ElemType PlainBlock[8], ElemType subKeys[16][48], ElemType cipherBlock[8]){

ElemType PlainBits[64];

ElemType copyRight[48]

int cnt;

Char8ToBit64(plainBlock,plainBits);

/* Disposición inicial (IP)

permutación)*/

DES_IP_Transform(plainBits);

/* 16 rondas de iteración*/

for(cnt = 0; cnt < 16; cnt++) {

memcpy(copyRight, PlainBits+32,32);

/* Reemplaza la mitad derecha y extiéndela de 32 bits a 48 bits*/

DES_E_Transform(copyRight);

/* Usar subclaves para distinguir la mitad derecha*/

DES_XOR(copyRight,subKeys[cnt],48);

/ * El resultado de XOR ingresa a S-box y genera un resultado de 32 bits*/

DES_SBOX(copyRight);

/* Transformación P*/

DES_P_Transform ( copyRight);

/* XOR la ​​mitad izquierda y la mitad derecha del texto plano*/

DES_XOR(plainBits,copyRight,32);

if(cnt != 15){

/* Finalmente completa el intercambio de las partes izquierda y derecha*/

DES_Swap(plainBits,plainBits+32);

}

}

/* Reemplazo anti-inicial (reemplazo de IP^1) */

DES_IP_1_ Transform(plainBits);

Bit64ToChar8(plainBits,cipherBlock);

Devuelve 0;

}

/* Descifrar un solo paquete de datos*/

int DES_DecryptBlock (ElemType cipherBlock[8], ElemType subKeys[16][48],ElemType PlainBlock[8]){

ElemType cipherBits[64];

ElemType copyRight [48 ];

int cnt;

Char8ToBit64(cipherBlock,cipherBits);

/* Reemplazo inicial (reemplazo de IP) */

DES_IP_Transform(cipherBits);

/* 16 rondas de iteración*/

/* 16 rondas de iteración*/

/* 16 rondas de iteración .

for(cnt = 15; cnt >= 0; cnt--){

memcpy(copyRight,cipherBits+32,32);

/* Ampliar la disposición de la mitad derecha de 32 bits a 48 bits*/

DES_E_ Transform(copyRight);

/* Diferenciar la mitad derecha con la subclave*/

DES_XOR(copyRight,subKeys[cnt],48);

/*El resultado diferencial ingresa al cuadro S y genera el resultado de 32 bits*/

DES_ SBOX ( copyRight);

/* P Transform*/

DES_P_Transform(copyRight);

/* Descompone la mitad izquierda y la mitad derecha del texto sin formato */

DES_XOR(cipherBits,copyRight,32);

if(cnt != 0){

/* Finalmente complete el intercambio de las partes izquierda y derecha* /

DES_Swap(cipherBits,cipherBits+32);

}

}

/* Reemplazo antiinicial (IP^1 reemplazo ) */

DES_IP_1_ Transform(cipherBits);

Bit64ToChar8(cipherBits,plainBlock);

Devuelve 0;

}< / p>

/* Archivo cifrado*/

int DES_Encrypt(char * PlainFile, char *keyStr,char *cipherFile){

ARCHIVO *plain,*cipher;

p>

int count;

ElemType PlainBlock[8],cipherBlock[8], keyBlock[8];

ElemType bKey[64];

Subclaves ElemType[16][48];

if ((plain = fopen(plainFile, "rb")) == NULL){

return PLAIN_FILE_OPEN_ERROR;

}

if((cipher = fopen(cipherFile, "wb")) == NULL){

return CIPHER_FILE_OPEN_ERROR;

}

/* Establecer la clave*/

memcpy(keyBlock,keyStr,8);

/* Convertir la clave a una secuencia binaria*/

Char8ToBit64(keyBlock,bKey);

/* Generar subclaves*/

DES_ MakeSubKeys(bKey,subKeys);

while( ! feof(plain)){

/* Lee 8 bytes cada vez y devuelve el número de bytes leídos correctamente*/

if((count = fread(plainBlock, sizeof(char) .8,plain)) == 8){

DES_EncryptBlock(plainBlock,subKeys,cipherBlock);

fwrite(cipherBlock,

sizeof(char),8,cipher);

}

}

if(count){

/* relleno*/

memset(plainBlock + count,'\0',7 - count);

/*El último carácter guarda el número de caracteres de relleno, incluido el último carácter*/

PlainBlock[7] = 8 - recuento;

DES_EncryptBlock(plainBlock,subKeys,cipherBlock);

fwrite(cipherBlock,sizeof(char),8,cipher);

}

fclose(plain);

fclose(cipher);

return OK;

}

/* Descifrar archivo*/

int DES_Decrypt(char *cipherFile, char * keyStr,char *plainFile){

ARCHIVO *plain, *cipher;

int count, times = 0;

long fileLen;

ElemType PlainBlock[8], cipherBlock[8], keyBlock[8];

p>

ElemType bKey[64];

ElemType subKeys[16][48];

if((cipher = fopen(cipherFile, "rb" )) == NULL) {

Devuelve CIPHER_FILE_OPEN_ERROR

}

if((plain = fopen(plainFile, "wb")) == NULL) {

Devuelve PLAIN_FILE_OPEN_ERROR;

}

/* Establecer clave*/

memcpy(keyBlock,keyStr,8);

/* Convertir la clave a una secuencia binaria*/

Char8ToBit64(keyBlock,bKey);

/* Generar subclaves*/

DES_MakeSubKeys(bKey, subKeys );

/* Obtener la longitud del archivo*/

fseek(cipher,0,SEEK_END); /* Coloca el puntero del archivo al final del archivo*/

fileLen = ftell(cipher); /* Obtener la posición actual del puntero del archivo*/

ftell(cipher);

rewind(cipher); /* Redirigir el puntero del archivo al encabezado del archivo*/

while(1){

/* Bytes en la contraseña el número debe ser un múltiplo entero de 8*/

fread(cipherBlock,sizeof(char),8,cipher /* Fread(cipherBlock,sizeof(char),8,cipher); ( cipherBlock,sizeof(char),8,cipher).

DES_DecryptBlock(cipherBlock,subKeys,plainBlock

veces += 8;

if( veces < fileLen){

fwrite(plainBlock, sizeof(char),8,plain);

}

else{

break ;

}

}

}

/* Determinar si el final está lleno*/

if( PlainBlock[7] < 8){

for(count = 8 - PlainBlock[7]; count < 7; count++){

if(plainBlock[count] != '\ 0'){

descanso;

}

}

}

}

}

if(count == 7){//* padding*/

fwrite(plainBlock,sizeof(char),8 - PlainBlock[7],plain ) ;

}

else{/* Sin relleno*/

fwrite(plainBlock,sizeof(char),8,plain);

}

fclose(plain);

fclose(cipher);

regresar OK;

}

int main()

{ DES_Encrypt("1.txt", "key.txt", "2.txt");

system("pausa");

DES_Decrypt("2.txt", "key.txt", "3.txt");

getchar();

devuelve 0; /p>

p>

}