PDA

View Full Version : سوال: مشکل در دریافت مقدار Decrypt شده از سرور ( WebReqest - encrypt - decrypt )



siavashsay
شنبه 07 اردیبهشت 1392, 18:37 عصر
دوستان من یک تابع رو از سایت
http://www.suhailkaleem.com/2010/02/13/encrypt-in-vb-net-and-decrypt-in-php-and-vice-versa/
گرفتم که ورودی رو encrypt میکنه و اون رو decrypt میکنه !
حالا اومدم از طریق WebRequest مقدار encrypt شده رو به یک صفحه PHP فرستادم و توسط اون مقدار decrpyt شده رو دارم دریافت میکنم !
اما مشکلی که هست یک سری از کاراکتر ها رو اشتباه ( بد ) بر میگردونه !
مثلا وقتی کلمه siavash رو وارد میکنم هم توی خود برنامه و هم از طریق webrequest درست برمیگردونه اما وقتی کلمه ای مثل as رو وارد میکنم فقط از سریق webrequest اشتباه ( بد ) بر میگردونه !
جالب اینجاس که خود اون صفحه PHP اون کلمه as رو درست برمیگردونه !
یعنی فقط مشکل دریافت بعضی کلمات رو از طریق webrequest دارم !
بنده هم فایل PHP هم برنامه رو ضمیمه کردم و کدهای مربوط رو هم گذاشتم !
ممنون میشم پیگیری کنید !
فایل module برنامه :


Imports System
Imports System.Text
Imports System.Security.Cryptography
Imports System.IO
Module crtypfunc
Sub Main()

'Shared 256 bit Key and IV here
Dim sKy As String = "lkirwf897+22#bbtrm8814z5qq=498j5" '32 chr shared ascii string (32 * 8 = 256 bit)
Dim sIV As String = "741952hheeyy66#cs!9hjv887mxx7@8y" '32 chr shared ascii string (32 * 8 = 256 bit)

Dim sTextVal As String = "Here is my data to encrypt!!!"

Dim eText As String
Dim dText As String

eText = EncryptRJ256(sKy, sIV, sTextVal)
dText = DecryptRJ256(sKy, sIV, eText)

End Sub

Public Function DecryptRJ256(ByVal prm_key As String, ByVal prm_iv As String, ByVal prm_text_to_decrypt As String)

Dim sEncryptedString As String = prm_text_to_decrypt

Dim myRijndael As New RijndaelManaged
myRijndael.Padding = PaddingMode.Zeros
myRijndael.Mode = CipherMode.CBC
myRijndael.KeySize = 256
myRijndael.BlockSize = 256

Dim key() As Byte
Dim IV() As Byte

key = System.Text.Encoding.ASCII.GetBytes(prm_key)
IV = System.Text.Encoding.ASCII.GetBytes(prm_iv)

Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV)

Dim sEncrypted As Byte() = Convert.FromBase64String(sEncryptedString)

Dim fromEncrypt() As Byte = New Byte(sEncrypted.Length) {}

Dim msDecrypt As New MemoryStream(sEncrypted)
Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

Return (System.Text.Encoding.ASCII.GetString(fromEncrypt) )

End Function

Public Function EncryptRJ256(ByVal prm_key As String, ByVal prm_iv As String, ByVal prm_text_to_encrypt As String)

Dim sToEncrypt As String = prm_text_to_encrypt

Dim myRijndael As New RijndaelManaged
myRijndael.Padding = PaddingMode.Zeros
myRijndael.Mode = CipherMode.CBC
myRijndael.KeySize = 256
myRijndael.BlockSize = 256

Dim encrypted() As Byte
Dim toEncrypt() As Byte
Dim key() As Byte
Dim IV() As Byte

key = System.Text.Encoding.ASCII.GetBytes(prm_key)
IV = System.Text.Encoding.ASCII.GetBytes(prm_iv)

Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key, IV)

Dim msEncrypt As New MemoryStream()
Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)

toEncrypt = System.Text.Encoding.ASCII.GetBytes(sToEncrypt)

csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
csEncrypt.FlushFinalBlock()

encrypted = msEncrypt.ToArray()

Return (Convert.ToBase64String(encrypted))

End Function
End Module


فایل form.vb

Imports System
Imports System.Text
Imports System.Security.Cryptography
Imports System.IO
Imports System.Net
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim fkey As String = "lkirwf897+22#bbtrm8814z5qq=498j5" '32 chr shared ascii string (32 * 8 = 256 bit)
Dim skey As String = "741952hheeyy66#cs!9hjv887mxx7@8y"
Dim username As String = TextBox1.Text
Dim eData As String
eData = EncryptRJ256(fkey, skey, username)
' Dim dData As String = DecryptRJ256(fkey, skey, username)

' Create a request using a URL that can receive a post.
Dim request As WebRequest = WebRequest.Create("http://localhost/vc/hashtest.php")
' Set the Method property of the request to POST.
request.Method = "POST"
' Create POST data and convert it to a byte array.
Dim postData As String = "Player=" + eData
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
' Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded"
' Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length
' Get the request stream.
Dim dataStream As Stream = request.GetRequestStream()
' Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
Dim response As WebResponse = request.GetResponse()
' Display the status.
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
' Console.WriteLine(responseFromServer)
'MsgBox(responseFromServer)
TextBox2.Text = responseFromServer
Button1.Enabled = True
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sKy As String = "lkirwf897+22#bbtrm8814z5qq=498j5" '32 chr shared ascii string (32 * 8 = 256 bit)
Dim sIV As String = "741952hheeyy66#cs!9hjv887mxx7@8y" '32 chr shared ascii string (32 * 8 = 256 bit)

Dim sTextVal As String = TextBox1.Text

Dim eText As String
Dim dText As String

eText = EncryptRJ256(sKy, sIV, sTextVal)
TextBox2.Text = DecryptRJ256(sKy, sIV, eText)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim sKy As String = "lkirwf897+22#bbtrm8814z5qq=498j5" '32 chr shared ascii string (32 * 8 = 256 bit)
Dim sIV As String = "741952hheeyy66#cs!9hjv887mxx7@8y" '32 chr shared ascii string (32 * 8 = 256 bit)

Dim sTextVal As String = TextBox1.Text

Dim eText As String
Dim dText As String
TextBox2.Text = EncryptRJ256(sKy, sIV, sTextVal)
End Sub
End Class

فایل PHP :


<?php

error_reporting(E_ALL);
if(isset($_POST['Player'])){
$ky = 'lkirwf897+22#bbtrm8814z5qq=498j5'; // 32 * 8 = 256 bit key
$iv = '741952hheeyy66#cs!9hjv887mxx7@8y'; // 32 * 8 = 256 bit iv
$from_vb = $_POST['Player']; // enter value from vb.net app here to test
$vtext = decryptRJ256($ky, $iv, $from_vb);
echo "Player : $vtext";

}

function decryptRJ256($key,$iv,$string_to_decrypt)
{

$string_to_decrypt = base64_decode($string_to_decrypt);

$rtn = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_decrypt, MCRYPT_MODE_CBC, $iv);

$rtn = rtrim($rtn, "\4");

return($rtn);

}
?>