在PHP和.NET SHA 1日实施PHP、NET、SHA

2023-09-06 18:26:23 作者:浪荡王者

我工作的一个项目(基于PHP),其中我需要计算SHA1,我使用这行code生成SHA-1在PHP。

I'm working on a project(PHP Based) in which I need to compute SHA1, I'm using this line of code to generate SHA1 in PHP.

$da = file_get_contents("payload.txt");
echo sha1($da);

这是code对于.NET

and this is the code for the .Net

private static string GetSHA1(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            SHA1Managed hashString = new SHA1Managed();
            string hex = "";

            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }

不过,我很困惑,因为这两种语言产生不同的结果,我不能提供他们两人中的任何盐(因为我不知道盐b,来使用什么/ C的API并没有定义)

But I'm confused because both of languages generate different results, I'm not provide any salt in both of them(as I don't know what to use in salt b/c the api didn't defined that)

我还需要后工作在RSA(我有一个密钥加密) 另外谁能告诉,莫非这些算法不同,由于语言和我失去了???什么事

I also need to work on RSA after that(I've a key for the encryption) Also Can anyone tell, does these algorithms differ due to languages or any thing I'm missing???

需要一些专家认为这个

这是整个算法生成SHA1和RSA加密

this is the whole Algorithm to generate SHA1 and RSA encryption

<?php
class MyEncryption
{

    public $pubkey = '...public key here...';
    public $privkey = '...private key here...';

    public function encrypt($data)
    {
        if (openssl_private_encrypt($data, $encrypted, $this->pubkey))
            $data = base64_encode($encrypted);
        else
            throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');

        return $data;
    }

    public function decrypt($data)
    {
        if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey))
            $data = $decrypted;
        else
            $data = '';

        return $data;
    }
}

$enc = new MyEncryption();
$enc->pubkey = file_get_contents("server.key");
$payload = file_get_contents("payload1.txt");//payload1.txt contain xml data
$hashRes = sha1($payload,true);
echo $enc->encrypt($hashRes);


?>

非常感谢

推荐答案

这是你的统一codeEncoding 。 PHP不知道编码任何东西,所以你的字符串只是包含了所有的文件执行,1字节:1。在.NET不过,如果你读文件为文本,它将承担一定的编码(通常为UTF-8,如果没有另行指定),然后将其转换为内部重新presentation,以及你到底它转换为UTF-16和哈希的的字节 - 这很可能没有像原来的所有字节,除非原来也是UTF-16。而且即使是UTF-16,它可能包括字节顺序标记,其中统一codeEncoding.GetBytes()没有。即使没有,原来的字符串可能没有被归,你可能会在什么地方归其前进的道路上(你不告诉我们的code,你读它),并且字节会不同的了。等等。

It's your UnicodeEncoding. PHP doesn't know anything about encodings, so your string simply contains all the bytes that the file does, 1:1. In .NET however, if you read the file as text, it will assume some encoding (typically UTF-8, if you don't specify otherwise) and then converts it to the internal representation, and in the end you convert it to UTF-16 and hash those bytes - which are probably nothing like the original at bytes all, unless the original also was UTF-16. And even if it was UTF-16, it might have included the Byte-Order-Mark, which UnicodeEncoding.GetBytes() doesn't. And even if it didn't, the original string might not have been normalized, and you might have normalized it somewhere along the way (you don't show us the code where you read it), and the bytes will be different again. Etc.

正确的做法是将读取整个文件作为字节的二进制块(例如,使用 System.IO.File.ReadAllBytes()),然后直接凑这些字节。有没有必要涉及任何文字的编码和转换。然后将哈希应该匹配。

The correct approach would be to read the whole file as a binary block of bytes (for example, with System.IO.File.ReadAllBytes()), and then hash those bytes directly. There's no need to involve any text encodings and conversions. And then the hashes should match.