我正在尝试使用PHP中的DES算法加密纯文本。我设法用C#编写了一个工作示例:String plaintext = "1y7wpc4iddseyrwez1lor8ow3297t.pd0bfl";String pass = "rpsxi.t.rjsmklexarygfqyrrkhwdjh";byte[] bytesPlain = Encoding.UTF8.GetBytes(plaintext);byte[] bytesPass = Encoding.UTF8.GetBytes(pass).Take(8).ToArray();DESCryptoServiceProvider p = new DESCryptoServiceProvider();p.Mode = CipherMode.ECB;ICryptoTransform t = p.CreateEncryptor(bytesPass, bytesPass);CryptoStreamMode m = CryptoStreamMode.Write;MemoryStream stream = new MemoryStream();CryptoStream cryptoStream = new CryptoStream(stream, t, m);cryptoStream.Write(bytesPlain, 0, bytesPlain.Length);cryptoStream.FlushFinalBlock();byte[] encBytesPlain = new byte[stream.Length];stream.Position = 0;stream.Read(encBytesPlain, 0, encBytesPlain.Length);string encrypted = Convert.ToBase64String(encBytesPlain);System.Diagnostics.Debug.WriteLine(encrypted);plaintext = 1y7wpc4iddseyrwez1lor8ow3297t.pd0bfl密码= rpsxi.t.rjsmklexarygfqyrrkhwdjh(仅密码的前8个字节用于加密消息)。无论如何,当我尝试使用crypt函数或我在github https://github.com/phpseclib/phpseclib-php5/blob/master/phpseclib/Crypt/DES.php上找到的此类尝试编写PHP版本时,我没有不会得到与C#相同的结果。我完全确定C#代码可以正常工作,但是我真的不知道如何在PHP中创建工作版本以在Base64中获得相同的结果。Edit1:我已经知道DES不再安全了,但是我仅出于学习目的而需要在网页中使用它,而且编写C#代码的方式是完全正确的,因为我需要遵循一些说明并获得准确的结果。
2 回答
- 2 回答
- 0 关注
- 180 浏览
添加回答
举报
0/150
提交
取消