تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[سؤال] طلب دالة تشفي النصوص
#1
السلام عليكم ورحمة الله
عايز حد يكون عنده خبرة في تشفير النصوص
يقولي بعد اذنه افضل طريقة للتشفير
وتكون تدعم اللغة العربية وجميع اللغات
وتكون قوية الي حد كبير
وجزاكم الله خيرا
الرد }}}
تم الشكر بواسطة:
#2
تشفيرة AES هي تشفيرة قوية جدا وحسب معلوماتي لم يتم كسرها حتى الان

تاخذ مفتاحين . الاول Key , والاخر IV

حيث يتم انتاج المفتاحين عند عملية التشفير

وعند عملية فك التشفير تقوم باعطاء الدالة المفتاحين الذان تم انتاجهما

فالبيانات يتم توليدها حسب صحة المفتاحين .. فلو ادخلت حرف واحد خطا من المفتاح ، سيظهر لك نص اخر

المثال مرفق بالسي شارب





كود المثال محول لVB


PHP كود :
Imports System.Collections.Generic
Imports System
.ComponentModel
Imports System
.Data
Imports System
.Drawing
Imports System
.IO
Imports System
.Linq
Imports System
.Security.Cryptography
Imports System
.Text
Imports System
.Threading.Tasks
Imports System
.Windows.Forms

    
Public Partial Class Form1
        Inherits Form
        
Public Sub New()
            
InitializeComponent()
        
End Sub

        
Private Shared Function EncryptStringToBytes(plainText As StringKey As Byte(), IV As Byte()) As Byte()
            
' Check arguments. 
            If plainText Is Nothing OrElse plainText.Length <= 0 Then
                Throw New ArgumentNullException("plainText")
            End If
            If Key Is Nothing OrElse Key.Length <= 0 Then
                Throw New ArgumentNullException("Key")
            End If
            If IV Is Nothing OrElse IV.Length <= 0 Then
                Throw New ArgumentNullException("Key")
            End If
            Dim encrypted As Byte()
            ' 
Create an RijndaelManaged object 
            
' with the specified key and IV. 
            Using rijAlg As New RijndaelManaged()
                rijAlg.Key = Key
                rijAlg.IV = IV

                ' 
Create a decrytor to perform the stream transform.
                
Dim encryptor As ICryptoTransform rijAlg.CreateEncryptor(rijAlg.KeyrijAlg.IV)

                
' Create the streams used for encryption. 
                Using msEncrypt As New MemoryStream()
                    Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                        Using swEncrypt As New StreamWriter(csEncrypt)

                            '
Write all data to the stream.
                            
swEncrypt.Write(plainText)
                        
End Using
                        encrypted 
msEncrypt.ToArray()
                    
End Using
                End Using
            End Using


            
' Return the encrypted bytes from the memory stream. 
            Return encrypted

        End Function

        Private Shared Function DecryptStringFromBytes(cipherText As Byte(), Key As Byte(), IV As Byte()) As String
            ' 
Check arguments
            If 
cipherText Is Nothing OrElse cipherText.Length <= 0 Then
                
Throw New ArgumentNullException("cipherText")
            
End If
            If 
Key Is Nothing OrElse Key.Length <= 0 Then
                
Throw New ArgumentNullException("Key")
            
End If
            If 
IV Is Nothing OrElse IV.Length <= 0 Then
                
Throw New ArgumentNullException("Key")
            
End If

            
' Declare the string used to hold 
            ' 
the decrypted text
            
Dim plaintext As String Nothing

            
' Create an RijndaelManaged object 
            ' 
with the specified key and IV
            
Using rijAlg As New RijndaelManaged()
                
rijAlg.Key Key
                rijAlg
.IV IV

                
' Create a decrytor to perform the stream transform.
                Dim decryptor As ICryptoTransform = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV)

                ' 
Create the streams used for decryption
                
Using msDecrypt As New MemoryStream(cipherText)
                    
Using csDecrypt As New CryptoStream(msDecryptdecryptorCryptoStreamMode.Read)
                        
Using srDecrypt As New StreamReader(csDecrypt)

                            
' Read the decrypted bytes from the decrypting stream 
                            ' 
and place them in a string.
                            
plaintext srDecrypt.ReadToEnd()
                        
End Using
                    End Using

                End Using
            End Using

            
Return plaintext
        End 
Function

        Private 
Sub button1_Click(sender As ObjectAs EventArgs)
            
Using myRijndael As New RijndaelManaged()

                
myRijndael.GenerateKey()
                
myRijndael.GenerateIV()

                
' Encrypt the string to an array of bytes. 
                textBox2.Text = Convert.ToBase64String(EncryptStringToBytes(textBox1.Text, myRijndael.Key, myRijndael.IV))


                textBox3.Text = Convert.ToBase64String(myRijndael.Key).ToString()
                textBox4.Text = Convert.ToBase64String(myRijndael.IV).ToString()
            End Using
        End Sub

        Private Sub button2_Click(sender As Object, e As EventArgs)
            Try
                Using myRijndael As New RijndaelManaged()
                    Dim chiperText As Byte() = Convert.FromBase64String(textBox5.Text.Trim())
                    myRijndael.Key = Convert.FromBase64String(textBox6.Text.Trim())
                    myRijndael.IV = Convert.FromBase64String(textBox7.Text.Trim())
                    ' 
Decrypt the bytes to a string
                    
Dim orginalText As String DecryptStringFromBytes(chiperTextmyRijndael.KeymyRijndael.IV)

                    
textBox8.Text orginalText
                End Using
            
Catch
                
MessageBox.Show("لايمكن فك التشفيرة""error"MessageBoxButtons.OKMessageBoxIcon.[Error])
            
End Try
        
End Sub

    End 
Class 


الملفات المرفقة
.rar   EncryptDecrypt using AES.rar (الحجم : 55.8 ك ب / التحميلات : 147)
الرد }}}
تم الشكر بواسطة: abulayth , sooriaty03 , hoob computer , mamas1
#3
السلام عليكم ورحمة الله

جزاك الله خيرا اخي الكريم

طيب بالنسبة لو انا عايز احدد مفتاح ثابت للتشفير وفك التشفير
الرد }}}
تم الشكر بواسطة:
#4
السلام عليكم
الرفع للاهمية
الرد }}}
تم الشكر بواسطة:
#5
شكرا لك أخي الشاكي لله
منقطع .. للدراسة Confused
الرد }}}
تم الشكر بواسطة:
#6
اعذرني على الرد المتأخر

انسى كل الي قلته سابقا

الدالة encrypt تستقبل (النص المراد تشفيره + المفتاح، لازم يكون طوله 32 محرف)
وستعود لك بالنص المشفر

الدالة decrypt تستقبل (النص المشفر + المفتاح نفسه المستخدم في التشفير)
وستعود لك بالنص الاصلي


PHP كود :
Public Shared Function Decrypt(toDecrypt As Stringkey As String) As String
    Dim keyArray 
As Byte() = UTF8Encoding.UTF8.GetBytes(key)
    
' AES-256 key
    Dim toEncryptArray As Byte() = Convert.FromBase64String(toDecrypt)
    Dim rDel As New RijndaelManaged()
    rDel.Key = keyArray
    rDel.Mode = CipherMode.ECB
    ' 
http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
    
Dim cTransform As ICryptoTransform rDel.CreateDecryptor()
    
Dim resultArray As Byte() = cTransform.TransformFinalBlock(toEncryptArray0toEncryptArray.Length)
    Return 
UTF8Encoding.UTF8.GetString(resultArray)
End Function

Public 
Shared Function Encrypt(toEncrypt As Stringkey As String) As String
    Dim keyArray 
As Byte() = UTF8Encoding.UTF8.GetBytes(key)
    
' 256-AES key
    Dim toEncryptArray As Byte() = UTF8Encoding.UTF8.GetBytes(toEncrypt)
    Dim rDel As New RijndaelManaged()
    rDel.Key = keyArray
    rDel.Mode = CipherMode.ECB
    ' 
http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
    
rDel.Padding PaddingMode.PKCS7
    
' better lang support
    Dim cTransform As ICryptoTransform = rDel.CreateEncryptor()
    Dim resultArray As Byte() = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)
    Return Convert.ToBase64String(resultArray, 0, resultArray.Length)
End Function

Private Sub button1_Click(sender As Object, e As EventArgs)

    textBox2.Text = Encrypt(textBox1.Text, "12345678901234567890123456789012")

End Sub

Private Sub button2_Click(sender As Object, e As EventArgs)
    Try
        textBox3.Text = Decrypt(textBox2.Text, "12345678901234567890123456789012")
    Catch
        MessageBox.Show("لايمكن فك التشفيرة", "error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
    End Try
End Sub 
الرد }}}
تم الشكر بواسطة: Sajad , mamas1 , tariq2812
#7
السلام عليكم ورحمة الله
اسف جدا علي التأخير في الرد كنت مشغول جدا
جزاك الله خيرا اخي محمد وجعله في موازين حسناتك
الرد }}}
تم الشكر بواسطة:



التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم