![]() |
|
#1
|
||||
|
||||
Vernam encryption cipherI have been working on a program that is supposed to encrypt and decrypt text files using a vernam cipher: http://en.wikipedia.org/wiki/Vernam_..._Vernam_cipher
I have been working on this for about a week, and have gotten the program down to where when it encrypts and decrypts files, it decrypts them with only a few errors. Here is my problem: When working with the space (" ") character, it seems to have a lot of problems. It also has problems processing files with more than one line; it will decrypt them, but with several errors. Here is an example of a typical decryption on a one-line file with no spaces: abcdefghijklmnopqrstuvwxyz to abcdefgkijklmnepqrstnvwxyz Last, it seems to have trouble with all characters that are not numbers or letters. The way I have it set up, all characters should be treated the same, and though i have worked on this problem for a long time, I have not been able to come up with a solution. Anyway, here is the code. I know that posting the entire program is generally frowned upon, but in this case the program is very difficult to understand with only small parts of it to work with. Everything that may require some explanation is commented. Any help is greatly appreciated! CPP / C++ / C Code:
|
||||
|
#2
|
||||
|
||||
|
Problem is you haven't given us any explanation as to what is wrong. "Errors" is just too broad of a description...
Does the encryption part work correctly? Does the decryption part work correctly? Output values from the program thoughout the process so you can see exactly where the problem is. Since I (and most here I'm guessing) don't know what a "vernam cipher" is, I can't tell you whether you coded the algorithm correctly. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#3
|
||||
|
||||
|
My apologies. Though I did provide a link, Wikipedia's definition of a Vernam Cipher isn't very clear. I can try to explain it myself:
Normal encryption methods use a form of encryption known as "substitution." This doesn't require much explanation; an example would be: a=r. Then, when encrypting the file, go through it and change all instances of a to r. When decrypting the file, change all instances of r to a. This, however, is very easy to crack through several methods. A vernam cipher, however, has been deemed uncrackable. In all its years of use, it has never been cracked. Here is an example: Say, in the text that you want to encrypt, the first letter is a. To reach "a" in the alphabet, one must advance 0 spaces. That's pretty simple. The first step in encryption is to generate a random key comprised of letters, numbers, and all other characters (in this case on the keyboard). This key must be equal to the length of the document that is to be encrypted. Now, let's say that the first letter of the random key is "c." One must advance 2 spaces in the alphabet to get to c. So, you would take 2(c)-0(a) to get the answer: 2. When you advance 2 letters into the alphabet, you get c. So, the encrypted letter is c. To reverse this process (for decryption), all you would have to do is take the letter from the encrypted text (c) and the letter from the key (c). Both of these are reached by advancing two spaces in the alphabet. So, 2-2=0. One would check 0, and seeing that the character reached when advancing into the alphabet 0 spaces is "a," one knows that the decrypted character is "a." Here is the list of values assigned to each character on the keyboard: a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10 l 11 m 12 n 13 o 14 p 15 q 16 r 17 s 18 t 19 u 20 v 21 w 22 x 23 y 24 z 25 A 26 B 27 C 28 D 29 E 30 F 31 G 32 H 33 I 34 J 35 K 36 L 37 M 38 N 39 O 40 P 41 Q 42 R 43 S 44 T 45 U 46 V 47 W 48 X 49 Y 50 Z 51 ` 52 1 53 2 54 3 55 4 56 5 57 6 58 7 59 8 60 9 61 0 62 - 63 = 64 ~ 65 ! 66 @ 67 # 68 $ 69 % 70 ^ 71 & 72 * 73 ( 74 ) 75 _ 76 + 77 [ 78 ] 79 \ 80 { 81 } 82 | 83 ; 84 ' 85 : 86 87 , 88 . 89 / 90 < 91 > 92 ? 93 Now that there is a better explanation of the Vernam Cipher, I will attempt to answer some of the other questions about this program. Quote:
Sorry. When I say errors, what I mean that when decrypting the file manually using the above value assignments, there are usually anywhere from 1-3 errors, errors meaning that the decrypted characters are not the same as the ones from the original file. edit: Also, these problems happen more often when decrypting/encrypting files with more than one line, spaces, and non-numeric, non-alphabetic characters. Quote:
Quote:
I think that should clear things up significantly. I do have one further question: The program cannot handle the " character, because obviously typing """ in c++ to indicate a quote as a character does not work. Is there a way around this? Again, any help is greatly appreciated! Thanks! |
|
#4
|
|||||
|
|||||
|
Quote:
Quote:
Quote:
Quote:
Quote:
CPP / C++ / C Code:
I see a couple things in your code that are bad to do. One is using dfile.eof() as you're doing. It the same as feof() in C if memory serves. Check this information for the reason. Look into the C++ equivalent of fseek()/ftell() instead. Somone here can post the actual method names. The second is a pet peeve of mine. This should explain... And read the tutorial on Formatting Code. Your formatting is halfway there, but needs work. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#5
|
|||||||
|
|||||||
|
Quote:
I agree completely. I will do this in the future. Quote:
Indeed it may, and the reason that I didn't do this is because I didn't think to try the hex equivalent. However, as I can't do this with the method I was using, would strcpy work? It seems as if it would return a const char to char error. I would check this myself but currently don't have access to a compiler. Quote:
The manual encryption always works; it's something in the program. Quote:
Thanks. Again, can this be stored with strcpy? Quote:
Essentially, that is what I am doing. To get around the problem of not being able to relate a const char to a number, I assigned the chars to their respective numbers in the array. EX: alph[0]="a". Quote:
I will check this out. I do need to clean up my code, that has always been a bit of an issue of mine Quote:
I will look at this as well. Thanks for the help, I'll try to implement the end of line and tab characters ASAP. |
|
#6
|
||||
|
||||
|
Quote:
But yes, strcpy() only needs the ending '\0' to signify the end of a string. It will copy everything up to the ending null. Quote:
Quote:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#7
|
||||
|
||||
|
Quote:
Actually, I had no idea that const chars were numbers. Are you saying what you posted earlier CPP / C++ / C Code:
would actually work? If I could implement it into a program by doing something like: CPP / C++ / C Code:
with the output as 'b?' This would greatly simplify this program. Again, I apologize for not attempting this myself. I will have access to a compiler later tonight. |
|
#8
|
||||
|
||||
|
[quote=Zorachus]Actually, I had no idea that const chars were numbers. [quote=Zorachus]
Of course they are. Everything in computers are just numbers... Quote:
CPP / C++ / C Code:
Quote:
Concept: Make two arrays, encrypt[] and decrypt[]. For each character you load the encrypt[] array with your encryption value, CPP / C++ / C Code:
Load decrypt[] with the reverse, of course... Will this fit in with this encryption technique? __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#9
|
||||
|
||||
|
Well, after spending almost four hours trying to make it fit this algorithm, I would have to say no. Defining the character numbers myself seems to be much easier, mainly because it is in a more chronological order, which makes it easier to handle with loops. I'm still working on it though, thanks for the advice!
|
|
#10
|
|||
|
|||
|
Quote:
I have a few suggestions: 1. Make a test program that reads chars from one file and puts each char to another file. Make absolutely certain that the result has exactly the number of chars as the original!!! Your method does not do this. I suggest infile.get() and outfile.put() to read and write a character at a time (Don't forget to open the files in binary mode. If you are on a Windows platform, it makes a difference.) Don't do anything else until you can perform this step with large and small files. Make sure the resultant output file has exactly the same length (and contents) as the input file. If hyou can't do this, then I don't see any hope of ever recovering an original file from an encrypted one. 2. Since the Vernam algorithm works on a character "stream", you don't have to read the entire file into an array and generate a key array of that size, etc., etc. The original Vernam method worked on a character at a time using mechanical teletype and relay logic. Memories of the size of arbitrarily long messages simply weren't available then, and are not necessary now for this algorithm. Also, I can't see where you performed the exclusive-or operation that the Vernam algorithm specifies. 3. You say that you are using text files, but text files have non-printing characters: newlines, carriage-returns, tabs, and possibly others. Why not make an algorithm that will work with any input file? If you want to restrict the algorithm's usefulness to printable ascii characters, you still must take into account certain control characters --- otherwise how could you reproduce your original file, since it undoubtedly has control characters? 4. Here is a possible approach for encryption: Code:
This way, you are generating the key, using the key for encryption, and writing the key file a byte at a time, all in one pass through the input, and with no memory arrays to clutter up the landscape. That's kind of the point of a streaming algorithm. Now if your instructor told you to make these large arrays for the different items, you can still do it, but the calculations are the same. 4. Then for decryption: Code:
Again, the processing is done a byte at a time with one pass through the message. In summary: I would write two separate programs to test encryption and decryption. I would put the file names hard-coded in the files to keep from having to enter file names for test purposes. I would use different file names so that I wouldn't destroy the original file each time I ran the program (your encoding program branch destroys the original file). Then, after the algorithms for encrypting and decrypting are thoroughly tested, I would put them together into whatever program organization that the assignment specified. Regards, Dave Last edited by davekw7x : 31-Jul-2005 at 16:20.
|
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| encryption stuff | Venom | C Programming Language | 8 | 12-Apr-2005 15:33 |
| need help with encryption | bobthesled | C Programming Language | 9 | 15-Feb-2005 12:24 |
| Encryption implementation issue | bigbangman | C Programming Language | 6 | 02-Sep-2004 12:21 |
| Help with binary files (encryption?) | pablowablo | C++ Forum | 6 | 28-Apr-2004 23:47 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The