我试图在OpenSSL中复制此博客上显示的工作流程: http://farid.hajji.name/blog/2009/07/27/public-key-cryptography-with-openssl/
然而,尽管存在许多变化,但身份验证似乎失败了。出了什么问题?请参阅下面的代码,您可以复制并粘贴到OpenSSL中。请注意,我使用的是OpenSSL的已编译Windows二进制版本。
//================Phase 1 - Setup================
//Generate my private key (myprivatekey.txt)
genpkey -algorithm RSA -out C:\myprivatekey.txt -pass pass:abc123 -pkeyopt rsa_keygen_bits:2048
//Generate friend's private key (friendprivatekey.txt)
genpkey -algorithm RSA -out C:\friendprivatekey.txt -pass pass:123abc -pkeyopt rsa_keygen_bits:2048
----------------
//Extract my public key (mypublickey.txt) from my private key (myprivatekey.txt)
rsa -passin pass:abc123 -in C:\myprivatekey.txt -pubout -out C:\mypublickey.txt
//Extract friend's public key (friendpublickey.txt) from my private key (friendprivatekey.txt)
rsa -passin pass:123abc -in C:\friendprivatekey.txt -pubout -out C:\friendpublickey.txt
----------------
//Generate my password (a random base64 string password saved mypassword.txt)
rand -base64 -out C:\mypassword.txt 128
//Generate friend's password (a random base64 string password saved to friendpassword.txt)
rand -base64 -out C:\friendpassword.txt 128
//Delete the .rnd file that's generated? Not sure what it is.
----------------
//Encrypt my password using my private key (encrypted password saved to a binary file - myencryptedpassword.txt)
pkeyutl -in C:\mypassword.txt -out C:\myencryptedpassword.txt -inkey C:\myprivatekey.txt -passin pass:abc123
//Encrypt friend's password using friend's private key (encrypted password saved to a binary file - friendencryptedpassword.txt)
pkeyutl -in C:\friendpassword.txt -out C:\friendencryptedpassword.txt -inkey C:\friendprivatekey.txt -passin pass:123abc
----------------
//Convert my encrypted password to base64 from binary (saved as myencryptedpasswordbase64.txt)
base64 -in C:\myencryptedpassword.txt -out C:\myencryptedpasswordbase64.txt
//Convert friend's encrypted password to base64 from binary (saved as friendencryptedpasswordbase64.txt)
base64 -in C:\friendencryptedpassword.txt -out C:\friendencryptedpasswordbase64.txt
----------------
//Create a signed hash of my password so my friend knows it's coming from me (signed hash saved as mysignedhash.txt and is in binary form)
dgst -sha256 -sign C:\myprivatekey.txt -passin pass:abc123 -out C:\mysignedhash.txt C:\myencryptedpasswordbase64.txt
//Create a signed hash of friend's password so I know it's coming from my friend (signed hash saved as friendsignedhash.txt and is in binary form)
dgst -sha256 -sign C:\friendprivatekey.txt -passin pass:123abc -out C:\friendsignedhash.txt C:\friendencryptedpasswordbase64.txt
----------------
//Convert my signed hash from binary to base64
base64 -in C:\mysignedhash.txt -out C:\mysignedhashbase64.txt
//Convert friend's signed hash from binary to base64
base64 -in C:\friendsignedhash.txt -out C:\friendsignedhashbase64.txt
//================Phase 2 - Authentication================
//Now, we reverse the process and authenticate the friend. Let's prefix all output files with "phase2"
//I provide friend with my public key and my encrypted password
//Friend provides me with their public key
//Convert friend's encrypted password from base64 to binary. The output file will be the same as friendsignedhash.txt
base64 -d -in C:\friendsignedhashbase64.txt -out C:\phase2friendsignedhash.txt
//Convert friend's signed hash from base64 to binary. The output file will be the same as C:\friendsignedhash.txt
base64 -d -in C:\friendencryptedpasswordbase64.txt -out C:\phase2friendencryptedpassword.txt
//Verify if the password originates from my friend (by checking against my friend's public key)
dgst -sha256 -verify C:\friendpublickey.txt -signature C:\phase2friendsignedhash.txt -out C:\friendresult.txt C:\phase2friendencryptedpassword.txt
知道验证失败的原因吗?
答案 0 :(得分:0)
问题是您签名friendencryptedpasswordbase64.txt
,但正在尝试使用phase2friendencryptedpassword.txt
(非base64)进行验证。如果您使用前者验证,它可以完美运行!
除此之外,我确实看到了一些与安全相关的问题。它们并没有引起您提出的问题,但它们违反了良好的安全措施。
首先,您可能希望使用朋友的公钥而不是私钥来加密密码。然后只有你朋友的私钥才能解密它。否则,任何拥有您公钥的人都可以解密您的加密密码。
其次,通常,您不希望将相同的密钥用于多种目的(即签名和加密)。 Use a separate key for each function