2015-08-30 04:21:40 2412次浏览 2条回答 0 悬赏 500 金钱

php水平实在不行。只有请教各位了。 希望有朋友帮忙下。 不胜感激。

public static string Decrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            int num = Text.Length / 2;
            byte[] buffer = new byte[num];
            for (int i = 0; i < num; i++)
            {
                int num3 = Convert.ToInt32(Text.Substring(i * 2, 2), 0x10);
                buffer[i] = (byte) num3;
            }
            provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Encoding.Default.GetString(stream.ToArray());
        }
 
 public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(Text);
            provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            StringBuilder builder = new StringBuilder();
            foreach (byte num in stream.ToArray())
            {
                builder.AppendFormat("{0:X2}", num);
            }
            return builder.ToString();
        }

最佳答案

  • ZendLaravelYii 发布于 2015-08-31 15:42 举报

    额,重点是你看了C#代码了么,有些定义类中,根本不存在无法引用。
    如果明白自己的功能需求,何必硬要在网上百度来的C#代码进行PHP转换,倒不如自己思考用PHP写。
    直接问需求是什么好了。

    1 条回复
    回复于 2015-09-03 03:01 回复

    以解决,还是自己用php重写了遍。谢谢。

    觉得很赞
  • 回答于 2015-09-01 08:53 举报

    \Yii::$app->security->encryptByPassword/decryptByPassword和你需要的功能类似,如果只是跟你一样的需求做加密解密,这两个函数应该跟你的功能类似。

    但是不保证这两组函数能够解密加密你提供的C#加密解密得到的编码。

    因为就你提供的这组加解密函数来看,无法确定其采用的加解密算法(DES也有好几种变形),无法确定采用何种算法的情况下PHP函数要能和C#打通只能自己慢慢试了..

    1 条回复
    回复于 2015-09-03 03:01 回复

    以解决,还是自己用php重写了遍。谢谢。

您需要登录后才可以回答。登录 | 立即注册
xjdata
CEO

xjdata

注册时间:2011-12-07
最后登录:2021-07-09
在线时长:112小时28分
  • 粉丝23
  • 金钱14169
  • 威望75
  • 积分16039

热门问题