1

I'm trying to make my own RSA encryption. I know there are build in methods in C# but I wanted to make my own program because I want to understand how it's done. I think I'm messing up when converting from and to the byte array. If someone could push me in the right direction, that would be great :).

private void btnEncrypt_Click(object sender, EventArgs e)
{
    EncryptieModulo = 55;
    PublicKey = 27;
    var PlainText = Encoding.UTF8.GetBytes(txtPlaintext.Text);
    for (int i = 0; i < PlainText.Length; i++)
    {
        PlainText[i] = (byte)(BigInteger.Pow(PlainText[i], PublicKey) % EncryptieModulo);
    }

    textBox1.Text = Convert.ToBase64String(PlainText);
}

private void btnDecrypt_Click(object sender, EventArgs e)
{
    EncryptieModulo = 55;
    PrivateKey = 3;
    var CrypText = Convert.FromBase64String(txtCCryptedText.Text);
    for (int i = 0; i < CrypText.Length; i++)
    {
        CrypText[i] = (byte)(BigInteger.Pow(CrypText[i], PrivateKey) % EncryptieModulo);
    }

    textBox1.Text = Encoding.UTF8.GetString(CrypText);
}
6
  • Please explain with example input and output what this code does, and explain what it is supposed to do and what you've tried to investigate those differences. Commented Dec 22, 2013 at 23:22
  • When I encrypt the string "Pablo" it returns "FDAgJQE=". Now when I try to decrypt it, it returns the string "*+5". Commented Dec 22, 2013 at 23:25
  • @CodeCaster: as I can see from the code he is trying to implement the very basic RSA to understand how it works. More or less what is described here en.wikipedia.org/wiki/RSA_%28cryptosystem%29#A_working_example The full implementation base on this emc.com/emc-plus/rsa-labs/pkcs/files/… would not be as simple, but he is just trying to work out the inner workings of RSA in it's most basic form. Commented Dec 22, 2013 at 23:28
  • Yes thank you zespri that is what I'm trying to do. Commented Dec 22, 2013 at 23:30
  • 1
    Just stick to byte arrays for encrypt/decrypt methods and get that part working. Than figure out if/why your string->byte->string sequence is broken. Commented Dec 23, 2013 at 0:06

1 Answer 1

1

Values that you encode have to be less than Modulo, which is in your case 55. In order for your example to work, you need to substitute Encoding.UTF8.GetBytes/Encoding.UTF8.GetString with your custom functions that translate latin alphabet characters to bytes between 0-54 and back.

For example you can do it like this:

private static byte[] GetBytes(string s)
{
    byte[] result = new byte[s.Length];
    for (int i = 0; i < s.Length; i++)
    {
        if (s[i] >= 'a' && s[i] <= 'z')
        {
            result[i] = (byte)(s[i] - 'a');
            continue;
        }
        if (s[i] >= 'A' && s[i] <= 'Z')
        {
            result[i] = (byte)(s[i] - 'A' + 26);
            continue;
        }
        throw new ArgumentOutOfRangeException();
    }
    return result;
}

private static string GetString(byte[] b)
{
    StringBuilder sb = new StringBuilder(b.Length);
    for (int i = 0; i < b.Length; i++)
    {
        if (b[i] >= 0 && b[i] < 26)
        {
            sb.Append((char)('a' + b[i]));
            continue;
        }
        if (b[i] >= 26 && b[i] < 52)
        {
            sb.Append((char)('A' + b[i] - 26));
            continue;
        }
        throw new ArgumentOutOfRangeException();
    }
    return sb.ToString();
}

Now substitute your Encoding.UTF8.GetBytes/Encoding.UTF8.GetString with our new ones and you should be good to go.

Note though, that what you just did is NOT really RSA encryption. You just have encrypted each byte individually using very short key. Security of this implementation is zilch. Please refer to the spec for all the intricacies of "real" implementation. (Padding for one comes to mind). In addition I advise you looking up a number of open source implementations of the real thing, if you want to understand the algorithm better.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that was really helpful. Apart from the very short key, are there any other reasons this is insecure?
Yes. You better ask this on crypto.stackexchange.com but generally, if you are encrypting byte by byte with any given key you will get only 256 different outputs for each byte and there will be one to one mapping between the output and the byte. That is every 0x01 will always encrypt to, say, 0x5fa3dd. I hope you can see how bad this is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.