0% found this document useful (0 votes)
100 views4 pages

FEAL

This document contains source code for encrypting and decrypting files using the FEAL cipher algorithm. It defines functions for the cipher's key schedule, encryption, and decryption routines. The main function handles opening input and output files, reading/writing blocks of data, and encrypting/decrypting blocks under the specified cipher mode.

Uploaded by

Pedro Homeniuk
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views4 pages

FEAL

This document contains source code for encrypting and decrypting files using the FEAL cipher algorithm. It defines functions for the cipher's key schedule, encryption, and decryption routines. The main function handles opening input and output files, reading/writing blocks of data, and encrypting/decrypting blocks under the specified cipher mode.

Uploaded by

Pedro Homeniuk
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

FEAL.

C /* Rotina de criptografia Referencia: FEAL - Fast Data Encipherment Algorithm Por Akihiro Shimizu e Shoji Miyaguchi */ #include "stdio.h" #define ubyte unsigned char #define uword unsigned int #define ulong unsigned long FILE *file_in, *file_out; ubyte *pti; ubyte *ptj; ubyte *ptl; ubyte *ptr; ulong key[2]; ulong word[2]; ulong prev[2]; ulong next[2]; ulong mixed0,mixed1; uword subkey [16]; char answer; int wki; uso() { printf("Uso: FEAL <file_in> <file_out> -<switch> <code>\n\n"); printf("<switch>\n"); printf(" e: encifra arquivo <file_in>\n"); printf(" d: decifra arquivo <file_out>\n\n"); printf("<code>\n"); printf(" chave de cifracao de ate 8 bytes\n"); } video(pt) ubyte *pt; { while (*pt) putchar(*pt++); } ubyte funcao_s(x,y,delta) ubyte x,y,delta; { delta = (x + y + delta) % 256; delta = (delta >> 6) + (delta << 2); return(delta); } ulong funcao_k(left,right) ulong left, right; { ubyte fk[4]; ptl = &left; ptr = &right; fk[1] = (*(ptl + 1) ^ *(ptl + 0)); fk[2] = (*(ptl + 2) ^ *(ptl + 3)); fk[1] = funcao_s( fk[1], (fk[2] ^ *(ptr + 0)), 0x01); fk[2] = funcao_s( fk[2], (fk[1] ^ *(ptr + 1)), 0x00); fk[0] = funcao_s(*(ptl + 0), (fk[1] ^ *(ptr + 2)), 0x00); fk[3] = funcao_s(*(ptl + 3), (fk[2] ^ *(ptr + 3)), 0x01); wki = 4; ptr = &fk[0]; while (wki--) *ptl++ = *ptr++; return(left); } ulong funcao_f(alfa,beta) ulong alfa; uword beta; { ubyte ff[4]; ptl = &alfa; ptr = &beta; Page 1

FEAL.C ff[1] = *(ptl + 1) ^ *(ptr + 0) ^ *(ptl + 0); ff[2] = *(ptl + 2) ^ *(ptr + 1) ^ *(ptl + 3); ff[1] = funcao_s(ff[1], ff[2], 1); ff[2] = funcao_s(ff[2], ff[1], 0); ff[0] = funcao_s(*(ptl + 0), ff[1], 0); ff[3] = funcao_s(*(ptl + 3), ff[2], 1); wki = 4; ptr = &ff[0]; while (wki--) *ptl++ = *ptr++; return(alfa); } key_block(dkey) ulong dkey[2]; { ulong daux, dtemp; int r; ptj = &subkey[0]; dtemp = funcao_k(dkey[0],dkey[1]); pti = &dtemp; wki = 4; while (wki--) *ptj++ = *pti++; for (r = 1 ; r <= 7 ; r++) { daux = dkey[0]; dkey[0] = dkey[1]; dkey[1] = dtemp; dtemp = funcao_k( dkey[0],(daux ^ dkey[1]) ); pti = &dtemp; wki = 4; while (wki--) *ptj++ = *pti++; } } encript(dword,x) ulong dword[2]; int x; { ulong daux, dtemp; int r, pos_skey; int ini,fim; if (x == 1) { pos_skey = 8; ini = 0; fim = 8; } else { pos_skey = 12; ini = 7; fim = -1; } ptj = &subkey[0] + pos_skey; ptr = &dtemp; wki = 4; while (wki--) (*ptr++ = *ptj++); dword[0] = dword[0] ^ dtemp; ptr = &dtemp; wki = 4; while (wki--) (*ptr++ = *ptj++); dword[1] = dword[1] ^ dtemp; dword[1] = dword[0] ^ dword[1]; for (r = ini; r != fim; r = r + x) { dword[0] = dword[0] ^ funcao_f(dword[1],subkey[r]); daux = dword[1]; dword[1] = dword[0]; dword[0] = daux; } Page 2

FEAL.C daux = dword[1]; dword[1] = dword[0]; dword[0] = daux; dword[1] = dword[1] ^ dword[0]; ptj = &subkey[0] + (pos_skey + (4 * x)); ptr = &dtemp; wki = 4; while (wki--) (*ptr++ = *ptj++); dword[0] = dword[0] ^ dtemp; ptr = &dtemp; wki = 4; while (wki--) (*ptr++ = *ptj++); dword[1] = dword[1] ^ dtemp; } main(argc,argv) int argc; char *argv[]; { ubyte *pto; int cipher,cnt,flag; if (argc != 5) { uso(); exit(); } if (*(argv[3]) != '-') { uso(); exit(); } else { switch (toupper(*(argv[3] + 1))) { case 'E' : cipher = 1; break; case 'D' : cipher = -1; break; default: uso(); exit(); } } if ((file_in = fopen(argv[1],"r")) == NULL) { video(argv[1]); video(": nao encontrado.\n"); exit(); } if ((file_out = fopen(argv[2],"r")) != NULL) { video(argv[2]); video(": ja existe. Sobrepoe (s/n)?"); flag = 1; while (flag) { switch (toupper(keyrd())) { case 'S' : flag = 0; video("\n"); break; case 'N' : video("\n cancelado ...\n"); exit(1); default : continue; } } } if ((file_out = fopen(argv[2],"w")) == NULL) { video(argv[2]); video(": nao foi possivel criar.\n"); exit(); } Page 3

FEAL.C pti = pto = wki = while argv[4]; &key[0]; 8; (wki--) *pto++ = *pti++;

key_block(key); mixed0 = key[0]; mixed1 = key[1]; if (cipher == 1) { do { cnt = fread((ubyte *) word,1,8,file_in); if (cnt != 8) ((char *) word) [7] = (char) cnt; word[0] ^= mixed0; word[1] ^= mixed1; encript(word,cipher); mixed0 = word[0]; mixed1 = word[1]; fwrite(word,1,8,file_out); } while (cnt == 8); } else { cnt = fread((ubyte *) word,1,8,file_in); do { next[0] = word[0]; next[1] = word[1]; encript(word,cipher); word[0] ^= mixed0; word[1] ^= mixed1; mixed0 = next[0]; mixed1 = next[1]; prev[0] = word[0]; prev[1] = word[1]; if ((cnt = fread((ubyte *) word,1,8,file_in)) != 8) { cnt = ((ubyte *) prev) [7]; } fwrite((ubyte *) prev,1,cnt,file_out); } while (cnt == 8 ); } fclose(file_out); fclose(file_in); exit(); }

Page 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy