Click here to Skip to main content
14,698,806 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
I'm trying to encrypt a file with a function(modexp) which does modular arithmetic. but I'm nor getting a proper way to read char by char and then do some arithmetic(modexp) on that char, then encrypted chat should be written to a new file.
I have written my code like this.
private void btnSubmit_Click(object sender, EventArgs e)
{

        FileStream fr = new FileStream(textBox6.Text, FileMode.Open, FileAccess.Read);
        try
        {
            MessageBox.Show("Encryption Started");
            FileStream fw = new FileStream(textBox7.Text, FileMode.Create, FileAccess.Write);
            byte[] buffer = new byte[1];
            while (fr.Position != fr.Length)
            {
                int m = fr.Read(buffer, 0, buffer.Length);
               string m1 = modexp(m.ToString().Trim(), textBox4.Text.Trim(), textBox3.Text.Trim());
               fw.Write(buffer, 0, Convert.ToInt32(m1));

            }
            fw.Flush();
            fr.Close();
            fw.Close();
            MessageBox.Show("Encryption Completed");

        }
        catch(Exception ex)
        {
            MessageBox.Show("File creation Error" + ex, "Implementation of RSA");
        }

  }

Please help me.

Thanks in advance.
Ganesh.
Posted
Updated 27-Mar-11 20:55pm
v2
Comments
Prerak Patel 28-Mar-11 1:56am
   
What is the problem?

1 solution

I think the problem is modeexp. You need to show its code. But look, it accepts three strings, first argument is not byte, but strings. Instead of encrypting this one byte (which would make little sense, but let's assume you need it), it tries to encrypt some string, which is just a human-readable representation of this byte. No, it won't encrypt this byte! I can guess you need to read the file in one string and pass it to modexp at once.

However, I don't trust this modexp. It if really was a correct encryption method, it would accepts array of byte as a first parameter, not a string. If the method converts a string parameter into something numeric this is… well, I don't even want to discuss it.

Now, why did you decide that "modular arithmetic" can be used as a real encryption method. Well, this is ridiculous, using no encryption is better. And why on Earth would you call all that RSA?!! RSA is a completely different and serious thing, fully implemented in .NET, see System.Security.Cryptography.RSA, System.Security.Cryptography.RSACryptoServiceProvider

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900