C# 中使用指数、模数和基数作为参数的 RSA 加密

发布于 2024-09-27 18:08:43 字数 1492 浏览 0 评论 0原文

我有一个专有应用程序,它使用扩展来处理加密。为了加密字符串,我将指数、模数、底数和字符串作为参数提供给它。它返回加密的字符串。

我需要能够在与专有应用程序对话的 ac# 应用程序中复制此功能。我不确定从哪里开始——非常感谢您能提供的任何帮助。

这就是我现在所拥有的;

public class Cryptography
{
    public static 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);

    }
} 

当我使用专有应用程序加密数据,然后尝试使用上面的代码时 - 生成的加密数据是不同的。

我不知道如何继续。

编辑;每次运行它都会返回不同的字符串。使用相同的输入字符串。

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 static 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趁微风不噪 2024-10-04 18:08:43

如果我正确理解你的问题,GregS 是正确的。程序可以通过多种方式对密钥和密文进行编码。不幸的是,如果您没有扩展程序所使用的协议或文件格式的规范,则必须对其进行逆向工程。

您评论了程序每次运行时都会返回不同字符串的事实。这是 RSA 的属性,事实上也是任何公钥加密原语的属性。确定性算法(即,在给定相同输入的情况下始终返回相同输出的非随机算法)不能用作公钥加密原语,因为它们容易受到选择明文攻击。

编辑:这意味着您加密明文并将结果与​​模块输出进行比较的方法将不起作用。相反,一旦您相信您已找到扩展模块吐出的 RSA 输出,请尝试解密它。

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.

心房的律动 2024-10-04 18:08:43

你的参数初始化有点奇怪。您确定获得有效的模数结果(低于字节表示)吗?

6E006F006E006F006E006F006D006F006E006F006D006E006F006D0066006F006F006E006F006E006D006F00

和指数:

6200

我认为他们是错误的,因为Unicode编码的特殊性。您可能只是输入错误,并想使用 System.Text.Encoding.UTF8.GetBytes。

Modulus 的结果会好得多:

6E6F6E6F6E6F6D6F6E6F6D6E6F6D666F6F6E6F6E6D6F

但一般来说我建议使用 Base64 进行关键或关键部分传输而不是 UTF-8 字符串。

您也没有指定使用的密钥大小。默认是1024吗?

因此,我建议尝试使用 UTF8 并检查密钥大小。希望它会有所帮助。

也许您也会对这个链接感兴趣 所有关于 RSAParameters< /a>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文