开发者

RSA Encryption in c# using Exponent, Modulus and Base as parameters

I have a proprietary application that uses an extension to handle cryptography. To encrypt a string I feed it Exponent, Modulus, Base and string as parameters. It returns the encrypted string.

I need to be able to replicate this functionality in a c# application that talks to the proprietary application. I'm unsure where to begin with this – and would appreciate any help you can give.

This is what I have at the moment;

public class Cryptography
{
    public sta开发者_如何学Pythontic RSACryptoServiceProvider rsa;

    public static void AssignParameter()
    {
        const int PROVIDER_RSA_FULL = 1;
        const string CONTAINER_NAME = "SpiderContainer";
        CspParameters cspParams;
        cspParams = new CspParameters(PROVIDER_RSA_FULL);
        cspParams.KeyContainerName = CONTAINER_NAME;
        //cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
        cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
        rsa = new RSACryptoServiceProvider(cspParams);
    }

    public static string Sencrypt(string input)
    {

        RSAParameters parameters = new RSAParameters();
        parameters.Modulus = System.Text.Encoding.Unicode.GetBytes("nononomonomnomfoononmo");
        parameters.Exponent = System.Text.Encoding.Unicode.GetBytes("b");
        rsa.ImportParameters(parameters);

        byte[] plainbytes = System.Text.Encoding.UTF8.GetBytes(input);
        byte[] cipherbytes = rsa.Encrypt(plainbytes, false);

        System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
        //return enc.GetString(cipherbytes);
        return Convert.ToBase64String(cipherbytes);

    }
} 

When I encrypt data using the proprietary application, and then try using the above code - the resulting encrypted data is different.

I'm at a loss at how to proceed.

edit; It returns a different string everytime it's ran. Using the same input string.


If I understand your question correctly, GregS is correct. There are a lot of ways the program could be encoding keys and cipher texts. Unfortunately, if you do not have a spec of the protocol or file format the extension is using, you will have to reverse engineer it.

You commented on the fact that the program returns a different string every time it is run. That is a property of RSA and, in fact, any public-key encryption primitive. Deterministic algorithms (i.e., non-random algorithms that always return the same output given the same inputs) cannot be used as public-key encryption primitives because they would be vulnerable to chosen-plaintext attacks.

edit: The implication is that your approach of encrypting plaintext and comparing the result to the module's output will not work. Instead, once you believe you have found the RSA output that the extension module is spitting out, try to decrypt it.


Your parameter initialization a bit strange. Are you sure you get valid result as Modulus (below byte representation)?

6E006F006E006F006E006F006D006F006E006F006D006E006F006D0066006F006F006E006F006E006D006F00

and Exponent:

6200

I think they are wrong, because of specificity of Unicode encoding. You may just mistype and want to use System.Text.Encoding.UTF8.GetBytes.

Result for Modulus will be much better:

6E6F6E6F6E6F6D6F6E6F6D6E6F6D666F6F6E6F6E6D6F

but in general I suggest to use Base64 for key or key parts transfer instead of UTF-8 strings.

Also you don't specify used key size. Is it 1024 as by default?

So, I suggest to try use UTF8 and check key size. Hope it will be helpful.

Maybe this link will be also interesting to you All About RSAParameters

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜