Maison >développement back-end >tutoriel php >C#下 des加密后跟php加密后 不匹配~求高人指点
C#代码预览
string beforeEncode = "usercode=18516094544&serviceid=140114021990&cporderid=0eb44dbd78a8deef3854b18794ebff5a4312&operationtype=3&channelid=00012243&version=1.0&time=20140923134312";string key = "Subs123";var encodebyte = WowEncryptDES(Encoding.UTF8.GetBytes(beforeEncode), key);string strEncode = Convert.ToBase64String(encodebyte);string strEncode1 = Server.UrlEncode(strEncode); /// <summary> /// MD5加密 /// </summary> /// <param name="value"></param> /// <returns></returns> public string MD5(string value) { return MD5(value, Encoding.GetEncoding("GBK")); } /// <summary> /// MD5加密 /// </summary> /// <param name="value"></param> /// <param name="encoding"></param> /// <returns></returns> public string MD5(string value, System.Text.Encoding encoding) { if (encoding == null) { encoding = System.Text.Encoding.Default; } MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] passKey = encoding.GetBytes(value); byte[] hashKey = md5.ComputeHash(passKey); StringBuilder md5String = new StringBuilder(); foreach (byte b in hashKey) { md5String.AppendFormat("{0:x2}", b); } return md5String.ToString(); } private readonly Encoding s_GB2312Encoding = Encoding.GetEncoding("GBK"); public byte[] WowEncryptDES(byte[] inputByteArray, string encryptKey) { using (var dCSP = new TripleDESCryptoServiceProvider()) { dCSP.Mode = CipherMode.CBC; dCSP.Padding = PaddingMode.Zeros; byte[] rgbKey = s_GB2312Encoding.GetBytes(MD5(encryptKey).ToLower().Substring(0, 24)); byte[] rgbIV = new byte[] { (byte)'0', (byte)'0', (byte)'0', (byte)'0', (byte)'0', (byte)'0', (byte)'0', (byte)'0' }; using (var mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write)) { cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return mStream.ToArray(); } } } }加密后结果 = C4diHm%2f8YVCLp%2b%2f10QuaPC3PiyDYgzJEpBtBJR6z2cyCH6HBq%2bVrMQAM%2f6Wakq5Y8kFvtMnZtpJa2J3e5ycF0I5utMvDno4u0981I9MrnTqpmkZFhDDFdIC2kM3t3Nt8IYicZ6eVfHnrPJQhn1b9UhP2BebkD5ip%2bmwoaJkIu%2fmkVVLt%2fJnpSFlZLS%2fP5L1tvps1BPjspOBayAYfGjwEgA%3d%3d
function Encrypt($data){ $iv = chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0); $key = 'Subs123'; // determine key bytes from the key text, using MD5 and wrapping around $key = md5($key); echo '$key</br>'.$key.'</br>'; $key = $key . $key; $tripleKey = substr($key, 0, mcrypt_get_key_size('tripledes', "cbc")); echo '$tripleKey</br>'.$tripleKey.'</br>'; // add PKCS#7 padding $blocksize = mcrypt_get_block_size('tripledes', "cbc"); $paddingSize = $blocksize - (strlen($data) % $blocksize); $data .= str_repeat(chr($paddingSize), $paddingSize); $encodedText = mcrypt_encrypt('tripledes', $tripleKey, $data, "cbc", $iv); return base64_encode($encodedText);}加密后=0SPyfexP%2FQA0x59PK3oQ%2BxeQDh6Dw%2BspG8TjjEDXTnVbWqu5Rdzx%2FzA8skcVdZtZoevMPnDaUhIQRwDzfMIkhK1Y5XGfj0FoI63fjPIp5CnEOK8bQPDfZbOcxWgKUkWDPSeWhTmJP3orpnA9CMygN%2BkHmvvetUxS2U%2BjZ6gR57%2B8Tjk8F3AmIpGZpgNXGhQMSJnivCRHp2IbxODBvGwkjg%3D%3D
在线等~~~~~
在线等~~~~~
如果不是我眼花了,那你的PHP里面用的好像是tripledes而不是des。
已解决......