8 Replies - 2858 Views - Last Post: 20 September 2012 - 07:54 AM Rate Topic: -----

#1 ashishnehete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-September 12

Encryption code giving different output after the first character

Posted 17 September 2012 - 03:58 AM

These were the instructions i followed
Encryption
Before you start encrypting or decrypting, initialize a random number generator with the
encryption key. You will then encrypt the file, letter by letter.
If the letter is not in the following string
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
then it is not encrypted and output as is.

If it is then it is encrypted in the following fashion.
1. Let length be the number of letters in the above string.
2. Obtain the next number from the random number generator. Call this rand.
3. Set rand to equal rand modulus length. This will give a number in the range 0 to
length-1.
4. Find the index location for the letter you are encrypting in the above string. For example,
the letter a is found at location 0 and the letter A at location 26.
5. Add rand to the location and then take the modulus length of the result. For example, if
we start with c at location 2 and rand = 5 then we get a (2 + 5) modulus length =
7.
6. The result is the location for the encrypted letter. Continuing with the example in step 5,
the value of 7 corresponds to h in the string. The letter c will be encrypted as h.

For example, if the input file contains the following

1 thing = $1000.00
Then the output file will look like
L URXqf = $t1ng.lc

When using on the encryption key 3412.

It is left as an exercise for the student to determine how to reverse the encryption and decrypt
the file.
One final point. Do not load the entire file into your program and then try to encrypt/decrypt
it. Read the file a letter at a time, encrypt/decrypt each letter and then write to the output file.

This is the code i wrote:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Encryptions
{
    class Crypt
    {
        private string CharCheckString, line;
        private char[] CharCheck;
        private uint length;//For length of CharCheck Array
        private uint rand;
        private char inputChar, EncChar;
        private int charIndex, EncCharLoc;
        private bool isExist;


        public Crypt()
        {
            CharCheckString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            CharCheck= CharCheckString.ToCharArray();
            length = (uint)CharCheck.Length;
            


        }

        public void Encryption(uint key, string FileName, string OutputFile)
        {
            RandonNoGen RNG = new RandonNoGen(key);
            rand = RNG.Next(); // rand=1896325970
            Console.WriteLine(rand);
            rand = rand % length;//length=62
            
            
            long offset;

            using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read))
            {
                for (offset = 0; offset < fs.Length; offset++)
                {
                    fs.Seek(offset,SeekOrigin.Begin);
                    inputChar=Convert.ToChar(fs.ReadByte());
                    
                    charIndex=Array.IndexOf(CharCheck,inputChar);
                    EncCharLoc= (int)((charIndex+rand) % length);

                    isExist=Array.Exists(CharCheck, element => element == inputChar);
                    if(isExist)
                    {
                        EncChar=CharCheck[EncCharLoc];
                        Console.WriteLine("Encrypted Character {0} {1}",EncChar,inputChar);
                    }
                    else{
                        EncChar=inputChar;
                        Console.WriteLine("Encrypted Character {0} {1}",EncChar,inputChar);
                    }
                }
            
            }
         
            }



        }
    }


It seems that the first character i get encrypted is correct but after that its messed up!
Can you help pointing out my mistakes here!?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Encryption code giving different output after the first character

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Encryption code giving different output after the first character

Posted 17 September 2012 - 04:19 AM

This would be a good time to learn how to debug your code, so you can figure these kinds of things out yourself.
Was This Post Helpful? 0
  • +
  • -

#3 ashishnehete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-September 12

Re: Encryption code giving different output after the first character

Posted 17 September 2012 - 06:51 AM

Quote

how to debug your code

It helped!
I think i managed to shorten scope of my problem to these two lines
 
charIndex=Array.IndexOf(CharCheck,inputChar);
EncCharLoc= (int)((charIndex+rand) % length);


but still have my doubts
so running it again and again to find the issue
Was This Post Helpful? 0
  • +
  • -

#4 ashishnehete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-September 12

Re: Encryption code giving different output after the first character

Posted 17 September 2012 - 06:10 PM

I realized my mistake while following the instruction i changed the code
but still it hasn't fixed my problem
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment_1
{
    class Crypt
    {
        private string CharCheckString, line;
        private char[] CharCheck;
        private uint length;//For length of CharCheck Array
        private uint rand;
        private char inputChar, EncChar;
        private int charIndex, EncCharLoc, temp;
        private bool isExist;


        public Crypt()
        {
            CharCheckString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            CharCheck= CharCheckString.ToCharArray();
            length = (uint)CharCheck.Length;
            


        }

        public void Encryption(uint key, string FileName, string OutputFile)
        {
            RandonNoGen RNG = new RandonNoGen(key);
            //rand = (int)RNG.Next();
            //Console.WriteLine(rand);
            //rand = (rand % length);
            
            
            long offset;

            using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read))
            {
                for (offset = 0; offset < fs.Length; offset++)
                {
                    fs.Seek(offset,SeekOrigin.Begin);
                    inputChar=Convert.ToChar(fs.ReadByte());

                    rand = RNG.Next();
                    Console.WriteLine(rand);
                    rand = (rand % length);

                    charIndex=Array.IndexOf(CharCheck,inputChar);
                    temp = (int)(charIndex + rand);
                    EncCharLoc= (int)(temp % length);

                    isExist=Array.Exists(CharCheck, element => element == inputChar);
                    if(isExist)
                    {
                        EncChar=CharCheck[EncCharLoc];
                        Console.WriteLine("Encrypted Character {0} {1}",EncChar,inputChar);
                    }
                    else{
                        EncChar=inputChar;
                        Console.WriteLine("Encrypted Character {0} {1}",EncChar,inputChar);
                    }
                }
            
            }
         
            }



        }
    }


Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Encryption code giving different output after the first character

Posted 17 September 2012 - 09:45 PM

In line 32 you use a class that is not part of the .NET library, nor is it provided. Makes it hard to recreate the results without it.

This post has been edited by Momerath: 17 September 2012 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#6 ashishnehete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-September 12

Re: Encryption code giving different output after the first character

Posted 17 September 2012 - 11:36 PM

View PostMomerath, on 17 September 2012 - 09:45 PM, said:

In line 32 you use a class that is not part of the .NET library, nor is it provided. Makes it hard to recreate the results without it.

Thanks Momerath I figured out where i went wrong during my encryption.
now i have another task of reversing the encryption for the decryption code!

Any suggestion how i go ahead with it??
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Encryption code giving different output after the first character

Posted 18 September 2012 - 07:04 AM

Start by doing the opposite of what you've already done. Then finish. We can't help until you try.
Was This Post Helpful? 0
  • +
  • -

#8 billy.peralta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-September 12

Re: Encryption code giving different output after the first character

Posted 20 September 2012 - 01:36 AM

You should run the encryptor every time the user text something inside the textbox
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Encryption code giving different output after the first character

Posted 20 September 2012 - 07:54 AM

The original poster will have a really hard time running the encryptor every time the user enters text into the TextBox considering that he is writing a console application so he can't create a WinForms or WPF TextBox.

Additionally, his input is coming from a file. From the original post:

Quote

You will then encrypt the file, letter by letter.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1