الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

سؤال: تطبيق RSA مع SHA1 ب N و E فقط

مغلق
بدأه Xacker في 16 أبريل 2006 · 9 رد · 872 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

احاول ان اقوم بتطبيق مثال RSA عبر .NET لكن تواجهني بعض المشاكل,

طول N يجب ان يكون 400h يعني 1024

E هي "AQAB"

N هي كما "rDgW9QtWU6FySpOM+PnvX05IryvmNHXtxdcen9wFnZNbKo8rZxdG3rZ4iP/ozYTflLs="

P,Q غير ضرورية, بالاصل غير معروفة

D ايضا غير معروفة لكنها غير ضرورية لما اسعى اليه

كما يظهر المتغيرات قاعدتها base64

احاول ان استخدم SHA1 كمزود للـ hash

طول الـ signature = 68

من المفترض ان يعود الناتج True (تاكيد) على صحة البيانات وتطابق الـ key مع الـ sign.. لم اقم بهذا من قبل على .NET لذا لا ادري ما المشكلة. اعتقد انه في تقطيع البيانات لذا هذا ما يستخدم في البرنامج الذي اعمل عليه :rolleyes:

p.s: ليس هناك اي مخالفة فهذا سؤال برمجي بحت :D

    'Public Exponent() As Byte = System.Text.Encoding.ASCII.GetBytes("AQAB")
    'Public Modulus() As Byte = System.Text.Encoding.ASCII.GetBytes("rDgW9QtWU6FySpOM+PnvX05IryvmNHXtxdcen9wFnZNbKo8rZxdG3rZ4iP/ozYTflLs=")
    Public Function GetStringFromByteArray(ByVal ByteArray() As Byte) As String
        Dim str As String = ""
        For Each bytechar As Byte In ByteArray
            str &= bytechar.ToString
        Next
        Return str
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim signProvider = New RSACryptoServiceProvider(400)
        'Dim rsa As RSACryptoServiceProvider = New RSACryptoServiceProvider(400)
        Dim RSA As New RSACryptoServiceProvider(400)
        Dim data As Byte() = ASCII.GetBytes(TextBox1.Text)
        Dim sign As Byte() = RSA.SignData(data, "SHA1")
        Dim key As String = GetStringFromByteArray(sign)
        Dim data_part As String = key.Substring(68)
        Dim signature_part As String = key.Substring(0, 68)
        TextBox1.Text = key
        TextBox2.Text = data_part
        TextBox3.Text = signature_part
        Dim buffer1 As Byte() = ASCII.GetBytes(data_part)
        Dim buffer2 As Byte() = ASCII.GetBytes(signature_part)
        Dim a As Boolean = RSA.VerifyData(buffer1, "SHA1", buffer2)
        Label1.Text = a
    End Sub

قرأت مقالات الاخ SOLO.NET جزاه الله الخير لكن لم استطع ايجاد رابط بين ما ذكر ومشكلتي لعله يكون الحل. ايضا SHA1 لم يتم التكلم عنها.

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#2

انظر هذا الرابط فقد يفيدك Walkthrough: Encrypting and Decrypting Strings in Visual Basic

عن عائشة رضي الله عنها أن النبي صلى الله عليه وسلم قال: إن الله يحب إذا عمل أحدكم عملا أن يتقنه

نعيب زماننا والعيب فينا ... وما لزماننا عيب سوانا

ونهجو ذا الزمان بغير ذنب ... ولو نطق الزمان لنا هجانا

محمد سامر أبو سلو

#3

شكرا جزيلا اخي على الرابط, لكن للاسف لم يستطع ان يخدمني في مشكلتي.. ايضا عذرا على الكود السخيف في الاعلى :P يبدو اني ما زلت تحت تاثير المرض وحبوب الالتهاب :)

الكود الذي قمت بتعديله هو التالي:

    Private Sub hash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hash.Click
        Dim RSA As New RSACryptoServiceProvider(400)   'declares the CSP to be used with the length of 1024bits
        Dim plaintext As Byte() = Encoding.Unicode.GetBytes(txtM2C.Text) 'sets the unicode representation of the plain text
        Dim hash As Byte() = RSA.SignData(plaintext, "SHA1")  'sign the plain text data with SHA1 algorithm
        txtC.Text = Convert.ToBase64String(hash)   'and convert the signed hash into base64 string
        bits.Text = Len(txtC.Text)
    End Sub
    Private Sub verify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles verify.Click
        Dim RSA As New RSACryptoServiceProvider(400)
        txtDP.Text = txtC.Text.Substring(68)
        txtSP.Text = txtC.Text.Substring(0, 68)
        Dim buffer1 As Byte() = Encoding.Unicode.GetBytes(txtDP.Text)
        Dim buffer2 As Byte() = Encoding.Unicode.GetBytes(txtSP.Text)
        Dim res As Boolean = RSA.VerifyData(buffer1, "SHA1", buffer2)
        result.Text = res
    End Sub

كما اعتقد لا غبار عليه, لكن النتيجة ايضا FALSE, حتى بدا الشك يساورني بخصوص القيمة FLASE لعلها -1 :D

المشروع مرفق, السؤال هو: لماذا لا يتم تدقيق البيانات واعطاء النتيجة الايجابية؟ على الرغم من اني حاولت بدون ادخال نص ومع ادخال نص واضافته الى txtC.text في نهايته كي يتم استرجاعه عند التدقيق لكن لا فائدة.

وشكرا

SHA1.exm.rar

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#4

تفضل هذا الكود بعد التعديل وهو يعمل جيدا

    Private Key As RSAParameters

    Private Sub hash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hash.Click
        Dim RSA As New RSACryptoServiceProvider(400)   'declares the CSP to be used with the length of 1024bits
        Dim plaintext As Byte() = Encoding.Unicode.GetBytes(txtM2C.Text) 'sets the unicode representation of the plain text
        Key = RSA.ExportParameters(True)
        Dim hash As Byte() = RSA.SignData(plaintext, New SHA1CryptoServiceProvider)  'sign the plain text data with SHA1 algorithm
        txtC.Text = Convert.ToBase64String(hash)   'and convert the signed hash into base64 string
        bits.Text = Len(txtC.Text)

    End Sub

    Private Sub verify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles verify.Click
        Dim RSA As New RSACryptoServiceProvider(400)
        RSA.ImportParameters(Key)
        Dim buffer1 As Byte() = Encoding.Unicode.GetBytes(txtM2C.Text)
        Dim buffer2 As Byte() = Convert.FromBase64String(txtC.Text)
        Dim res As Boolean = RSA.VerifyData(buffer1, New SHA1CryptoServiceProvider, buffer2)
        result.Text = res.ToString
    End Sub

كما يمكنك الاستغناء عن Key في الكود السابق فيصبح الكود كالتالي

    Private Sub hash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hash.Click
        Dim RSA As New RSACryptoServiceProvider(400)   'declares the CSP to be used with the length of 1024bits
        Dim plaintext As Byte() = Encoding.Unicode.GetBytes(txtM2C.Text) 'sets the unicode representation of the plain text
        Dim hash As Byte() = RSA.SignData(plaintext, New SHA1CryptoServiceProvider)  'sign the plain text data with SHA1 algorithm
        txtC.Text = Convert.ToBase64String(hash)   'and convert the signed hash into base64 string
        bits.Text = Len(txtC.Text)
    End Sub

    Private Sub verify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles verify.Click
        Dim RSA As New RSACryptoServiceProvider(400)
        Dim buffer1 As Byte() = Encoding.Unicode.GetBytes(txtM2C.Text)
        Dim buffer2 As Byte() = Convert.FromBase64String(txtC.Text)
        Dim res As Boolean = RSA.VerifyData(buffer1, New SHA1CryptoServiceProvider, buffer2)
        result.Text = res.ToString
    End Sub

مرفق البرنامج مع الكود المعدل ولكن تم تحويله إلى Vb2005 بسبب أنه لايوجد Vb2003 عى جهازي حاليا

SHA1.exm.zip

تم تعديل هذه المشاركة بواسطة samerselo في 16 أبريل 2006 في 23:09

عن عائشة رضي الله عنها أن النبي صلى الله عليه وسلم قال: إن الله يحب إذا عمل أحدكم عملا أن يتقنه

نعيب زماننا والعيب فينا ... وما لزماننا عيب سوانا

ونهجو ذا الزمان بغير ذنب ... ولو نطق الزمان لنا هجانا

محمد سامر أبو سلو

#5

وهذا نص برنامج كامل كمثال بطريقة مشابهة لطريقتك وهو من نوع Console Application

Imports System.Security.Cryptography
Imports System.Text

Module RSACSPExample

    Sub Main()
        Try
            ' Create a UnicodeEncoder to convert between byte array and string.
            Dim ByteConverter As New ASCIIEncoding

            Dim dataString As String = "Data to Sign"

            ' Create byte arrays to hold original, encrypted, and decrypted data.
            Dim originalData As Byte() = ByteConverter.GetBytes(dataString)
            Dim signedData() As Byte

            ' Create a new instance of the RSACryptoServiceProvider class 
            ' and automatically create a new key-pair.
            Dim RSAalg As New RSACryptoServiceProvider

            ' Export the key information to an RSAParameters object.
            ' You must pass true to export the private key for signing.
            ' However, you do not need to export the private key
            ' for verification.
            Dim Key As RSAParameters = RSAalg.ExportParameters(True)

            ' Hash and sign the data.
            signedData = HashAndSignBytes(originalData, Key)

            ' Verify the data and display the result to the 
            ' console.
            If VerifySignedHash(originalData, signedData, Key) Then
                Console.WriteLine("The data was verified.")
            Else
                Console.WriteLine("The data does not match the signature.")
            End If

        Catch e As ArgumentNullException
            Console.WriteLine("The data was not signed or verified.")
        End Try
    End Sub

    Function HashAndSignBytes(ByVal DataToSign() As Byte, ByVal Key As RSAParameters) As Byte()
        Try
            ' Create a new instance of RSACryptoServiceProvider using the 
            ' key from RSAParameters.  
            Dim RSAalg As New RSACryptoServiceProvider

            RSAalg.ImportParameters(Key)

            ' Hash and sign the data. Pass a new instance of SHA1CryptoServiceProvider
            ' to specify the use of SHA1 for hashing.
            Return RSAalg.SignData(DataToSign, New SHA1CryptoServiceProvider)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return Nothing
        End Try
    End Function


    Function VerifySignedHash(ByVal DataToVerify() As Byte, ByVal SignedData() As Byte, ByVal Key As RSAParameters) As Boolean
        Try
            ' Create a new instance of RSACryptoServiceProvider using the 
            ' key from RSAParameters.
            Dim RSAalg As New RSACryptoServiceProvider

            RSAalg.ImportParameters(Key)

            ' Verify the data using the signature.  Pass a new instance of SHA1CryptoServiceProvider
            ' to specify the use of SHA1 for hashing.
            Return RSAalg.VerifyData(DataToVerify, New SHA1CryptoServiceProvider, SignedData)

        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return False
        End Try
    End Function
End Module

عن عائشة رضي الله عنها أن النبي صلى الله عليه وسلم قال: إن الله يحب إذا عمل أحدكم عملا أن يتقنه

نعيب زماننا والعيب فينا ... وما لزماننا عيب سوانا

ونهجو ذا الزمان بغير ذنب ... ولو نطق الزمان لنا هجانا

محمد سامر أبو سلو

#6

اخي جزاك الله الخير على كل حرف خطته يداك. يعمل بكل كفاءة والحمد لله. هناك سؤال اخر لو سمحت

حاولت ان اقوم باستخدام متغيرات ثابتة لـ N (Modulus) و E (Exponent) لكن يتم اعلامي بضرورة تزويد الخوارزمية بقيمة الـ D, كما تعلم الـ N و الـ E يتم استخدامهم للتشفير لذا انا هنا لست بحاجة للـ D فما السبيل الى تجاوز هذه العقبة, الكود كما وضعته انت وما حاولت الاستخدام فيه:

    Dim RSA As New RSACryptoServiceProvider(400)
    Dim RSAInfo As New Params
    Public Class Params
        Public puk As String = "<RSAKeyValue><Modulus>rDgW9QtWU6FySpOM+PnvX05IryvmNHXtxdcen9wFnZNbKo8rZxd
G3rZ4iP/ozYTflLs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
    End Class
    Private Sub hash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hash.Click
        Dim plaintext As Byte() = Encoding.Unicode.GetBytes(txtM2C.Text)
        RSA.FromXmlString(RSAInfo.puk)
        Dim hash As Byte() = RSA.SignData(plaintext, New SHA1CryptoServiceProvider)
        txtC.Text = Convert.ToBase64String(hash)
        bits.Text = Len(txtC.Text)
    End Sub
    Private Sub verify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles verify.Click
        Dim buffer1 As Byte() = Encoding.Unicode.GetBytes(Mid(txtC.Text, 69, (Len(txtC.Text) - 68)))
        Dim buffer2 As Byte() = Convert.FromBase64String(Mid(txtC.Text, 1, 68))
        Dim res As Boolean = RSA.VerifyData(buffer1, New SHA1CryptoServiceProvider, buffer2)
        result.Text = res.ToString
    End Sub

شكرا مرة اخرى

تم تعديل هذه المشاركة بواسطة Xacker في 17 أبريل 2006 في 00:06

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#7

لا شكر على واجب

بالنسبة لسؤالك الأخير لا أدري

عن عائشة رضي الله عنها أن النبي صلى الله عليه وسلم قال: إن الله يحب إذا عمل أحدكم عملا أن يتقنه

نعيب زماننا والعيب فينا ... وما لزماننا عيب سوانا

ونهجو ذا الزمان بغير ذنب ... ولو نطق الزمان لنا هجانا

محمد سامر أبو سلو

#8

كنت ادقق منذ قليل بالفرق بين SignData و Encrypt, في الاولى يجب ان نقوم بتزويد الخوارزمية بجميع المفاتيح, في الثانية نحتاج فقط الى المفتاح العام.

لكن الفكرة ان الثانية تقوم بالتشفير فقط. ما اريد تطبيقه هو الحصول على التوقيع الرقمي Signed Hash الخاص بالبيانات المدخلة, كما نعلم التوقيع لا يستخدم في فك التشفير وانما في المصادقة.. بالتالي, لو كنت مضطرا الى استعمال SignData ولكني اريد ان استخدم قيم N و E محددة سلفا. فهل استطيع التلاعب بهذه القيم؟ وهل الناتج سيتعلق بها فقط ام بكامل بارامترات الخوارزمية؟

    Dim RSA As New RSACryptoServiceProvider(400)
    Public PublicParams As RSAParameters
    Public PrivateParams As RSAParameters
Private Sub hash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hash.Click
        PublicParams = RSA.ExportParameters(False)
        PrivateParams = RSA.ExportParameters(True)
        PublicParams.Modulus = Encoding.Unicode.GetBytes("rDgW9QtWU6FySpOM+PnvX05IryvmNHXtxdcen9wFnZNbKo8rZxdG3rZ4iP/ozYTflLs=")
        PublicParams.Exponent = Encoding.Unicode.GetBytes("AQAB")
        PrivateParams.Modulus = Encoding.Unicode.GetBytes("rDgW9QtWU6FySpOM+PnvX05IryvmNHXtxdcen9wFnZNbKo8rZxdG3rZ4iP/ozYTflLs=")
        PrivateParams.Exponent = Encoding.Unicode.GetBytes("AQAB")
    Dim plaintext As Byte() = Encoding.Unicode.GetBytes(txtM2C.Text)
    Dim hash As Byte() = RSA.SignData(plaintext, New SHA1CryptoServiceProvider)
    txtC.Text = Convert.ToBase64String(hash)
    bits.Text = Len(txtC.Text)
End Sub
Private Sub verify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles verify.Click
    Dim buffer1 As Byte() = Encoding.Unicode.GetBytes(Mid(txtC.Text, 69, (Len(txtC.Text) - 68)))
    Dim buffer2 As Byte() = Convert.FromBase64String(Mid(txtC.Text, 1, 68))
    Dim res As Boolean = RSA.VerifyData(buffer1, New SHA1CryptoServiceProvider, buffer2)
    result.Text = res.ToString
End Sub

لكان الامر سهلا لو ان القيم الناتجة من RSA هي عبارة عن ناتج لا يتغير لكن الفكرة انها تتغير, على الرغم من ان قيم P و Q تم اعدادها مرة واحدة في المشروع, لذا هل من الممكن الموافقة على الكود اعلاه بكونه يستخدم N و E فقط في توليد التوقيع والتصديق عليه وذلك بايهامه بوجود المفتاح الخاص؟

اتمنى ان اكون قد اوضحت وجهة نظري. وشكرا

تم تعديل هذه المشاركة بواسطة Xacker في 17 أبريل 2006 في 13:53

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#9

كيف لم انتبه الى هذا من قبل! وظيفة Encrypt ستقوم بتوليد قيم عشوائية في كل مرة, بينما الـ SignData القيمة الناتجة منها وحيدة لا تتغير الا بتغيير النص المراد الحصول على التوقيع الرقمي له.. اعتقد ان ما قمت به صحيح لكن.. ما زال لدي شك بخصوص امر اخر

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#10

والله اني لاضحك على نفسي :lol: بعد كل هذه السنوات آتي لأخطئ في هذه.. كل ابن آدم خطاء وخير الخطائين التوابون.. التوبة :D

فهمي لـ SignData كان خاطئا, بالفعل هي تحتاج الى PrivateKey ولا يمكن تطبيق ما اريد فعله بهذه الطريقة لذا ساحاول باسلوب آخر. إلى هنا ينتهي هذا الموضوع.

المعلومات التي حصلت عليها من هذه التجربة لا تضر باي حال :)

شكرا للجميع

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

هذا الموضوع مغلق.

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…