Reputation: 25
I am Encrypting a string in c# and sending this to a php page like this.
sfplr.Attributes.Add("href", "http://sml.com.pk/a/sfpl/reports.php?id=" + Convert.ToBase64String(Encoding.Unicode.GetBytes(emailid)));
and the url genrated from this code is like this
http://sml.com.pk/a/sfpl/reports.php?id=bQBhAGwAaQBrAC4AYQBkAGUAZQBsAEAAcwBoAGEAawBhAHIAZwBhAG4AagAuAGMAbwBtAC4AcABrAA==
Now I want to decrypt again this bQBhAGwAaQBrAC4AYQBkAGUAZQBsAEAAcwBoAGEAawBhAHIAZwBhAG4AagAuAGMAbwBtAC4AcABrAA==
in php o display in php page.Please any one help me to do this
Upvotes: 0
Views: 294
Reputation: 518
Using the base64_decode
function will return the bytes you originally passed into the convert.toBase64String
.
After that you'll have to retreive them in the following way :
<?php $emailBytes = base64_decode($_GET['id']); ?>
That will return, if it has been serialized correctly, the array you made in C# by using the Convert.toBase64String, see Encoding.getBytes.
Meaning, if the array is passed as you expected it to be (which I doubt)
$msg = "";
foreach ($emailBytes as $character) {
$msg .= (char) $character;
}
I doubt you even need the Encoding.getBytes , you can just past a string as an argument.
In C# use
Convert.ToBase64String(emailid);
And then all you'd have to do in php is the retrieval in the way I first mentioned, that will give you the original passed String.
Also, as pointed out several times, that is an encryption you're performing, it's just making sure that it can be passed in the URL in a way no conflicts occur.
If you do want to encrypt, use a library on the C# side, encode the message, base64 encode it. Then on PHP side you'd have to do the reverse.
Upvotes: 0
Reputation: 2288
Try using base64_decode
function
Reference http://php.net/manual/en/function.base64-decode.php
In your case, it will be:
<?php
echo base64_decode($_GET['id']);
?>
Upvotes: 2
Reputation: 6147
$email = base64_decode("bQBhAGwAaQBrAC4AYQBkAGUAZQBsAEAAcwBoAGEAawBhAHIAZwBhAG4AagAuAGMAbwBtAC4AcABrAA==");
echo $email; //****** You will get email here... */
Please refer Is "convert.tobase64string" in .Net equal to "base64_encode" in PHP?
Upvotes: 2