function encrypted_id($dfsid) { $key ="3go8&$8*3*3h0k(2)2"; $key_len = strlen($key); for($i = 0; $i < strlen($dfsid); $i++){ $dfsid[$i] = $dfsid[$i] ^ $key[$i % $key_len]; } $raw_code = base64_encode(md5($dfsid, true)); $code = str_replace(array("/", "+"), array("_", "-"), $raw_code); return $code; } echo encrypted_id("1255642278953624");
这是php代码,帮忙翻译成C#的。
下面是本人本人写的代码,但是转码后的结果不一致。
public string encrypted(string id) { string key = "3go8&$8*3*3h0k(2)2"; int key_len = key.Length; int length = id.Length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.Append((id[i] ^ key[i % key_len]).ToString()); } //转换成md5 string md5String =FormsAuthentication.HashPasswordForStoringInConfigFile(sb.ToString(), "md5"); //转换成base64 byte[] byteArray = System.Text.Encoding.Default.GetBytes(md5String); string content = Convert.ToBase64String(byteArray); //替换 string code = content.Replace("/","_"); code = code.Replace("+","-"); return code; }
正确的话,如 id为 1255642278953624 加密后为WV3B2aSwkN52NLLGcFT9Yw==
但是本人解码出来的结果是 NjUyMzlGRkUxMDI4QTQzNEEwRTUzNDg2M0U5MEZGRjg=
有没有大神帮忙解答一下的
解决方案
30
static void Main(string[] args) { var id = "1255642278953624"; string key = "3go8&$8*3*3h0k(2)2"; int key_len = key.Length; int length = id.Length; char[] ids = id.ToArray(); //StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { ids[i] = (char)(id[i] ^ key[i % key_len]); //sb.Append((id[i] ^ key[i % key_len]).ToString()); } //转换成base64 byte[] byteArray = System.Text.Encoding.Default.GetBytes(ids); MD5 md5 = new MD5CryptoServiceProvider(); byte[] output = md5.ComputeHash(byteArray); var base64String = Convert.ToBase64String(output); //base64String = base64String.Replace("/", "_"); //base64String = base64String.Replace("+", "-"); Console.WriteLine(base64String); Console.ReadLine(); }
最后输出是WV3B2aSwkN52NLLGcFT9Yw==
20
static void Main(string[] args) { var p = encrypted("1255642278953624"); Console.WriteLine(p); //WV3B2aSwkN52NLLGcFT9Yw== } public static string encrypted(string id) { string key = "3go8&$8*3*3h0k(2)2"; int key_len = key.Length; var byteArray = id.Select((x, i) => (byte)(x ^ key[i % key_len])).ToArray(); MD5 md5 = new MD5CryptoServiceProvider(); byte[] output = md5.ComputeHash(byteArray); var base64String = Convert.ToBase64String(output); return base64String; }