MACTripleDES in PHP

后端 未结 2 517
余生分开走
余生分开走 2021-01-06 00:32

I am trying to get a MAC TripleDES equivalent of the C# MACTripleDES class.

I have tried following mcrypt(), but that is just encoding in TripleDES. I need to get an

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 01:08

    I'm not sure since Microsoft didn't bother to say what standard their class conforms to, but I suspect that this NIST document is what the Microsoft class is computing, only using triple DES in place of DES.

    I guess you will have to write your own method using the primitives in mcrypt.

    EDIT 1:

    Inspired by the bounty, I have these two examples showing equivalent result in PHP and C#.

    First, C#:

    using System;
    using System.Text;
    using System.Security.Cryptography;
    
    namespace TDESMacExample
    {
        class MainClass
        {
            public static void Main (string[] args)
            {
                var keyString = "012345678901234567890123";
                var keyBytes = Encoding.ASCII.GetBytes(keyString);
                var mac = new MACTripleDES(keyBytes);
                var data = "please authenticate me example number one oh one point seven niner";
                Console.WriteLine(data.Length);
                var macResult = mac.ComputeHash(Encoding.ASCII.GetBytes(data));
                Console.WriteLine(BitConverter.ToString(macResult));
                // B1-29-14-74-EA-E2-74-2D
            }
        }
    }
    

    Next, PHP:

        ";
        ?>
    

提交回复
热议问题