尝试使用rsacryptoserviceprovider生成大质数 [英] Trying to use rsacryptoserviceprovider to generate large primes

查看:61
本文介绍了尝试使用rsacryptoserviceprovider生成大质数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下,基本上我创建了一个RSACryptoServiceProvider实例,导出参数和< try>将P和Q参数转换为数字。 P和Q应该是素数,但我必须做错事,因为我得到了很多偶数。我读过的参数是小端格式的Base64字符串。发布的示例需要引用System.Numerics。



 选项 严格 开启 
进口系统.Security.Cryptography
Imports System.Xml
Imports System.IO
Imports System.Numerics
公共 Form1
WithEvents CBKeySizes As ComboBox 使用 {.Location = 点( 10 10 ),. Size = 大小( 120 20 )}
WithEvents BTNGo As 按钮使用 {.Location = 点( 150 10 ),. Size = 大小( 80 20 ),. TExt = 生成}
WithEvents TXTPrimes As TextBox 使用 {.Location = 点(< span class =code-digit> 10
40 ),. Size = 大小( 500 200 ),。Multiline = True ,_
.Anchor = CType (AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Right,AnchorStyles ),_
.ScrollBars = ScrollBars.Vertical}
WithEvents CBreverse As < span class =code-keyword>新 CheckBox 使用 {.Checked = True , .Text = 反向字节?,。Location = 点( 250 10 ),. Size = 大小( 120 20 )}
私有 功能 ShowArray(inputArray() As 字节作为 字符串
Dim RetString As String = < span class =code-string>
对于 每个 by As Byte inputArray
RetString& = by.ToString( X2)&
下一页
返回 RetString
结束 功能
私有 函数 BArray_To_Decimal(InputArray()作为 字节作为 String
Dim WorkArray()作为 字节 = InputArray
如果 CBreverse.Checked 那么 Array.Reverse(WorkArray)
Dim BigInt As BigInteger = BigInteger(Wor kArray)
返回 BigInt.ToString
结束 函数
私有 函数 BArray_To_Hex(InputArray()作为 字节作为 String
Dim WorkArray() As 字节 = InputArray
如果 CBreverse.Checked 那么数组.Reverse(WorkArray)
Dim BigInt As BigInteger = BigInteger(WorkArray)
返回 BigInt.ToString( x
结束 功能
私人 Sub BTNGo_Click(发件人 As System。 Object ,e As System.EventArgs)句柄 BTNGo.Click
TXTPrimes.Clear()
使用 rsa As RSACryptoServiceProvider( CInt (CBKeySizes.Text))
Dim Params As RSAParameters = rsa.ExportParameters( True
TXTPrimes.AppendText( P(Array):& ShowArray(Params.P)& vbNewLine)
TXTPrimes.AppendText( P(Base 64):& Convert。 ToBase64String(Params.P)& vbNewLine)
TXTPrimes.AppendText( P(Prime-Dec): & BArray_To_Decimal(Params.P)& vbNewLine)
TXTPrimes.AppendText( P(Prime-Hex):& BArray_To_Hex(Params.P)& vbNewLine)
TXTPrimes.AppendText(vbNewLine)
TXTPrimes.AppendText( Q(数组):& ShowArray(Params.Q)& vbNewLine)
TXTPrimes.AppendText( Q(Base 64):& Convert.ToBase64String(Params.Q)& vbNewLine)
TXTPrimes.AppendText( Q(Prime-Dec):& BArray_To_Decimal(Params.Q)& vbNewLine)
TXTPrimes.AppendText( Q(Prime-Hex):& BArray_To_Hex (Params.Q)& vbNewLine)
结束 使用
结束 Sub

私人 Sub Form1_Load(sender As System。 Object ,e As System.EventArgs)句柄 MyBase .Load
Me .Size = 大小( 550 300
.Text = Primes v ia RSA Key Generation
Me .Font = 字体( Consolas 8
.Controls.Add(CBKeySizes)
.Controls.Add(BTNGo)
.Controls.Add(TXTPrimes)
.Controls.Add(CBreverse)
使用 RSATest 作为 RSACryptoServiceProvider
Dim legalKeySizes() As KeySizes = RSATest.LegalKeySizes
如果(legalKeySizes.Length> 0 然后
i 作为 整数 = 0 legalKeySizes.Length - 1 步骤 1
对于 K As 整数 = legalKeySizes(i).MinSize legalKeySizes(i).MaxSize Step legalKeySizes(i).SkipSize
CBKeySizes.Items.Add(K)
下一步
下一步
CBKeySizes.SelectedIndex = 0
Else
MessageBox.Show( 致命错误获得法律密钥大小 无法继续,MessageBoxButtons.OK,MessageBoxIcon。停止
结束
结束 如果
结束 使用
结束 Sub
结束 班级





我的尝试:



代码粘贴有问题,希望有人能告诉我发生故障的地方。

解决方案

对不起,缺乏全面的答案。但是我可以建议你能做些什么。



请参阅我对这个问题的评论,所有这些。在我的上一篇评论中,我按照你的想法描述了我的实验,这提供了一个非常有力的证据,证明你观察到的问题与你错误的假设相关,即P和Q参数的数字解释,即字节数组,与用于初始化 System.Numeric.BigInteger 的解释。我认为很好的机会是,如果你反转字节数组,解释将匹配非负数。我没有任何证据(从理论上讲,可以有很多不同的解释),但不太可能使用任何其他选项。



如果你真的想要要深入研究,你需要期望 RSACryptoServiceProvider 的源代码及其相关的依赖关系。不幸的是,Microsoft文档不够详细。 (我可能不需要 BigInteger 源代码。)您可以在此处访问.NET FCL源代码:参考资料来源 [ ^ ]。



另外,我刚刚发现了一篇关于这个主题的文章,还没有读过。请参阅: .NET 4 BigInteger和RSA签名和加密 [ ^ ]。



一般,这个类并不是真正意图提供素数的来源,但我必须承认你的想法非常好,因为这个类提供了不错的性能。您只需要找到正确的数值解释,或者更好的是,直接在字节数组上执行的所有操作的整个算法。



-SA

Code is below, basically I create an instance of the RSACryptoServiceProvider, export the Parameters and <try> to turn the P and Q parameters into numbers. P and Q are supposed to be primes but I must be doing something wrong because I get a lot of even numbers. The parameters, or so I read are Base64 Strings in little endian format. The sample posted requires a reference to System.Numerics.

Option Strict On
Imports System.Security.Cryptography
Imports System.Xml
Imports System.IO
Imports System.Numerics
Public Class Form1
    WithEvents CBKeySizes As New ComboBox With {.Location = New Point(10, 10), .Size = New Size(120, 20)}
    WithEvents BTNGo As New Button With {.Location = New Point(150, 10), .Size = New Size(80, 20), .Text = "Generate"}
    WithEvents TXTPrimes As New TextBox With {.Location = New Point(10, 40), .Size = New Size(500, 200), .Multiline = True, _
                        .Anchor = CType(AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Right, AnchorStyles), _
                        .ScrollBars = ScrollBars.Vertical}
    WithEvents CBreverse As New CheckBox With {.Checked = True, .Text = "Reverse Bytes ?", .Location = New Point(250, 10), .Size = New Size(120, 20)}
    Private Function ShowArray(inputArray() As Byte) As String
        Dim RetString As String = ""
        For Each by As Byte In inputArray
            RetString &= by.ToString("X2") & " "
        Next
        Return RetString
    End Function
    Private Function BArray_To_Decimal(InputArray() As Byte) As String
        Dim WorkArray() As Byte = InputArray
        If CBreverse.Checked Then Array.Reverse(WorkArray)
        Dim BigInt As BigInteger = New BigInteger(WorkArray)
        Return BigInt.ToString
    End Function
    Private Function BArray_To_Hex(InputArray() As Byte) As String
        Dim WorkArray() As Byte = InputArray
        If CBreverse.Checked Then Array.Reverse(WorkArray)
        Dim BigInt As BigInteger = New BigInteger(WorkArray)
        Return BigInt.ToString("x")
    End Function
    Private Sub BTNGo_Click(sender As System.Object, e As System.EventArgs) Handles BTNGo.Click
        TXTPrimes.Clear()
        Using rsa As New RSACryptoServiceProvider(CInt(CBKeySizes.Text))
            Dim Params As RSAParameters = rsa.ExportParameters(True)
            TXTPrimes.AppendText("P (Array  )   : " & ShowArray(Params.P) & vbNewLine)
            TXTPrimes.AppendText("P (Base 64)   : " & Convert.ToBase64String(Params.P) & vbNewLine)
            TXTPrimes.AppendText("P (Prime-Dec) : " & BArray_To_Decimal(Params.P) & vbNewLine)
            TXTPrimes.AppendText("P (Prime-Hex) : " & BArray_To_Hex(Params.P) & vbNewLine)
            TXTPrimes.AppendText(vbNewLine)
            TXTPrimes.AppendText("Q (Array  )   : " & ShowArray(Params.Q) & vbNewLine)
            TXTPrimes.AppendText("Q (Base 64)   : " & Convert.ToBase64String(Params.Q) & vbNewLine)
            TXTPrimes.AppendText("Q (Prime-Dec) : " & BArray_To_Decimal(Params.Q) & vbNewLine)
            TXTPrimes.AppendText("Q (Prime-Hex) : " & BArray_To_Hex(Params.Q) & vbNewLine)
        End Using
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(550, 300)
        Me.Text = "Primes via RSA Key Generation"
        Me.Font = New Font("Consolas", 8)
        Me.Controls.Add(CBKeySizes)
        Me.Controls.Add(BTNGo)
        Me.Controls.Add(TXTPrimes)
        Me.Controls.Add(CBreverse)
        Using RSATest As New RSACryptoServiceProvider
            Dim legalKeySizes() As KeySizes = RSATest.LegalKeySizes
            If (legalKeySizes.Length > 0) Then
                For i As Integer = 0 To legalKeySizes.Length - 1 Step 1
                    For K As Integer = legalKeySizes(i).MinSize To legalKeySizes(i).MaxSize Step legalKeySizes(i).SkipSize
                        CBKeySizes.Items.Add(K)
                    Next
                Next
                CBKeySizes.SelectedIndex = 0
            Else
                MessageBox.Show("Fatal Error getting Legal Key Sizes", "Cannot Continue", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                End
            End If
        End Using
    End Sub
End Class



What I have tried:

Code pasted in question, hoping someone can show me where I am malfunctioning.

解决方案

Sorry for the lack of comprehensive answer. But I can suggest what you can do.

Please see my comments to the question, all of them. In my last comment, I described my experiment following your idea, which provides a pretty strong evidence that the problem you observed is related to your wrong assumption that numeric interpretation of P and Q parameters, which are array of bytes, is the same as the interpretation used to initialize System.Numeric.BigInteger. I think that good chances are, the interpretation will match, for non-negative numbers, if you invert the array of byte. I don't have any proof (theoretically speaking, there can be many different interpretations), but it's unlikely that any other options could be used.

If you really want to dig into it, you need to expect the source code of RSACryptoServiceProvider and relevant dependencies of it. Unfortunately, Microsoft documentation is not detailed enough. (I would not probably need BigInteger source code.) You can access .NET FCL source code here: Reference Source[^].

Also, I just found one article on the topic, did not read it yet. Please see: .NET 4 BigInteger and RSA Signature and Encryption[^].

Generally, this class is not really intended to provide the source of prime number, but I must admit that your idea is very good, as the class provides decent performance. You only need to find out correct numeric interpretation or, better yet, the whole arithmetic of all operations performed directly on arrays of bytes.

—SA


这篇关于尝试使用rsacryptoserviceprovider生成大质数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆