السلام عليكم ورحمة الله
عايز حد يكون عنده خبرة في تشفير النصوص
يقولي بعد اذنه افضل طريقة للتشفير
وتكون تدعم اللغة العربية وجميع اللغات
وتكون قوية الي حد كبير
وجزاكم الله خيرا
Public Partial Class Form1 Inherits Form Public Sub New() InitializeComponent() End Sub
Private Shared Function EncryptStringToBytes(plainText As String, Key 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.Key, rijAlg.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(msDecrypt, decryptor, CryptoStreamMode.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 Object, e As 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(chiperText, myRijndael.Key, myRijndael.IV)
textBox8.Text = orginalText End Using Catch MessageBox.Show("لايمكن فك التشفيرة", "error", MessageBoxButtons.OK, MessageBoxIcon.[Error]) End Try End Sub
الدالة encrypt تستقبل (النص المراد تشفيره + المفتاح، لازم يكون طوله 32 محرف)
وستعود لك بالنص المشفر
الدالة decrypt تستقبل (النص المشفر + المفتاح نفسه المستخدم في التشفير)
وستعود لك بالنص الاصلي
PHP كود :
Public Shared Function Decrypt(toDecrypt As String, key 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(toEncryptArray, 0, toEncryptArray.Length) Return UTF8Encoding.UTF8.GetString(resultArray) End Function
Public Shared Function Encrypt(toEncrypt As String, key 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)
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