Vigenere Cipher II - how to use files

  • Thread starter Piotr Turkowski
  • Start date
P

Piotr Turkowski

Hi,
The code is here: http://free.ud.pl/~piotr/data/vigenere.zip
Its my program for decrypting and encrypting text.

Shot polish course:
szyfrowanie (szyfruj) - encrypting (text i want to code ->
(*(^%&^GHJBBVvkek)
odszyfrowanie (deszyfrowanie) - decrypting ( *^BNHJ*&% - > secret text)
obsluga plikow - using files

If you delete case 4 (obs³ugê plików) from the menu and the bad function
the program will work.

This is the problem. I don't know how to use files. I want to make a
function in which you will enter a path for incoming file and outgoing
file.

There are 4 possibilities:
1) you have entered both paths, the program will encrypt or decrypt text
from file_in to file_out
2) you have enetered path for file_in, the text will be encrypted or
decrypted and showed on display
3) you have entered path for file_out, the text will be taken from
keyboard and writted into file_out
4) no paths, the text will be got form keyboard and showed on display

In this function first I want to check if paths are correct.

If you understood me, please help me with this. I'm tired of this.

BTW:
How to enter a password, and show stars(*) on screen ?
How to make a decision in a menu without confirming it by enter?
 
M

Mike Wahler

Piotr Turkowski said:
Hi,
The code is here: http://free.ud.pl/~piotr/data/vigenere.zip
Its my program for decrypting and encrypting text.

Shot polish course:
szyfrowanie (szyfruj) - encrypting (text i want to code ->
(*(^%&^GHJBBVvkek)
odszyfrowanie (deszyfrowanie) - decrypting ( *^BNHJ*&% - > secret text)
obsluga plikow - using files

If you delete case 4 (obsluge plików) from the menu and the bad function
the program will work.

This is the problem. I don't know how to use files.


Where's your texbook? :)
I want to make a
function in which you will enter a path for incoming file and outgoing
file.

There are 4 possibilities:
1) you have entered both paths, the program will encrypt or decrypt text
from file_in to file_out
2) you have enetered path for file_in, the text will be encrypted or
decrypted and showed on display
3) you have entered path for file_out, the text will be taken from
keyboard and writted into file_out
4) no paths, the text will be got form keyboard and showed on display

I'd probably approach it something like this
(not compiled or tested):

#include <stdio.h>

void func(const char *in_path, /* Pass NULL for stdin */
const char *out_path) /* Pass NULL for stdout */
{
FILE *in = in_path ? fopen(in_path, "r") : stdin;
FILE *out = out_path ? fopen(out_path, "w") : stdout;

if(in && out)
{
/* do your stuff */

if(in && in != stdin)
fclose(in);

if(out && out != stdout)
fclose(out);
}
else
{
/* could not open either input, output, or both.
Handle error. */
}

}

void callit(void)
{
func("inputpath", "outputpath"); /* case 1) */

func("inputpath", NULL); /* case 2) */

func(NULL, "outputpath"); /* case 3) */

func(NULL, NULL); /* case 4) */
}

Another possibility is to use an 'emtpy string' ("")
instead of NULL to indicate stdin and stdout, but
the idea is the same.
In this function first I want to check if paths are correct.

"Correctness" of a 'path name' is in the domain of your
operating system, so you'll need to check your documentation.

If by "correct" you mean does e.g. the input file exist
or the output file not exist, See the documentation for
the 'open mode' (second argument) of 'fopen()'. 'fopen()'
returns a NULL pointer upon failure, a non-NULL pointer
upon success. 'stdin' and 'stdout' are automatically
'open' at program startup (and don't try to close them! :)).
If you understood me, please help me with this. I'm tired of this.

BTW:
How to enter a password, and show stars(*) on screen ?

Not possible with standard C. Perhaps your implementation
provides an extension for this. Consult your documentation.
How to make a decision in a menu without confirming it by enter?

Same answer as to the the first 'BTW' question.

HTH,
-Mike
 
D

David Rubin

Piotr said:
Hi,
The code is here: http://free.ud.pl/~piotr/data/vigenere.zip
Its my program for decrypting and encrypting text.

Shot polish course:
szyfrowanie (szyfruj) - encrypting (text i want to code ->
(*(^%&^GHJBBVvkek)
odszyfrowanie (deszyfrowanie) - decrypting ( *^BNHJ*&% - > secret text)
obsluga plikow - using files

Seems to me that encryption is overkill :)
BTW:
How to enter a password, and show stars(*) on screen ?

Cannot be done in standard C.
How to make a decision in a menu without confirming it by enter?

You are asking about reading a keypress. This cannot be done in standard
C. Please read the FAQ.

/david
 
M

Mike Wahler

Piotr Turkowski said:
I have never guessed what 'overkill' mean,

"Overkill" means essentially the application of much
more and/or complex than necessary work and/or resources
to a problem. (e.g. using a shotgun to kill insects is
'overkill').

I'm not familiar with 'Vigenere Cipher', so I'm not
qualified to judge any code for it in that respect.

-Mike
 
D

David Rubin

Mike said:
"Overkill" means essentially the application of much
more and/or complex than necessary work and/or resources
to a problem. (e.g. using a shotgun to kill insects is
'overkill').

I'm not familiar with 'Vigenere Cipher', so I'm not
qualified to judge any code for it in that respect.

In any case, it was a joke. Since I can't read Polish, your Polish phrases seem
well encrypted to me.

FWIW, IIRC, the Vigenere Cipher is a multi-level substitution cipher; basically
a table of different substitution ciphers. It can be cryptanalyzed relatively
easily using a modified letter frequency attack. Since we already know the
cleartext is in Polish in your application, you are at a significant disadvantage...

/david
 
P

Piotr Turkowski

David said:
FWIW, IIRC, the Vigenere Cipher is a multi-level substitution cipher;
basically a table of different substitution ciphers. It can be
cryptanalyzed relatively easily using a modified letter frequency
attack. Since we already know the cleartext is in Polish in your
application, you are at a significant disadvantage...

Well, actualy it's not that easy. Original Vigenere Cipher is made of 26
alphabets, my one is made of 94 alphas.. If you wanna try, I can't
encrypt 10 letter fraze in my program, and you can try to decrypt it.
 
B

Brian MacBride

Piotr Turkowski said:
Hi,
The code is here: http://free.ud.pl/~piotr/data/vigenere.zip
Its my program for decrypting and encrypting text.

Shot polish course:
szyfrowanie (szyfruj) - encrypting (text i want to code ->
(*(^%&^GHJBBVvkek)
odszyfrowanie (deszyfrowanie) - decrypting ( *^BNHJ*&% - > secret text)
obsluga plikow - using files

If you delete case 4 (obs³ugê plików) from the menu and the bad function
the program will work.

This is the problem. I don't know how to use files. I want to make a
function in which you will enter a path for incoming file and outgoing
file.

There are 4 possibilities:
1) you have entered both paths, the program will encrypt or decrypt text
from file_in to file_out
2) you have enetered path for file_in, the text will be encrypted or
decrypted and showed on display
3) you have entered path for file_out, the text will be taken from
keyboard and writted into file_out
4) no paths, the text will be got form keyboard and showed on display

In this function first I want to check if paths are correct.

If you understood me, please help me with this. I'm tired of this.

Well you do have a problem here...

You have...

void szyfruj(){

int ii, c, i, s, p, npass, nnpass;
char *pass;
char ALPHA[94][94]={0};

wypelnij_tab(ALPHA);

printf("Podaj haslo: ");
gets(pass);
for (npass=0; pass[npass] != '\0';++npass); /* liczenie ilosci znakow */

....
}

Notice there is no storage allocated for 'pass'. Your code is crawling with this
error. Some compilers will let you get away with this so I can get...

/*
1. Szyfrowanie tekstu.
2. Deszyfrowanie tekstu.
3. Statystyka.
4. Obsluga plikow.
5. Zakonczenie programu.

Menu: 1
Podaj haslo: abc
Podaj tekst do zaszyfrowania:
Hello
+IQOS
*/

Others have commented on your 'overkill'. I take that to mean 'overly
complicated'.
Consider...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum DIR {EN, DE};
const char START = ' ', END = '~';

void cipher (const enum DIR);

int main (void) {

/* menu programu */
int menu;
for (;;) {
printf ("\n");
printf ("1. Szyfrowanie tekstu.\n");
printf ("2. Deszyfrowanie tekstu.\n");
printf ("3. Zakonczenie programu.\n");
printf ("\n\tMenu: ");
scanf ("%d", &menu);
getchar ();

switch (menu) {
case 1:
cypher (EN); /* ENcode */
break;
case 2:
cypher (DE); /* DEcode */
break;
case 3:
return 0;
default:
break;
}
}
}

void cipher (const enum DIR d) {
const int len = END - START, pos = d ? len : 0;
char pass[40], *p;
int c;

printf ("Podaj haslo: ");

fgets (pass, sizeof pass, stdin);
/* May include a newline ('\n') */

p = strchr (pass, '\n');
if (p)
/* Lose the newline ('\n') */
*p = '\0';
p = pass;

printf ("Podaj tekst:\n");

while ((c = getchar ()) != '\n')
if (c >= START && c <= END) {
const int i = pos - *p + START;
printf ("%c", (abs (i) + c - START) % len + START);
if (!*++p)
p = pass;
}
else
printf ("%c", c);
putchar ('\n');
}

/*
1. Szyfrowanie tekstu.
2. Deszyfrowanie tekstu.
3. Zakonczenie programu.

Menu: 1
Podaj haslo: abc
Podaj tekst:
Hello
+IQOS

1. Szyfrowanie tekstu.
2. Deszyfrowanie tekstu.
3. Zakonczenie programu.

Menu: 2
Podaj haslo: abc
Podaj tekst:
+IQOS
Hello
*/

Pardon my Polish!

Regards

Brian
 
R

RoRsOaMrPiEo

03 Mar 2004 17:47 -0500 said:
In any case, it was a joke. Since I can't read Polish, your Polish phrases seem
well encrypted to me.

FWIW, IIRC, the Vigenere Cipher is a multi-level substitution cipher; basically
a table of different substitution ciphers. It can be cryptanalyzed relatively
easily using a modified letter frequency attack. Since we already know the
cleartext is in Polish in your application, you are at a significant disadvantage...

/david

if you make a XOR first of "vigenere" ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top