我有一个用于保存高分表的sql数据库。当我发送数据时,我有这个PHP代码:
<?php
$db = mysql_connect('mysql', 'myuser', 'mypass') or die('Could not connect: ' . mysql_error());
mysql_select_db('my_db') or die('Could not select database');
// Strings must be escaped to prevent SQL injection attack.
$name = mysql_real_escape_string($_GET['name'], $db);
$score = mysql_real_escape_string($_GET['score'], $db);
$hash = $_GET['hash'];
$secretKey="mySecretKey";
$real_hash = md5($name . $score . $secretKey);
if($real_hash == $hash) {
// Send variables for the MySQL database class.
$query = "insert into scores values (NULL, '$name', '$score');";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
?>
现在,我在c#中有两段代码...一台发送数据,调用函数和函数本身...
发送数据:
IEnumerator PostScores(string name, int score)
{
string hash = Md5Functions.Md5Sum(name + score + secretKey);
string post_url = addScoreURL + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
WWW hs_post = new WWW(post_url);
yield return hs_post;
if (hs_post.error != null)
{
print("There was an error posting the high score: " + hs_post.error);
}
}
当一切正常时,这就是我的功能:
using UnityEngine;
using System.Collections;
Public Static Md5Functions {
public static string Md5Sum(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
// encrypt bytes
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++)
{
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
}
现在,由于我必须将整个解决方案导入Visual Studio而且它不允许MD5CryptoServiceProvider,我已经搜索过并且我更改了这个Md5Function:
using UnityEngine;
using System.Collections;
#if UNITY_METRO
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
#else
using System.Text;
using System.Security.Cryptography;
#endif
public static class Md5Functions
{
static string md5val;
static void Start()
{
md5val = Md5Sum("Hello World!");
Debug.Log(md5val);
}
static void OnGUI()
{
GUILayout.Label(md5val);
}
public static string Md5Sum(string strToEncrypt)
{
#if UNITY_METRO
// Convert the message string to binary data.
IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(strToEncrypt, BinaryStringEncoding.Utf8);
// Create a HashAlgorithmProvider object.
HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
// Demonstrate how to retrieve the name of the hashing algorithm.
string strAlgNameUsed = objAlgProv.AlgorithmName;
// Hash the message.
IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);
// Verify that the hash length equals the length specified for the algorithm.
if (buffHash.Length != objAlgProv.HashLength)
return null;
// Convert the hash to a string (for display).
string strHashBase64 = CryptographicBuffer.EncodeToBase64String(buffHash);
// Return the encoded string
return strHashBase64.PadLeft(32, '0');
#else
UTF8Encoding ue = new UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
// encrypt bytes
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++)
{
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
#endif
}
}
当然,现在,它不起作用,因为php代码是为第一个Md5Function制作的......
愿你们中的任何人帮我解决这个问题,所以我可以让它再次运行,请使用最后一个Md5Function吗?