douwen5681 2008-10-22 04:03
浏览 70
已采纳

在C#中解密PHP加密字符串

I have a string encrypted in PHP that I would like to decrypt in C#. I used the tutorial below to do the encryption, but am having problems decrypting. Can anyone post an example on how to do this?

http://www.sanity-free.org/131/triple_des_between_php_and_csharp.html

  • 写回答

2条回答 默认 最新

  • dongyan1974 2008-10-22 04:56
    关注

    Hope this helps:

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Decrypt("47794945c0230c3d"));
        }
    
        static string Decrypt(string input)
        {
            TripleDES tripleDes = TripleDES.Create();
            tripleDes.IV = Encoding.ASCII.GetBytes("password");
            tripleDes.Key = Encoding.ASCII.GetBytes("passwordDR0wSS@P6660juht");
            tripleDes.Mode = CipherMode.CBC;
            tripleDes.Padding = PaddingMode.Zeros;
    
            ICryptoTransform crypto = tripleDes.CreateDecryptor();
            byte[] decodedInput = Decoder(input);
            byte[] decryptedBytes = crypto.TransformFinalBlock(decodedInput, 0, decodedInput.Length);
            return Encoding.ASCII.GetString(decryptedBytes);
        }
    
        static byte[] Decoder(string input)
        {
            byte[] bytes = new byte[input.Length/2];
            int targetPosition = 0;
    
            for( int sourcePosition=0; sourcePosition<input.Length; sourcePosition+=2 )
            {
                string hexCode = input.Substring(sourcePosition, 2);
                bytes[targetPosition++] = Byte.Parse(hexCode, NumberStyles.AllowHexSpecifier);
            }
    
            return bytes;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿