gpt4 book ai didi

c# - 数字签名如何保证消息已由 'Alice' 发送

转载 作者:行者123 更新时间:2023-11-30 22:16:25 25 4
gpt4 key购买 nike

我相信我了解数字签名的工作原理,但我仍然不明白它如何仍然保证消息已被已知发件人 (Alice) 加密。

让我们假设 Alice 想给 Bob 发送消息。爱丽丝和鲍勃上周见过面,鲍勃正在等待回复 yesno。汤姆是中层黑客

1。没有数字签名的场景:

// 1) Alice get's Bob public key
var bobPubKey = GetBobsPubKey();

// 2) Alice encrypts yes with bob's public key
var encryptedMessage = AssymetricEncryption.Encrypt("YES", bobPubKey );

// 3) Send message to bob
sendMsgToBob(encryptedMessage);

这种方法的问题是 Tom 可能截取了消息 encryptedMessage 并将该消息替换为 var newEncryptedMsg = AssymetricEncryption.Encrypt("NO!, bobPubKey ); 当 bob 收到消息时,他不知道它已被 Tom 修改!

2。使用数字签名的场景

// 1) Alice get's Bob's public key 
var bobPubKey = GetBobsPubKey();

// 2) Alice encrypts 'yes' with bob's public key
var encryptedMessage = AssymetricEncryption.Encrypt("YES", bobPubKey );

// 3) Alice creates a hash of the encrypted message
var hashOfEncMsg = Sha1(encryptedMessage);

// 4) Alice encryptes that hash with her priveate key
var digitalSignature = AssymetricEncryption.Encrypt(hashOfEncMsg , alicePrivKey);

// 5) Alice sends both the encryptedMsg plus the signature to Bob
var msgToSend = string.Format("someUrl.asp?encMsg={0}&digSignatrue={1}",encryptedMessage , digitalSignature );
sendBobAMsg(msgToSend ); // msg contains encryptedConted + digital Signature

这是鲍勃必须做的事情,以确保消息来自爱丽丝

// 0) Bob receives encrypted msg plus digital signatrue
var msg = RecieveMsg();
var digitalSignature = GetDigSgnatureFromMsg(msg);
var encryptedMessage = GetEncyptedMsgFromMsg(msg);

// 1) Decrypt the msg
var plainText = AssymetricEncryption.Decrypt(encryptedMessage , bobPrivateKey ); // = YES

/* now cause bob also received a digital signature let's do more steps to guaranty that the message came from Alice */

// 2) get Alice public key
var alicePubKey = GetAlicePubKey();

// 2) Create the same hash that Alice created for the msg
var ciphertext = AssymetricEncryption.Encrypt(plainText , bobPubKey ); // here plainText = YES
var hashOfEncMsg = Sha1(ciphertext);

// 3) Decrypt DigitalSignature hash with Alice public key
var aliceHash = AssymetricEncryption.Decrypt(digitalSignature , alicePublicKey);

// 4) Here alice Hash must equal hashOfEncMsg
if( hashOfEncMsg != aliceHash ) { throw new exception("Message has been modified or it does not come from Alice!");

所以我的问题是关于最后的第 4 步,其中 Alice 哈希必须等于 hashOfEncMsg。为什么如果此验证成立,Bob 可以保证消息来自 Alice?

我相信 Tom 仍然可以修改消息,方法如下:(我可能在某处错了;数字签名不可能不是他们声称的那样)。

  1. Tom 拦截 Alice 的消息

  2. Tom 知道该消息具有数字签名,因此他生成了一个 key 组合,其中他的公钥将与 Alice 的公钥相同。 (虽然 Tom 的私钥与 Alice 的不同)

  3. Tom 用 Bob 的公钥加密“No”

  4. Tom 像 Alice 一样创建数字签名,但使用他的私钥

  5. 当 bob 收到消息时,他用他的私钥解密它,他看到“否”

  6. 然后 bob 将验证签名以查看消息是否来自 alice

  7. bob 用他的公钥加密“No”并计算哈希值。即 = hash1

  8. bob 解密 get 的 alice 公钥,该公钥与 tom 的相同。

  9. 然后鲍勃使用该 key 解密挖掘签名。解密该挖掘签名应该等于 hash1 ! .

  10. Bob 现在认为 Alice 发送了 NO!


编辑

根据我收到的回复,这里是问题的解决方案:

Bob 可以保证消息来自 Alice,因为 Tom 无法生成与 Alice 公钥在数学上相关的私钥。换句话说,第 2 步(Tom 知道消息有数字......)将需要很多时间。因此,如果爱丽丝在合理的时间内回复,那么鲍勃可以保证消息来自爱丽丝。

最佳答案

汤姆必须加密“不!”拥有可以用爱丽丝的公钥解密的私钥,他不能使用自己的私钥。

这意味着 Tom 需要一对私钥/公钥,但他只知道公钥。从公钥计算私钥在计算上非常困难(在合理的时间内实际上是不可能的)。

关于c# - 数字签名如何保证消息已由 'Alice' 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17390810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com