# Encryption code giving different output after the first character

Page 1 of 1

## 8 Replies - 4883 Views - Last Post: 20 September 2012 - 07:54 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=292209&amp;s=4849a8d8820ff72da18db287e16a424a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ashishnehete

• New D.I.C Head

Reputation: 0
• 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);

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

• Saucy!

Reputation: 6246
• Posts: 24,014
• 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.

### #3 ashishnehete

• New D.I.C Head

Reputation: 0
• 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

### #4 ashishnehete

• New D.I.C Head

Reputation: 0
• 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);

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);
}
}

}

}

}
}

```

### #5 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• 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

### #6 ashishnehete

• New D.I.C Head

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

## Re: Encryption code giving different output after the first character

Posted 17 September 2012 - 11:36 PM

Momerath, 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??

### #7 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5103
• Posts: 9,283
• 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.

### #8 billy.peralta

• New D.I.C Head

Reputation: 0
• 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

### #9 Skydiver

• Code herder

Reputation: 6166
• Posts: 21,264
• 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.