05-26-2012، 07:16 PM
الان سی پلاس متاسفانه ندارم اما سورس بالای چیز ساده ای
یادمه گفتی واسه جای مهمی برنامه مینویسی
این کارتو انجام میده :
میخوام بدونم در چه حد برات مهمه امنیت رمزنگاری فایل ها؟
یادمه گفتی واسه جای مهمی برنامه مینویسی
این کارتو انجام میده :
کد:
#include<conio.h>
#include <stdio.h>
#include<string.h>
int l[4],r[4],keys[2][8],ct[8];
void sbox(int sip[],int p[],int sbno,int i)
{
int sbox[2][4][4]={1,0,3,2,3,2,1,0,0,2,1,3,3,1,3,2,0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3};
int rw,c,sop;
rw = sip[3]+sip[0]*2;
c = sip[2]+sip[1]*2;
sop = sbox[sbno][rw][c]; //sop gives decimal value of S-Box Output
for(;sop!=0;sop/=2)
p[i--]=sop%2;
}
void cmp_fun(int round)
{
int EP[]={4,1,2,3,2,3,4,1},i,epd[8];
int slip[4],srip[4];
int p[4]={0},p4[]={2,4,3,1},np[4];
for(i=0;i<8;i++) // E/P Permutation
epd[i]=r[EP[i]-1];
for(i=0;i<8;i++)//Performing XOR with Key
if(i<4)
slip[i] = epd[i]^keys[round][i]; // usingKey1=>0
else
srip[i-4] = epd[i]^keys[round][i];
sbox(slip,p,0,1);//Calling SBox 1, 0->SBOX 1
sbox(srip,p,1,3);//Calling SBox 1, 1->SBOX 2
for(i=0;i<4;i++) //P4 permutation
np[i]=p[p4[i]-1];
for(i=0;i<4;i++)
l[i] = l[i]^np[i];
}
void left_shift(int keyip[],int nob)
{
int t1,t2,i;
while(nob>0)
{
t1=keyip[0],t2=keyip[5];
for(i=0;i<9;i++)
if(i<4)
keyip[i] =keyip[i+1];
else if(i>4)
keyip[i] = keyip[i+1];
keyip[4]=t1,keyip[9]=t2;
nob--;
}
}
void Bin(int dec,int b[])
{
int i,a[8],j;
for(i=0;i<8;i++)
{
while(dec!=0)
{
a[i]=dec%2;
dec=dec/2;
i++;
}
a[i]=0;
}
for(i=7,j=0;j<10;j++,i--)
{
if(i>=0)
b[j]=a[i];
else
b[j]=0;
}
}
void ch_bin(unsigned char ch,int b[])
{
int i,a[8],j;
int dec;
dec=(int)ch;
for(i=0;i<8;i++)
{
while(dec!=0)
{
a[i]=dec%2;
dec=dec/2;
i++;
}
a[i]=0;
}
for(i=7,j=0;j<8;j++,i--)
b[j]=a[i];
}
unsigned char bin_ch(int b[])
{
int i,j,p=1;
int dec=0;
unsigned char ch;
for(i=7;i>=0;i--)
{
dec=dec+(b[i]*p);
p=p*2;
}
ch=(unsigned char)dec;
return ch;
}
void pass_key(char *s,int key[])
{
int a[10],arr[10],ai[10],ar[10][10];
int i,j,k;
for(i=0;i<10;i++)
{
if(i<strlen(s))
a[i]=(int)s[i];
else
a[i]=0;
Bin(a[i],arr);
for(k=0,j=0;k<10;k++,j++)
ar[i][k]=arr[j];
}
k=0;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(i==j)
{
key[k]=ar[i][j];
k++;
}
}
void gen_keys(int key[])
{
int i,keyip[10];
int p10[]={3,5,2,7,4,10,1,9,8,6},p8[]={6,3,7,4,8,5,10,9};
for(i=0;i<10;i++) // Permutation P10
keyip[i] = key[p10[i]-1];
left_shift(keyip,1); // Left Shifting (Array,No of bts)
for(i=0;i<8;i++) //Permuting P8 on key1
keys[0][i] = keyip[p8[i]-1];// Key1 Generated!!
left_shift(keyip,2);// Generating Key2 . .
for(i=0;i<8;i++)
keys[1][i] = keyip[p8[i]-1];// Key2 Generated!!
}
void En_De(int pt[],int c)
{
int ip[]={2,6,3,1,4,8,5,7},ipi[]={4,1,3,5,7,2,8,6},t[8],i;
for(i=0;i<8;i++)// Performing Permutation on input bits!!
if(i<4)
l[i]=pt[ip[i]-1];
else
r[i-4] = pt[ip[i]-1];
cmp_fun(c);//Round 0+1 using key 0+1
for(i=0;i<4;i++) //Swapping left & right
r[i]=l[i]+r[i],l[i]=r[i]-l[i],r[i]=r[i]-l[i];
cmp_fun(!c); // Round 1+1 wid key1+1 wid swapped bits
for(i=0;i<8;i++)
if(i<4)
t[i]=l[i];
else
t[i]=r[i-4];
for(i=0;i<8;i++)
ct[i] = t[ipi[i]-1];
}
void main()
{
int pt[8],i,j,choice,l;
char s[10],name[20],target[20],pwd[20];
char ch,chr,cha;
int key[10],bin[8],key1[10];
FILE *fin;
FILE *fout;
start:
clrscr();
printf("\n\n\t\t\t");
textcolor(11);
cprintf("Program on Encryption and Decryption");
printf("\n\n\n\t");
textcolor(6);
cprintf("0. To Exit !");
textcolor(7);
printf("\n\t1. encrypt the Text file ");
printf("\n\t2. Decrypt the encrypted file");
printf("\n\t3. Help");
printf("\n\tEnter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("\n\t Enter the Password \n\t");
scanf("%s",&s);
pass_key(s,key);//conversion of password to 10 bit key
gen_keys(key); // Generating Keys key1 & key2
printf("\n\nEnter The Path Of A File Which Is To Be Encrypted : \n\t");
fflush(stdin);
gets(name);
fin=fopen(name,"r"); //Open TheInput File In A Binary Mode
if(!fin)
{ //ShowErrorOccur If File Does Not Exist
printf("Error In Openinig Of A File : \n\t"); //Or AnyErrorOccurs
printf("Re-Enter the file path.......\n\t");
getch();
goto start;
}
printf("Enter The New Encrypted File Name : \n\t");
fflush(stdin);
gets(target);
fout=fopen(target,"w+"); //Open TheOutPut File In A Binary Mode
if(!fout)
{
fclose(fin);
printf("Error In Opening Of Target File :\n\t"); //Show ErrorIfAny Error Occrs In Opening Of A File
printf("RE-Enter the target file name......\n\t");
getch();
goto start;
}
fputs(s,fout);
while(fin)
{
ch=fgetc(fin);
if(ch==EOF)
{
fclose(fin);
fclose(fout);
break;
}
ch_bin(ch,bin);
En_De(bin,0);
chr=bin_ch(ct);
fputc(chr,fout);
}
getch();
goto start;
}
case 2:
{
printf("\n\nEnter The Path Of A File Which Is To Be decrypted : \n\t");
fflush(stdin);
gets(name);
fin=fopen(name,"r"); //Open TheInput File In A Binary Mode
if(!fin)
{ //ShowErrorOccur If File Does Not Exist
printf("Error In Openinig requested File...... \n\t");
printf("Re-Enter the File path......\n\t");
getch(); //Or AnyErrorOccurs
goto start;
}
printf("enter the length of password\n\t");
scanf("%d",&l);
fgets(s,l+1,fin);
printf("\n\tEnter the password.......\n\t");
BACK5:
scanf("%s",&pwd);
if(strcmp(s,pwd)==0)
{
pass_key(pwd,key);
gen_keys(key); // Generating Keys key1 & key2
printf("\nEnter The New decrypted File Name : \n\t");
fflush(stdin);
gets(target);
fout=fopen(target,"w+"); //Open TheOutPut File In A Binary Mode
if(!fout)
{
fclose(fin);
printf("Error In Opening Of Target File :\n\t"); //Show ErrorIfAny Error Occrs In Opening Of A File
printf("Re-Enter the file name.......\n\t");
getch();
goto start;
}
while(fin)
{
cha=fgetc(fin);
if(cha==EOF)
{
fclose(fin);
fclose(fout);
break;
}
ch_bin(cha,bin);
En_De(bin,1);
chr=bin_ch(ct);
fputc(chr,fout);
}
}
else
{
printf("\nre-enter the password\n\t");
goto BACK5;
}
getch();
goto start;
}
case 3:
{
printf("\n\n\n\t1. This program is to encrypt and decrypt the text file.");
printf("\n\n\t2. enter the path for text file you wants to encrypt and decrypt.");
printf("\n\n\t3. The program may not give correct results in fullsreen.");
getch();
goto start;
}
case 0:
{
clrscr();
textcolor(14);
cprintf("\n\n\n --Thank U for using this software program-- ");
printf("\n\n\n");
getch();
break;
}
default:
textcolor(9);
cprintf("\n Invalid choice !");
getch();
textcolor(7);
goto start;
}
}
میخوام بدونم در چه حد برات مهمه امنیت رمزنگاری فایل ها؟
گروه دور همی پارسی کدرز
https://t.me/joinchat/GxVRww3ykLynHFsdCvb7eg
https://t.me/joinchat/GxVRww3ykLynHFsdCvb7eg