공개 키는 서명을 어떻게 확인합니까?
공개 / 개인 키가 어떻게 작동하는지 더 잘 파악하려고합니다. 발신자가 문서의 해시를 얻기 위해 개인 키를 사용하여 문서에 디지털 서명을 추가 할 수 있지만 이해하지 못하는 것은 공개 키를 사용하여 해당 서명을 확인하는 방법입니다.
내 이해는 공개 키가 암호화되고 개인 키가 해독된다는 것입니다.
데이터 / 메시지 암호화에 대해서는 "공개 키 암호화, 개인 키 해독"에 대한 이해가 정확합니다. 디지털 서명의 경우에는 그 반대입니다. 디지털 서명을 사용하면 본인이 서명 한 문서가 본인에게서 온 것임을 증명하려고합니다. 그러기 위해서는 개인 키만 가지고 있어야합니다.
가장 간단한 설명의 디지털 서명은 서명자의 개인 키로 암호화 된 데이터 (파일, 메시지 등)의 해시 (SHA1, MD5 등)입니다. 그것이 서명자가 가지고 있거나 신뢰할 수있는 것이어야하는 것이기 때문입니다. 누구나 서명자의 공개 키에 액세스 할 수 있어야합니다.
따라서 디지털 서명을 확인하려면 수신자
- 동일한 데이터 (파일, 메시지 등)의 해시를 계산합니다.
- 발신자의 PUBLIC 키를 사용하여 디지털 서명을 해독하고
- 2 개의 해시 값을 비교합니다.
일치하면 서명이 유효한 것으로 간주됩니다. 일치하지 않으면 다른 키를 사용하여 서명했거나 데이터가 의도적으로 또는 실수로 변경되었음을 의미합니다.
희망이 도움이됩니다!
키는 반대로 작동합니다.
공개 키 암호화, 개인 키 해독 (암호화) :
openssl rsautl -encrypt -inkey public.pem -pubin -in message.txt -out message.ssl
openssl rsautl -decrypt -inkey private.pem -in message.ssl -out message.txt
개인 키 암호화, 공개 키 해독 (서명) :
openssl rsautl -sign -inkey private.pem -in message.txt -out message.ssl
openssl rsautl -inkey public.pem -pubin -in message.ssl -out message.txt
다음은이 전체 흐름을 테스트하는 예제 스크립트입니다 openssl
.
#!/bin/sh
# Create message to be encrypted
echo "Creating message file"
echo "---------------------"
echo "My secret message" > message.txt
echo "done\n"
# Create asymmetric keypair
echo "Creating asymmetric key pair"
echo "----------------------------"
openssl genrsa -out private.pem 1024
openssl rsa -in private.pem -out public.pem -pubout
echo "done\n"
# Encrypt with public & decrypt with private
echo "Public key encrypts and private key decrypts"
echo "--------------------------------------------"
openssl rsautl -encrypt -inkey public.pem -pubin -in message.txt -out message_enc_pub.ssl
openssl rsautl -decrypt -inkey private.pem -in message_enc_pub.ssl -out message_pub.txt
xxd message_enc_pub.ssl # Print the binary contents of the encrypted message
cat message_pub.txt # Print the decrypted message
echo "done\n"
# Encrypt with private & decrypt with public
echo "Private key encrypts and public key decrypts"
echo "--------------------------------------------"
openssl rsautl -sign -inkey private.pem -in message.txt -out message_enc_priv.ssl
openssl rsautl -inkey public.pem -pubin -in message_enc_priv.ssl -out message_priv.txt
xxd message_enc_priv.ssl
cat message_priv.txt
echo "done\n"
이 스크립트는 다음을 출력합니다.
Creating message file
---------------------
done
Creating asymmetric key pair
----------------------------
Generating RSA private key, 1024 bit long modulus
...........++++++
....++++++
e is 65537 (0x10001)
writing RSA key
done
Public key encrypts and private key decrypts
--------------------------------------------
00000000: 31c0 f70d 7ed2 088d 9675 801c fb9b 4f95 1...~....u....O.
00000010: c936 8cd0 0cc4 9159 33c4 9625 d752 5b77 .6.....Y3..%.R[w
00000020: 5bfc 988d 19fe d790 b633 191f 50cf 1bf7 [........3..P...
00000030: 34c0 7788 efa2 4967 848f 99e2 a442 91b9 4.w...Ig.....B..
00000040: 5fc7 6c79 40ea d0bc 6cd4 3c9a 488e 9913 _.ly@...l.<.H...
00000050: 387f f7d6 b8e6 5eba 0771 371c c4f0 8c7f 8.....^..q7.....
00000060: 8c87 39a9 0c4c 22ab 13ed c117 c718 92e6 ..9..L".........
00000070: 3d5b 8534 7187 cc2d 2f94 0743 1fcb d890 =[.4q..-/..C....
My secret message
done
Private key encrypts and public key decrypts
--------------------------------------------
00000000: 6955 cdd0 66e4 3696 76e1 a328 ac67 4ca3 iU..f.6.v..(.gL.
00000010: d6bb 5896 b6fe 68f1 55f1 437a 831c fee9 ..X...h.U.Cz....
00000020: 133a a7e9 005b 3fc5 88f7 5210 cdbb 2cba .:...[?...R...,.
00000030: 29f1 d52d 3131 a88b 78e5 333e 90cf 3531 )..-11..x.3>..51
00000040: 08c3 3df8 b76e 41f2 a84a c7fb 0c5b c3b2 ..=..nA..J...[..
00000050: 9d3b ed4a b6ad 89bc 9ebc 9154 da48 6f2d .;.J.......T.Ho-
00000060: 5d8e b686 635f b6a4 8774 a621 5558 7172 ]...c_...t.!UXqr
00000070: fbd3 0c35 df0f 6a16 aa84 f5da 5d5e 5336 ...5..j.....]^S6
My secret message
done
공개 키는 암호화되며 개인 키만 해독 할 수 있으며 그 반대도 마찬가지입니다. 둘 다 다른 해시로 암호화하지만 각 키는 다른 암호화를 해독 할 수 있습니다.
일부 발신자가 보낸 메시지를 확인하는 몇 가지 방법이 있습니다. 예를 들면 다음과 같습니다.
발신자는 다음을 보냅니다.
메시지
개인 키로 암호화 된 메시지의 해시
수신자:
- 메시지를 얻기 위해 공개 키로 서명 (2)을 해독합니다. 아마도 (1)과 같은 메시지이지만 아직 알 수 없습니다. 이제 확인해야 할 두 가지 메시지가 동일합니다. 이를 위해 공개 키로 암호화하고 두 해시를 비교합니다. 그래서 우리는 ....
- Encrypt the original message (1) with the public key to obtain a hash
- Encrypt the decrypted message (3) to get a second hash and compare to (4) to verify that they are identical.
If they aren't identical it means either the message was tampered with or it was signed with some other key and not the one we thought...
Another example would be for the sender to use a common hash that the receiver might know to use as well. For example:
The sender sends:
- A message
- Takes a known hash of the message, then encrypts the hash with the private key
The receiver:
- Decrypts (2) and gets a hash value
- Hashes the message (1) with the same hash used by the sender
- Compares the two hashes to make sure they match
This again ensures the message wasn't tampered with and it is from the expected sender.
Thought I'd provide a supplemental explanation for anyone looking for something more intuitively revealing.
A big part of this confusion arises from naming 'public keys' and 'private keys' as such because how these things actually work is directly at odds with how a 'key' is understood to be.
Take encryption for example. It could be thought of as working like so:
- The parties that want to be able to read the secret messages each keep a key hidden (i.e. a private key)
- The parties that want to be able to send secret messages all have the ability to obtain an unlocked locked (i.e. a public lock)
- Then sending a secret message is as easy as locking it with an unlocked lock, but unlocking it afterwards can only be done with one of the hidden keys.
This allows secret messages to be sent between parties, but from an intuitive standpoint here, 'public lock' is a more suitable name than 'public key'.
However, for sending digital signatures the roles are somewhat reversed:
- The party that wants to sign messages is the only one with access to the unlocked locks (i.e. a private lock)
- The parties that want to verify the signature all have the ability to obtain a key (i.e. a public key)
- Then what the signer does is create two identical messages: the one that anyone can read and one to accompany it, but which they lock with one of their private locks.
Then when the receiver gets the message, they can read it, and then use the public key to unlock the locked message and compare the two messages. If the messages are the same, then they know that:
The unlocked message wasn't tampered with during travel and,
The message must have been from the person who has the matching lock to their public key.
And finally, this entire system only works if anyone who wants to validate a signer's signature has an authoritative place to go to to get the matching key to the signer's locks. Otherwise, anyone can say "Hey, here's the key to so-and-so's private lock", send you a message pretending to be them but lock it with their private lock, you perform all the above steps and believe the message must actually be from the person you thought, but you're fooled because you were mislead as to the true owner of a public key.
So long as there's a trust-worthy source for retrieving a signer's public key, you'll know who the rightful owner of a public key is, and will be able to validate their signature.
참고URL : https://stackoverflow.com/questions/18257185/how-does-a-public-key-verify-a-signature
'IT' 카테고리의 다른 글
grep을 grep에 파이핑 한 후 색상 유지 (0) | 2020.06.29 |
---|---|
XML 스키마를 시각화하는 방법? (0) | 2020.06.29 |
Spring의 @RequestBody 및 @ResponseBody 주석 (0) | 2020.06.29 |
C # 변수 이름 앞에 @를두면 무엇을합니까? (0) | 2020.06.29 |
Vue에서 계산 된 방법과 방법 (0) | 2020.06.29 |