my first proggy

As you probably know by know I'm a noob, but learning fast (asked a lot of stupid and less stupid questions on these boards the past 2 weeks).... I finally made my first proggy in C++ (after 2 weeks of learning). Here it is:
[code]
#include
#include
#include
#include
#include
#include

char binToChar(bool*);
int Exponent(int,int);
void charToBin(char, bool*);

void main(void){
char FileName[256],SaveName[256],Keys[9];
int k[10];
int Counter[10];
int Count,temp,i,j;

Count=0;
Counter[0]=0;
Counter[1]=0;
Counter[2]=0;
Counter[3]=0;
Counter[4]=0;
Counter[5]=0;
Counter[6]=0;
Counter[7]=0;
Counter[8]=0;
Counter[9]=0;

bool convert[15];
char Byte;
printf("Please enter the filename for encryption:
");
fflush(stdin);
scanf("%s",&FileName);
printf("Save file to:
");
fflush(stdin);
scanf("%s",&SaveName);
printf("Please enter password (8 characters/numbers):
");
fflush(stdin);
scanf("%s",&Keys);

k[0]=((int) Keys[0])/5;
if ((k[0]==1)|(k[0]==0)){
k[0]=3;
}
k[1]=((int) Keys[1]) - k[0];
if (k[1] < 0){
k[1] *= -1;
}
k[2]=((int) Keys[2])/10;
if ((k[2]==1)|(k[2]==0)){
k[2]=4;
}
k[3]=((int) Keys[3])/50;
if ((k[3]==1)|(k[3]==0)){
k[3]=7;
}
k[4]=((int) Keys[4])/(30+k[3]);
if ((k[4]==1)|(k[4]==0)){
k[4]=8;
}
k[5]=(k[4]+k[2])/ (int) Keys[5];
if ((k[5]==1)|(k[5]==0)){
k[5]=6;
}
k[6]=((int) Keys[6])/5;
if ((k[6]==1)|(k[6]==0)){
k[6]=k[6]+k[3];
}
k[7]=(int) Keys[7];
system("cls");
printf("Encrypting file.......");
time_t Time_1 , Time_2 ;

time( &Time_1 );

ifstream OpenFile(FileName);
ofstream SaveFile(SaveName);
while(!OpenFile.eof())
{
OpenFile.get(Byte);
Count++;
switch(Count){
case 1:
temp= (int) Byte;
temp+=k[4];
if(temp>255){
temp-=255;
}
Byte=(char) temp;
break;
case 3:
temp= (int) Byte;
temp+=k[2];
if(temp>255){
temp-=255;
}
Byte=(char) temp;
break;
case 5:
temp= (int) Byte;
temp+=k[1];
if(temp>255){
temp-=255;
}
Byte=(char) temp;
Count=0;
break;

}
charToBin(Byte,convert);
for(i=0;i<8;i++){
for(j=0;j<10;j++){
Counter[j]++;
if (Counter[j]==k[j]){
convert[i]=!convert[i];
Counter[j]=0;
}
}
}

SaveFile << binToChar(convert);
}

SaveFile.close();
OpenFile.close();
time( &Time_2 );


printf("Time elapsed: %d seconds ",Time_2-Time_1 );
getch();



}

char binToChar(bool ToConvert[15]){
/*
Converts 8 bits to its asci equivalent.
Gets a boolean array (true = 1, false = 0),
and converts the 8 first bits to integer (decimal),
then casts the integer to character and returns it.
*/
char Character;
int i,AscVal=0,expo;
expo=0;
for (i=7;i>-1;i--){
if(ToConvert[i]==true){
AscVal = AscVal + Exponent(2,expo);
}
expo++;
}
Character = (char) AscVal;

return Character;
}

int Exponent(int Base,int Expo){
/*
Calculates exponents of a certain base number.
So exponent(2,4) will return 2*2*2*2=16
*/
int CalcInt,i;
CalcInt=1;
for(i=1;i<=Expo;i++){
CalcInt= CalcInt * Base;
}
return CalcInt;
}

void charToBin(char Character, bool* MemAdress){
/*
Gets the memory adress of the boolean array and a character. It then
fills up the boolean array to represent a byte (equal to the character)
*/
int i,Whole=(int) Character,Remain, Counter=7;

while(Whole!=0){
Remain=Whole % 2;
Whole=(Whole-Remain) / 2;
MemAdress[Counter]=(bool) Remain;
//Above line creates warning(cast int to bool), and can be replaced by this:
//switch (Remain){
//case 0:
// MemAdress[Counter]=false;
// break;
//case 1:
// MemAdress[Counter]=true;
//}
Counter--;
}
Counter++;
if(Counter!=0){
for(i=0;i<Counter;i++){
MemAdress[i]=false;
}
}
}
[/code]

a couple of questions:

-How does the code look? I mean, I'm a beginner so I must be doing stuff completely wrong. Please correct it so I can learn from my mistakes.
-Can anyone tell me why it odesn't work for all files? I mean it seems to create a file with a different amount of bytes the the original file. It shouldn't do this at all (I read one character and write one character at a time.. so the amount of bytes should be the same)
-and last but not least... how good is this encryption? Would it be easy to crack? A series of "a" s run trough this algo returns:
"[italic?????????????????[/italic]" How hard would it be to crack this and return "aaaaaaaaaaaaa... " again?
-any speed tips? Takes 6 to 8 secs on my comp for 1 megabyte... bit to slow in my opninion.. 1 sec is my goal.
«1

Comments

  • no .h for standard headers, use string instead of char*, no print or scan. (dont you read any other posts? i wrote this a lot of times.)

    if your encryption algo is the same u asked about last time, i already wrote what i think of it. anything else than that prime number factorization thing cannot be considered safe.

    because: the thing that has to be kept safe in your case is the encryption algorithm. if it is known, deriving the key is trivial.

    in real cases, the algorithm can be (and often is) known, without the private key this doesnt help.


    : As you probably know by know I'm a noob, but learning fast (asked a lot of stupid and less stupid questions on these boards the past 2 weeks).... I finally made my first proggy in C++ (after 2 weeks of learning). Here it is:
    : [code]
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    :
    : char binToChar(bool*);
    : int Exponent(int,int);
    : void charToBin(char, bool*);
    :
    : void main(void){
    : char FileName[256],SaveName[256],Keys[9];
    : int k[10];
    : int Counter[10];
    : int Count,temp,i,j;
    :
    : Count=0;
    : Counter[0]=0;
    : Counter[1]=0;
    : Counter[2]=0;
    : Counter[3]=0;
    : Counter[4]=0;
    : Counter[5]=0;
    : Counter[6]=0;
    : Counter[7]=0;
    : Counter[8]=0;
    : Counter[9]=0;
    :
    : bool convert[15];
    : char Byte;
    : printf("Please enter the filename for encryption:
    ");
    : fflush(stdin);
    : scanf("%s",&FileName);
    : printf("Save file to:
    ");
    : fflush(stdin);
    : scanf("%s",&SaveName);
    : printf("Please enter password (8 characters/numbers):
    ");
    : fflush(stdin);
    : scanf("%s",&Keys);
    :
    : k[0]=((int) Keys[0])/5;
    : if ((k[0]==1)|(k[0]==0)){
    : k[0]=3;
    : }
    : k[1]=((int) Keys[1]) - k[0];
    : if (k[1] < 0){
    : k[1] *= -1;
    : }
    : k[2]=((int) Keys[2])/10;
    : if ((k[2]==1)|(k[2]==0)){
    : k[2]=4;
    : }
    : k[3]=((int) Keys[3])/50;
    : if ((k[3]==1)|(k[3]==0)){
    : k[3]=7;
    : }
    : k[4]=((int) Keys[4])/(30+k[3]);
    : if ((k[4]==1)|(k[4]==0)){
    : k[4]=8;
    : }
    : k[5]=(k[4]+k[2])/ (int) Keys[5];
    : if ((k[5]==1)|(k[5]==0)){
    : k[5]=6;
    : }
    : k[6]=((int) Keys[6])/5;
    : if ((k[6]==1)|(k[6]==0)){
    : k[6]=k[6]+k[3];
    : }
    : k[7]=(int) Keys[7];
    : system("cls");
    : printf("Encrypting file.......");
    : time_t Time_1 , Time_2 ;
    :
    : time( &Time_1 );
    :
    : ifstream OpenFile(FileName);
    : ofstream SaveFile(SaveName);
    : while(!OpenFile.eof())
    : {
    : OpenFile.get(Byte);
    : Count++;
    : switch(Count){
    : case 1:
    : temp= (int) Byte;
    : temp+=k[4];
    : if(temp>255){
    : temp-=255;
    : }
    : Byte=(char) temp;
    : break;
    : case 3:
    : temp= (int) Byte;
    : temp+=k[2];
    : if(temp>255){
    : temp-=255;
    : }
    : Byte=(char) temp;
    : break;
    : case 5:
    : temp= (int) Byte;
    : temp+=k[1];
    : if(temp>255){
    : temp-=255;
    : }
    : Byte=(char) temp;
    : Count=0;
    : break;
    :
    : }
    : charToBin(Byte,convert);
    : for(i=0;i<8;i++){
    : for(j=0;j<10;j++){
    : Counter[j]++;
    : if (Counter[j]==k[j]){
    : convert[i]=!convert[i];
    : Counter[j]=0;
    : }
    : }
    : }
    :
    : SaveFile << binToChar(convert);
    : }
    :
    : SaveFile.close();
    : OpenFile.close();
    : time( &Time_2 );
    :
    :
    : printf("Time elapsed: %d seconds ",Time_2-Time_1 );
    : getch();
    :
    :
    :
    : }
    :
    : char binToChar(bool ToConvert[15]){
    : /*
    : Converts 8 bits to its asci equivalent.
    : Gets a boolean array (true = 1, false = 0),
    : and converts the 8 first bits to integer (decimal),
    : then casts the integer to character and returns it.
    : */
    : char Character;
    : int i,AscVal=0,expo;
    : expo=0;
    : for (i=7;i>-1;i--){
    : if(ToConvert[i]==true){
    : AscVal = AscVal + Exponent(2,expo);
    : }
    : expo++;
    : }
    : Character = (char) AscVal;
    :
    : return Character;
    : }
    :
    : int Exponent(int Base,int Expo){
    : /*
    : Calculates exponents of a certain base number.
    : So exponent(2,4) will return 2*2*2*2=16
    : */
    : int CalcInt,i;
    : CalcInt=1;
    : for(i=1;i<=Expo;i++){
    : CalcInt= CalcInt * Base;
    : }
    : return CalcInt;
    : }
    :
    : void charToBin(char Character, bool* MemAdress){
    : /*
    : Gets the memory adress of the boolean array and a character. It then
    : fills up the boolean array to represent a byte (equal to the character)
    : */
    : int i,Whole=(int) Character,Remain, Counter=7;
    :
    : while(Whole!=0){
    : Remain=Whole % 2;
    : Whole=(Whole-Remain) / 2;
    : MemAdress[Counter]=(bool) Remain;
    : //Above line creates warning(cast int to bool), and can be replaced by this:
    : //switch (Remain){
    : //case 0:
    : // MemAdress[Counter]=false;
    : // break;
    : //case 1:
    : // MemAdress[Counter]=true;
    : //}
    : Counter--;
    : }
    : Counter++;
    : if(Counter!=0){
    : for(i=0;i<Counter;i++){
    : MemAdress[i]=false;
    : }
    : }
    : }
    : [/code]
    :
    : a couple of questions:
    :
    : -How does the code look? I mean, I'm a beginner so I must be doing stuff completely wrong. Please correct it so I can learn from my mistakes.
    : -Can anyone tell me why it odesn't work for all files? I mean it seems to create a file with a different amount of bytes the the original file. It shouldn't do this at all (I read one character and write one character at a time.. so the amount of bytes should be the same)
    : -and last but not least... how good is this encryption? Would it be easy to crack? A series of "a" s run trough this algo returns:
    : "[italic]???????????????????????????????[/italic]" How hard would it be to crack this and return "aaaaaaaaaaaaa... " again?
    : -any speed tips? Takes 6 to 8 secs on my comp for 1 megabyte... bit to slow in my opninion.. 1 sec is my goal.
    :

  • why convert[15] and not convert[8]?
    temp should be unsigned char, so you dont have to do the wrapping manually.
    declare variables as late as possible. why are they all on top? do you come from pascal?



    : As you probably know by know I'm a noob, but learning fast (asked a lot of stupid and less stupid questions on these boards the past 2 weeks).... I finally made my first proggy in C++ (after 2 weeks of learning). Here it is:
    : [code]
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    :
    : char binToChar(bool*);
    : int Exponent(int,int);
    : void charToBin(char, bool*);
    :
    : void main(void){
    : char FileName[256],SaveName[256],Keys[9];
    : int k[10];
    : int Counter[10];
    : int Count,temp,i,j;
    :
    : Count=0;
    : Counter[0]=0;
    : Counter[1]=0;
    : Counter[2]=0;
    : Counter[3]=0;
    : Counter[4]=0;
    : Counter[5]=0;
    : Counter[6]=0;
    : Counter[7]=0;
    : Counter[8]=0;
    : Counter[9]=0;
    :
    : bool convert[15];
    : char Byte;
    : printf("Please enter the filename for encryption:
    ");
    : fflush(stdin);
    : scanf("%s",&FileName);
    : printf("Save file to:
    ");
    : fflush(stdin);
    : scanf("%s",&SaveName);
    : printf("Please enter password (8 characters/numbers):
    ");
    : fflush(stdin);
    : scanf("%s",&Keys);
    :
    : k[0]=((int) Keys[0])/5;
    : if ((k[0]==1)|(k[0]==0)){
    : k[0]=3;
    : }
    : k[1]=((int) Keys[1]) - k[0];
    : if (k[1] < 0){
    : k[1] *= -1;
    : }
    : k[2]=((int) Keys[2])/10;
    : if ((k[2]==1)|(k[2]==0)){
    : k[2]=4;
    : }
    : k[3]=((int) Keys[3])/50;
    : if ((k[3]==1)|(k[3]==0)){
    : k[3]=7;
    : }
    : k[4]=((int) Keys[4])/(30+k[3]);
    : if ((k[4]==1)|(k[4]==0)){
    : k[4]=8;
    : }
    : k[5]=(k[4]+k[2])/ (int) Keys[5];
    : if ((k[5]==1)|(k[5]==0)){
    : k[5]=6;
    : }
    : k[6]=((int) Keys[6])/5;
    : if ((k[6]==1)|(k[6]==0)){
    : k[6]=k[6]+k[3];
    : }
    : k[7]=(int) Keys[7];
    : system("cls");
    : printf("Encrypting file.......");
    : time_t Time_1 , Time_2 ;
    :
    : time( &Time_1 );
    :
    : ifstream OpenFile(FileName);
    : ofstream SaveFile(SaveName);
    : while(!OpenFile.eof())
    : {
    : OpenFile.get(Byte);
    : Count++;
    : switch(Count){
    : case 1:
    : temp= (int) Byte;
    : temp+=k[4];
    : if(temp>255){
    : temp-=255;
    : }
    : Byte=(char) temp;
    : break;
    : case 3:
    : temp= (int) Byte;
    : temp+=k[2];
    : if(temp>255){
    : temp-=255;
    : }
    : Byte=(char) temp;
    : break;
    : case 5:
    : temp= (int) Byte;
    : temp+=k[1];
    : if(temp>255){
    : temp-=255;
    : }
    : Byte=(char) temp;
    : Count=0;
    : break;
    :
    : }
    : charToBin(Byte,convert);
    : for(i=0;i<8;i++){
    : for(j=0;j<10;j++){
    : Counter[j]++;
    : if (Counter[j]==k[j]){
    : convert[i]=!convert[i];
    : Counter[j]=0;
    : }
    : }
    : }
    :
    : SaveFile << binToChar(convert);
    : }
    :
    : SaveFile.close();
    : OpenFile.close();
    : time( &Time_2 );
    :
    :
    : printf("Time elapsed: %d seconds ",Time_2-Time_1 );
    : getch();
    :
    :
    :
    : }
    :
    : char binToChar(bool ToConvert[15]){
    : /*
    : Converts 8 bits to its asci equivalent.
    : Gets a boolean array (true = 1, false = 0),
    : and converts the 8 first bits to integer (decimal),
    : then casts the integer to character and returns it.
    : */
    : char Character;
    : int i,AscVal=0,expo;
    : expo=0;
    : for (i=7;i>-1;i--){
    : if(ToConvert[i]==true){
    : AscVal = AscVal + Exponent(2,expo);
    : }
    : expo++;
    : }
    : Character = (char) AscVal;
    :
    : return Character;
    : }
    :
    : int Exponent(int Base,int Expo){
    : /*
    : Calculates exponents of a certain base number.
    : So exponent(2,4) will return 2*2*2*2=16
    : */
    : int CalcInt,i;
    : CalcInt=1;
    : for(i=1;i<=Expo;i++){
    : CalcInt= CalcInt * Base;
    : }
    : return CalcInt;
    : }
    :
    : void charToBin(char Character, bool* MemAdress){
    : /*
    : Gets the memory adress of the boolean array and a character. It then
    : fills up the boolean array to represent a byte (equal to the character)
    : */
    : int i,Whole=(int) Character,Remain, Counter=7;
    :
    : while(Whole!=0){
    : Remain=Whole % 2;
    : Whole=(Whole-Remain) / 2;
    : MemAdress[Counter]=(bool) Remain;
    : //Above line creates warning(cast int to bool), and can be replaced by this:
    : //switch (Remain){
    : //case 0:
    : // MemAdress[Counter]=false;
    : // break;
    : //case 1:
    : // MemAdress[Counter]=true;
    : //}
    : Counter--;
    : }
    : Counter++;
    : if(Counter!=0){
    : for(i=0;i<Counter;i++){
    : MemAdress[i]=false;
    : }
    : }
    : }
    : [/code]
    :
    : a couple of questions:
    :
    : -How does the code look? I mean, I'm a beginner so I must be doing stuff completely wrong. Please correct it so I can learn from my mistakes.
    : -Can anyone tell me why it odesn't work for all files? I mean it seems to create a file with a different amount of bytes the the original file. It shouldn't do this at all (I read one character and write one character at a time.. so the amount of bytes should be the same)
    : -and last but not least... how good is this encryption? Would it be easy to crack? A series of "a" s run trough this algo returns:
    : "[italic]???????????????????????????????[/italic]" How hard would it be to crack this and return "aaaaaaaaaaaaa... " again?
    : -any speed tips? Takes 6 to 8 secs on my comp for 1 megabyte... bit to slow in my opninion.. 1 sec is my goal.
    :

  • : -Can anyone tell me why it odesn't work for all files? I mean it seems to create a file with a different amount of bytes the the original file. It shouldn't do this at all (I read one character and write one character at a time.. so the amount of bytes should be the same)

    don't have much time so i'll only answer this one halffast atm - make sure you're not reading in binary files as ascii txt.

    oh yea, it's C that your writing not C++ :)

    ------------------------------------------
    The art of war is not to die for your country, but to make the other bastard die for his!

  • [b][red]This message was edited by the PeterTheMaster at 2002-6-30 16:5:45[/red][/b][hr]


    : int i,AscVal=0,expo;
    : expo=0;

    guess(read).

    and didnt i give you a reasonable algo to convert a bool array to a char and vice versa? if you dont read theres no reason for me to write.

    and i dont see what your temp variable is good for.

    why do you put the algo in the main? are you never going to use it again?

    you might want fail where you use eof.

    : why convert[15] and not convert[8]?
    : temp should be unsigned char, so you dont have to do the wrapping manually.
    : declare variables as late as possible. why are they all on top? do you come from pascal?
    :
    :
    :
    : : As you probably know by know I'm a noob, but learning fast (asked a lot of stupid and less stupid questions on these boards the past 2 weeks).... I finally made my first proggy in C++ (after 2 weeks of learning). Here it is:
    : : [code]
    : : #include
    : : #include
    : : #include
    : : #include
    : : #include
    : : #include
    : :
    : : char binToChar(bool*);
    : : int Exponent(int,int);
    : : void charToBin(char, bool*);
    : :
    : : void main(void){
    : : char FileName[256],SaveName[256],Keys[9];
    : : int k[10];
    : : int Counter[10];
    : : int Count,temp,i,j;
    : :
    : : Count=0;
    : : Counter[0]=0;
    : : Counter[1]=0;
    : : Counter[2]=0;
    : : Counter[3]=0;
    : : Counter[4]=0;
    : : Counter[5]=0;
    : : Counter[6]=0;
    : : Counter[7]=0;
    : : Counter[8]=0;
    : : Counter[9]=0;
    : :
    : : bool convert[15];
    : : char Byte;
    : : printf("Please enter the filename for encryption:
    ");
    : : fflush(stdin);
    : : scanf("%s",&FileName);
    : : printf("Save file to:
    ");
    : : fflush(stdin);
    : : scanf("%s",&SaveName);
    : : printf("Please enter password (8 characters/numbers):
    ");
    : : fflush(stdin);
    : : scanf("%s",&Keys);
    : :
    : : k[0]=((int) Keys[0])/5;
    : : if ((k[0]==1)|(k[0]==0)){
    : : k[0]=3;
    : : }
    : : k[1]=((int) Keys[1]) - k[0];
    : : if (k[1] < 0){
    : : k[1] *= -1;
    : : }
    : : k[2]=((int) Keys[2])/10;
    : : if ((k[2]==1)|(k[2]==0)){
    : : k[2]=4;
    : : }
    : : k[3]=((int) Keys[3])/50;
    : : if ((k[3]==1)|(k[3]==0)){
    : : k[3]=7;
    : : }
    : : k[4]=((int) Keys[4])/(30+k[3]);
    : : if ((k[4]==1)|(k[4]==0)){
    : : k[4]=8;
    : : }
    : : k[5]=(k[4]+k[2])/ (int) Keys[5];
    : : if ((k[5]==1)|(k[5]==0)){
    : : k[5]=6;
    : : }
    : : k[6]=((int) Keys[6])/5;
    : : if ((k[6]==1)|(k[6]==0)){
    : : k[6]=k[6]+k[3];
    : : }
    : : k[7]=(int) Keys[7];
    : : system("cls");
    : : printf("Encrypting file.......");
    : : time_t Time_1 , Time_2 ;
    : :
    : : time( &Time_1 );
    : :
    : : ifstream OpenFile(FileName);
    : : ofstream SaveFile(SaveName);
    : : while(!OpenFile.eof())
    : : {
    : : OpenFile.get(Byte);
    : : Count++;
    : : switch(Count){
    : : case 1:
    : : temp= (int) Byte;
    : : temp+=k[4];
    : : if(temp>255){
    : : temp-=255;
    : : }
    : : Byte=(char) temp;
    : : break;
    : : case 3:
    : : temp= (int) Byte;
    : : temp+=k[2];
    : : if(temp>255){
    : : temp-=255;
    : : }
    : : Byte=(char) temp;
    : : break;
    : : case 5:
    : : temp= (int) Byte;
    : : temp+=k[1];
    : : if(temp>255){
    : : temp-=255;
    : : }
    : : Byte=(char) temp;
    : : Count=0;
    : : break;
    : :
    : : }
    : : charToBin(Byte,convert);
    : : for(i=0;i<8;i++){
    : : for(j=0;j<10;j++){
    : : Counter[j]++;
    : : if (Counter[j]==k[j]){
    : : convert[i]=!convert[i];
    : : Counter[j]=0;
    : : }
    : : }
    : : }
    : :
    : : SaveFile << binToChar(convert);
    : : }
    : :
    : : SaveFile.close();
    : : OpenFile.close();
    : : time( &Time_2 );
    : :
    : :
    : : printf("Time elapsed: %d seconds ",Time_2-Time_1 );
    : : getch();
    : :
    : :
    : :
    : : }
    : :
    : : char binToChar(bool ToConvert[15]){
    : : /*
    : : Converts 8 bits to its asci equivalent.
    : : Gets a boolean array (true = 1, false = 0),
    : : and converts the 8 first bits to integer (decimal),
    : : then casts the integer to character and returns it.
    : : */
    : : char Character;
    : : int i,AscVal=0,expo;
    : : expo=0;
    : : for (i=7;i>-1;i--){
    : : if(ToConvert[i]==true){
    : : AscVal = AscVal + Exponent(2,expo);
    : : }
    : : expo++;
    : : }
    : : Character = (char) AscVal;
    : :
    : : return Character;
    : : }
    : :
    : : int Exponent(int Base,int Expo){
    : : /*
    : : Calculates exponents of a certain base number.
    : : So exponent(2,4) will return 2*2*2*2=16
    : : */
    : : int CalcInt,i;
    : : CalcInt=1;
    : : for(i=1;i<=Expo;i++){
    : : CalcInt= CalcInt * Base;
    : : }
    : : return CalcInt;
    : : }
    : :
    : : void charToBin(char Character, bool* MemAdress){
    : : /*
    : : Gets the memory adress of the boolean array and a character. It then
    : : fills up the boolean array to represent a byte (equal to the character)
    : : */
    : : int i,Whole=(int) Character,Remain, Counter=7;
    : :
    : : while(Whole!=0){
    : : Remain=Whole % 2;
    : : Whole=(Whole-Remain) / 2;
    : : MemAdress[Counter]=(bool) Remain;
    : : //Above line creates warning(cast int to bool), and can be replaced by this:
    : : //switch (Remain){
    : : //case 0:
    : : // MemAdress[Counter]=false;
    : : // break;
    : : //case 1:
    : : // MemAdress[Counter]=true;
    : : //}
    : : Counter--;
    : : }
    : : Counter++;
    : : if(Counter!=0){
    : : for(i=0;i<Counter;i++){
    : : MemAdress[i]=false;
    : : }
    : : }
    : : }
    : : [/code]
    : :
    : : a couple of questions:
    : :
    : : -How does the code look? I mean, I'm a beginner so I must be doing stuff completely wrong. Please correct it so I can learn from my mistakes.
    : : -Can anyone tell me why it odesn't work for all files? I mean it seems to create a file with a different amount of bytes the the original file. It shouldn't do this at all (I read one character and write one character at a time.. so the amount of bytes should be the same)
    : : -and last but not least... how good is this encryption? Would it be easy to crack? A series of "a" s run trough this algo returns:
    : : "[italic]???????????????????????????[/italic]" How hard would it be to crack this and return "aaaaaaaaaaaaa... " again?
    : : -any speed tips? Takes 6 to 8 secs on my comp for 1 megabyte... bit to slow in my opninion.. 1 sec is my goal.
    : :
    :
    :







  • [b][red]This message was edited by the Darius at 2002-7-1 1:6:19[/red][/b][hr]
    : if your encryption algo is the same u asked about last time, i already wrote what i think of it. anything else than that prime number factorization thing cannot be considered safe.

    Here is a classic example where you need to check your facts.

    1) While Prime Number Factorization is considered safe, it is NOT PROVEN so. If some smart cookie comes along and figures out how to factor numbers really quickly then every system based on that problem is TOTALLY BROKEN. Also, quantum computers WILL break systems based on this (and other) problems as they can perform NP-complete problems in P time.

    2) Prime Number Factorization cryptosystems aren't the only cryptosystems out there. There are ones that have PROVEN breaking limits. Another popular cryptosystem is Elliptical Curve Cryptosystems which rely on a TOTALLY different problem. ECCs are also not PROVEN to be safe, but are considered safe (until someone breaks them... same goes for prime number factorization).

    3) The two cryptosystems above are PUBLIC KEY cryptosystems. Secret key cryptosystems usually work in a totally different manner. Also, a secret key cryptosystem would be more appropriate for this program. These cryptosystems typically rely on ciphers and prime number factorization has nothing to do with them.

    : because: the thing that has to be kept safe in your case is the encryption algorithm. if it is known, deriving the key is trivial.

    Peter is completely right here. You can find, at least, the basic algorithm for any modern cryptosystem online easily. Relying on the fact that the algorithm is unknown is a specific example of "security through obscurity". This is a very bad thing and should never be RELIED upon. A bad analogy could be the difference between you hiding in your house and you locking your house. While hiding from those mofia hitmen you forgot to pay is a more secure than standing in the middle of the street, it doesn't really stop them, it just slows them down. Technically, the key for a cryptosystem could be considered a case of "security through obscurity", but in this case the algorithm is benching on the fact that it would require MASSIVE processing power to clear up the "obscurity". Massive as in the universe could go through it's entire life-time a couple of times before you finish. This would be analogous (using the crappy analogy) to having like a 1000-digit combination lock on your front door. Technically, it's only slowing down the mobsters, but as it will take the rest of their natural lives and then MUCH more to find the correct combination it's not considered "security through obscurity". Back to computer land, getting the algorithm for a cryptosystem would be the relatively trivial process of reverse engineering an encryption/decryption program.


    :
    : in real cases, the algorithm can be (and often is) known, without the private key this doesnt help.
    :

    Well *splitting hairs*, it helps some. "security through obscurity" is still security albeit flawed. Obviously, you are never going to hit the right key brute-forcing if you are using the wrong algorithm. But as I stated this is just splitting hairs and anyways if the algorithm needs to be executed then it is known to the executor. In other words, you can't make a program that doesn't do what it says it does (at the machine code level).

    Anyways, for a REAL introduction to cryptography check out the Crypto-FAQ at www.rsa.com.

    Now that that's out of my system. Another flaw in your design is poor software engineering. There is no need for you to implement the Exponentiation algorithm as it is already defined and standard (the pow function of the cmath header).

    Also, this is not a problem so much, I'm just pointing it out. Anyways from my previous reply it's a moot point, but...
    Your Exponentiation function is inefficient. More efficient versions can be made by exploiting the fact that (x^2)^2 = x^4 (well thats a specific example of a general case). Another specific example will make it clear how it works (though not necessarily how to implement it).

    Let's say you want a^17. Instead of multiplying 17 a's together you could...
    x=a;
    x=x*x;
    //x==a^2
    x=x*x;
    //x==a^4
    x=x*x;
    //x==a^8
    x=x*x;
    //x==a^16
    a=x*a; //a^16*a==a^17

    This only requires 5 multiplies.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion



  • I have a background with pascal indeed.... been programming with VB for about 2 years tough.. so its 2 years since I touched Pascal ;)

    anyway.. the 15 is just because the algo initially worked a little different. Then it didn't nescessarely produce 8 bits.. but for example 12. The first 8 bits were then converted, and the remaining were moved to the beginning of the array. Looked like pretty good encryption, but the files became a lot bigger, so I changed that. I should change it back to 8 now offcourse.

  • You gave me a reasonable algo for that indeed.. (just checked the boards again)... sorry must've overlooked it. I'll do it like you said back then.

  • [b][red]This message was edited by the blah at 2002-7-1 14:47:41[/red][/b][hr]
    Why can't my algo be public? There are 256*256*256*256*256*256*256*256=18446744073709551616 possible keys...(in theory, becuase one could narrow it down to the alphabet and the numbers so 62^8=218340105584896 possibilitys left). If another key is used, the result will be garbage. So one would have to check every combination, and altough that can be automated....it would still take a lot of time. Isn't this the same with other algo's ... brute forcing is always a possibility... or am I way off here?



  • : : declare variables as late as possible. why are they all on top? do you come from pascal?

    Why do you quote the entire thread in all of your replies? Do you come from Fraggle Rock?

    j/k ;-)

    Flut

  • : Why do you quote the entire thread in all of your replies? Do you come from Fraggle Rock?

    seems to be normal on this board. its faster than selecting single quotes or delete everything.
    whats Fraggle Rock? sounds like muppets show. do you come from kindergarten?
  • : [b][red]This message was edited by the blah at 2002-7-1 14:47:41[/red][/b][hr]
    : Why can't my algo be public? There are 256*256*256*256*256*256*256*256=18446744073709551616 possible keys...(in theory, becuase one could narrow it down to the alphabet and the numbers so 62^8=218340105584896 possibilitys left). If another key is used, the result will be garbage. So one would have to check every combination, and altough that can be automated....it would still take a lot of time. Isn't this the same with other algo's ... brute forcing is always a possibility... or am I way off here?


    Your encryption doesn't have that many possible keys.

    The crap you do to Key[] to get k[] only weakens your algorithm (which is weak anyways). Why? First off, I don't need to know Key[] I only need to know k[]. Next, every time you divide you lessen the number of significant bits. So, if you divide key[1] by four let's say, you'd make the bottom two bits of key[1] not matter at all later. And as I said, you only need to know k[] and various parts of k[] are going to have less of a dynamic range, so that lessens the number of possible combinations.

    Next keysize doesn't matter if the algorithm's no good. I could use a massive key with xor encryption, that doesn't mean it's safe.

    Your "encryption" algorithm is probably very susceptible to plaintext attacks. Plaintext attacks are where the attacker knows the plaintext and the ciphertext and s/he is trying to find the secret key. I'm pretty sure that the entire secret key could be discovered using just ONE plaintext attack. (k[] not Key[] as I said, Key[] isn't necessary and as some bits of Key[] aren't used you can't find out what those bits were)

    I have a question. How do you decrypt the ciphertext? Somehow I don't think just running it through the algorithm again will do it.

    Anyways, read that cryptofaq in my other post.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • [b][red]This message was edited by the PeterTheMaster at 2002-7-2 0:17:34[/red][/b][hr]

    : I have a question. How do you decrypt the ciphertext? Somehow I don't think just running it through the algorithm again will do it.
    :

    why not? he only flips some bits. running it through again will flip them back. its an xor algorithm. (he seems to think flipping bits several times makes them more encoded :-) )
    he seems to think: if i dont understand my algorithm any more, how should anyone be able to decrypt?
    i told him before to use xor to manipulate the chars instead of these strange bool arrays. i think this would make it more obvious what happens.
    [code]
    while(OpenFile.get(Byte)){
    Count++;
    switch(Count){
    case 1: Byte+=k[4]; break;
    case 3: Byte+=k[2]; break;
    case 5: Byte+=k[1]; Count=0; break;
    }
    for(int i=0;i<8;i++){
    for(int j=0;j<10;j++){
    Counter[j]++;
    if (Counter[j]==k[j]){
    Byte^=(1<<(7-i));
    Counter[j]=0;
    }
    }
    }

    SaveFile << Byte;
    }
    [/code]
    : Your "encryption" algorithm is probably very susceptible to plaintext attacks.

    yes, i also told him this before: only very view(i have no time for maths now) subsequent bytes are enough to derive the key with one try.










  • :
    : : Why do you quote the entire thread in all of your replies? Do you come from Fraggle Rock?

    : seems to be normal on this board. its faster than selecting single quotes or delete everything.

    Faster yes, better no. It certainly isn't the norm for anyone that knows how to use message boards. The only person it's faster for is the person writing the reply..... for the rest of us it makes reading them a LOT slower.

    : whats Fraggle Rock? sounds like muppets show. do you come from kindergarten?
    :

    Sorry about that. Must be a local insult...... try not to get your knickers in a twist though.... it was meant kindly ;-)

    Flut

  • : he seems to think: if i dont understand my algorithm any more, how should anyone be able to decrypt?
    : i told him before to use xor to manipulate the chars instead of these strange bool arrays. i think this would make it more obvious what happens.


    euh... sorry if I get the tone of your message wrong here... but its clear you're looking down on me.
    Now.. I wasn't saying I made this good encryption algo.. and I wasn't saying I will use it or something. I wrote this to train the programming language. Remember I only have a couple of weeks C experience. The intention was just writing something that works, not to reinvent the wheel. My question "how hard is it to crack this" and "why can't my algo be public" are not statements like "it is a good algo".. NO! I was merely looking for an explanation on how to crack encryption like this. I posted in this way becuase if I asked it this way I tought I'd get more relevant answers... however, all you seem to do is look down and don't provide me real answers "I said this before" doesn't really do it for me. And sure you said stuff before, only always with that same undertone. I wonder how you started out. I'd like to see you're questions after your 2-3 first weeks of programming.
    This isn't ment with disrespect towards you. You do help out lots of people on these boards, you helped me too, just wish you dind't assume so much and left the attitude behind.
  • A few comments on your code:

    [code]
    //void main(void)
    // Should of course be:
    int main(int argc,char* argv[])
    {
    char FileName[256],SaveName[256],Keys[9];
    int k[10];
    int Counter[10]={0}; // Init all 10 values to 0
    int Count,temp,i,j;

    Count=0;
    bool convert[15];
    char Byte;
    printf("Please enter the filename for encryption:
    ");
    // fflush(stdin); //
    autoflushes for printf
    scanf("%s",&FileName);
    printf("Save file to:
    ");
    // fflush(stdin);
    scanf("%s",&SaveName);
    printf("Please enter password (8 characters/numbers):
    ");
    // fflush(stdin);
    scanf("%s",&Keys);

    // As for the encryption - well.. Other people have
    // discussed that quite a lot already :o) and I'm not
    // into encyption anyway...

    system("cls");
    printf("Encrypting file.......");
    time_t Time_1 , Time_2 ;

    time( &Time_1 );

    // So - you switch to streams. Yeah - why not.
    // It's good practice :o)
    ifstream OpenFile(FileName);
    ofstream SaveFile(SaveName);
    while(!OpenFile.eof())
    {
    OpenFile.get(Byte);
    // Count++;
    // switch(Count){ 'Optimise' to:
    Count++;
    switch(++Count){
    case 1:
    // You have no deafult case. Is that right??
    // ...

    // No need to pass array - just pass pointer.
    char binToChar(bool *pToConvert){
    /*
    Converts 8 bits to its asci equivalent.
    Gets a boolean array (true = 1, false = 0),
    and converts the 8 first bits to integer (decimal),
    then casts the integer to character and returns it.
    */
    // Enjoy this implemention. A bit faster and shorter
    unsigned char Character=0;
    int i=8;
    while(i--){
    Character<<=1; // Shift left
    if(ToConvert[i])){
    Character++; // Mask a bit
    }
    }
    return (signed char)Character;
    }

    // So this is the reverse function. Again - shifting comes in handy:
    void charToBin(char Character, bool* MemAdress){
    /*
    Gets the memory adress of the boolean array and a character. It then
    fills up the boolean array to represent a byte (equal to the character)
    */
    unsigned char uc=(unsigned char)Character;

    int i=8;
    while(i--)
    {
    MemAdress[i]=(if(Character&0x01)); // Test for bit set
    Character>>=1; // Shift right
    [/code]

    : -How does the code look?

    Not too bad at all.

    :I mean, I'm a beginner so I must be doing stuff completely wrong.

    You should definitely read something about shifting.

    The two operators << and >> do other stuff than being overloaded :o)

    E.g.
    2<<4 = 2 shifted four times left = 16
    24>>1 = 24 shifted one time right = 12

    : -Can anyone tell me why it odesn't work for all files? I mean it seems to create a file with a different amount of bytes the the original file.


    You should debug the code. I haven't had time to look into the exact behaviour, but do think that something not quite right was going on in your code (the part that I didn't comment)

    : How hard would it be to crack this and return "aaaaaaaaaaaaa... " again?

    Hehe... I'm not sure that you algo takes care of that. Example:
    I have encrypted my harddisk by multiplying all bytes on it with 0. Unfortunately, noone has ever been able to decrypt my encrypted data ;-)

    Actually, I've heard about some password verifiers that don't even save the passwords, but simply what the passwords are encrypted into (with a routine that is a bit gentler than my example with *0 above, but still offers no decryption). This way, some passwords also turns into the same one, but it is so unlikely that it doesn't matter. More importantly, noone can get the passwords back, which is (normally) good.

    : -any speed tips? Takes 6 to 8 secs on my comp for 1 megabyte... bit

    Hmm... With the things I've optimsed, it should be quicker.

    Good luck
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories