我使用OpenSSL创建了一个RSA密钥对:
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:4096
openssl rsa -pubout -in private_key.pem -out public_key.pem
这些是关键:
-----BEGIN PRIVATE KEY-----
MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQC7ZRktot/dOrNX
nyc8u1qtQM7Pb+OOLupSkqsVKeBaT1J/Yru9XF2EqlUo9aBSsjZLw1Q7TP8Ocj2s
Boj09otqgb9fjo/OPybhvr+z1FdmwRSShnZ5aGvNdM3eVklr98DrY+anYLPGE8GM
H3WL4tFKFtu2HK5E8NLLAuZx3SoNRnFJ2MZV4irf/vkdSOVRo9+u6f2Hj/0Swftq
SRfkF2gXJUUG0cesrFSPqYyFH5qbH6dDkwTAH27WcFt+X/YNkkzyC/wVtI/b0Ng1
Adjiqa84kxHADoeyPBmRNlCio3T4bx3hDYkSEhpRJQGMqEQdPTdU3lQBixCked3m
aOWW1+SGcqCvNnDAQsgdPa8PaQimYbefwQPDDT3fdKev/Vsip5/9B605QPONaRfO
IUg/Kfh/TL722t4ccFHZfXBKBF/JapMy0aY7e8U5CtkIu0Uh9Y2ZEHTaOMMxXCOK
UuO2rL4ki2MlnwC+zaduolKPPm8pI2tbVv2I9vtT3hSQOMrDFBeCtCVuv0lbu8Em
t+VhtPYvqbUvKlsoC0IzRhdnPhUjkJhZh6pnwxFBgkzpry+3mNQgTLAmHx86xROE
MqI7haMTXyakEKDEjM87Wl0fM8BqxxOILJ/1dp5iNAj7sn3F1EPhjwZHhswzlQQI
r68NPNvuGhYr0ch7ufezybKOlbKGJwIDAQABAoICAHKHiWng350gITfuJ1bw0qyM
rVkOcy8U8VViGnmmzF2lmTANsdw7tVQ9niqYZFHmZVTMnpBXsJKts4+NtuqwO+ju
P34xbSaKZkMWB6fqoThFzUL4NzRgY3KAbsnyRpFcLGYw+mlVqsnmjSYMnBTQnUUA
z2tv//pWjR5SOXFh0C+o29hcSmt2/OlFtg713GGXH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FYWr/TM1
eYTR9iGpRLGN+MBwpZIr94z02nrl0GhNNoyq+skDjtJ2Bi66YIe/IJu86mIZu5Ma
FxHiQ4CS5eVAn5hotPLcR/0B56rCZw==
-----END PRIVATE KEY-----
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAu2UZLaLf3TqzV58nPLta
rUDOz2/jji7qUpKrFSngWk9Sf2K7vVxdhKpVKPWgUrI2S8NUO0z/DnI9rAaI9PaL
aoG/X46Pzj8m4b6/s9RXZsEUkoZ2eWhrzXTN3lZJa/fA62Pmp2CzxhPBjB91i+LR
ShbbthyuRPDSywLmcd0qDUZxSdjGVeIq3/75HUjlUaPfrun9h4/9EsH7akkX5Bdo
FyVFBtHHrKxUj6mMhR+amx+nQ5MEwB9u1nBbfl/2DZJM8gv8FbSP29DYNQHY4qmv
OJMRwA6HsjwZkTZQoqN0+G8d4Q2JEhIaUSUBjKhEHT03VN5UAYsQpHnd5mjlltfk
hnKgrzZwwELIHT2vD2kIpmG3n8EDww0933Snr/1bIqef/QetOUDzjWkXziFIPyn4
f0y+9treHHBR2X1wSgRfyWqTMtGmO3vFOQrZCLtFIfWNmRB02jjDMVwjilLjtqy+
JItjJZ8Avs2nbqJSjz5vKSNrW1b9iPb7U94UkDjKwxQXgrQlbr9JW7vBJrflYbT2
L6m1LypbKAtCM0YXZz4VI5CYWYeqZ8MRQYJM6a8vt5jUIEywJh8fOsUThDKiO4Wj
E18mpBCgxIzPO1pdHzPAascTiCyf9XaeYjQI+7J9xdRD4Y8GR4bMM5UECK+vDTzb
7hoWK9HIe7n3s8myjpWyhicCAwEAAQ==
-----END PUBLIC KEY-----
接下来,我创建了包含我的消息的文件:
echo "Hello, World" > message.txt
然后我用我的私钥对邮件进行了签名,并将签名存储在base64中的文件中:
openssl dgst -sha256 -sign private_key.pem -out signature message.txt
base64 signature > signature.txt
这是消息的签名:
huMEGAIfj+RQ4XHLVYEkGlgbcz5Wv62QBWk6MsxbiDBIX1ZgjhjKY3zr8livAs3x4QjfOmxqs+hOGDo9einXlNWWinslD9q+0ScoJPauK2UuIbAzLsOFoUGJBlFvCALClDlP+oIkT8qLzVPL2gONln3G68p/yZdKIQldaVMDA3mQEBJgHLKxShW1iveqhXF9P6SijE6PstTZ21KEOgMKXVjqbA8EdYucWZv1IwllFXfWJuk36MrgcfGTDesjK95LgCaZ0MjQqsiQQFA3tmZSntE8oRs7UK2fBkx/RCoFIHut4szdaUUvvxLNXVq5hgEjxysvw8Pls86iC/kfu84fJL6Xc3z9vcw4j4H2u5iJS+XGCcW4Jk+2UNprkyLtTu3uWvz21ruIZTlPhDNXdMexr9DNf7WyIsIZ3zeM1/O/Jgx6j890jN7Tth+wIIqawWuSBjd5PT3WyAqOJNtc5u0EhKlNWkoMorHuf4hWjf07XMJuS3F7PMsOGYaEwHXpc1JNN2U94TKyyPBfEpecvlo84FjKW6cpb+PINQcOVh3UeXCInk32/T4/rv4Ro6DCFlNKGobOozRSTt9a8vnQ2zOgc8omZiQzpvU8o8i92tgYtY9bIrOmIBgD+Lm/eJctjcm/qFOcNwVqj6s9Zth/pMuW+/tLxV/bsdK0XNCjGV+zAqM=
这是在Java中创建相同签名的方式:
Signature signature = Signature.getInstance("SHA256withRSA");
String privateKeyString = "-----BEGIN PRIVATE KEY-----" + paste the key from above +"-----END PRIVATE KEY-----";
privateKeyString = privateKeyString.replaceAll("\\n", "").replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
PKCS8EncodedKeySpec encodedPrivateKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(encodedPrivateKeySpec);
signature.initSign(privateKey);
signature.update("Hello, World".getBytes("UTF-8"));
byte[] signatureValue = signature.sign();
System.out.println(Base64.encodeBase64String(signatureValue));
最后是Java输出:
DYGB6T1QAKMjgyU++rh5Ee41ED0PtvOcU1Bz+WWUM82ONC42y0VSR3a2dyxsay0YL0U8UXHF7l5F51vxxayif1F8h3tsMsyV4PKVCgOYaJooTloOJT+R5oh7mywg1lCd/u9KYHYIc7x+MopDEfiCGWETMFHt7GUqRViV3PdTtYWzKy87GN74bnzQQMqmY+BH+IZhdpdt4IC7RFh9FCwLf7RBhfX3t/dhfFhdee34r/N8aQfxos9d23IDsvudtc0zlP2uluejDqUei4FQGdKuiLCBNQJGwK6yUDXIQ0N3sm19hmysqevsj/hmLPu1BC0Uj9ryxpdL8ViYE4Sj7fLZqUD/LyMbdO4a6WbLSlLbWnd4BYfzFND6919QcUGrcqb1W1Vz5Tso3mceNT8LUb0A69F6yVnbJX1zoQaUDbHnlStsKOopfVlmLgniOXOLlzoqTUzxwRUi8+Iv4EHTLZKP//CO4iAxXR+w/LihFuUTXtTZ+Fcxj1eRbeifyqmOqsGCvPHxjqvr5FvmadusyvgkM18UQvN8f/ohReokQqlr7iW0uUPtefSuffYgNxBjm1bE6kl+/xYE21A/annfdi3dDIeif9+/NM95NQOOD3/YEFaw+H2sbBnskdMmpu5P3QKms4ppJTkZjSPn+Meg5ZAW68Pu74WNFed6qs9SjvW0nQU=
我的问题:为什么两个签名不同?我尝试分别使用OpenSSL和Java进行验证,并且在两种情况下都成功。但是,当然,验证Java中的OpenSSL签名失败了。
答案 0 :(得分:1)
如果我添加\ n:
signature.update("Hello, World\n".getBytes("UTF-8"));
我明白了:
huMEGAIfj+RQ4XHLVYEkGlgbcz5Wv62QBWk6MsxbiDBIX1ZgjhjKY3zr8livAs3x4QjfOmxqs+hOGDo9einXlNWWinslD9q+0ScoJPauK2UuIbAzLsOFoUGJBlFvCALClDlP+oIkT8qLzVPL2gONln3G68p/yZdKIQldaVMDA3mQEBJgHLKxShW1iveqhXF9P6SijE6PstTZ21KEOgMKXVjqbA8EdYucWZv1IwllFXfWJuk36MrgcfGTDesjK95LgCaZ0MjQqsiQQFA3tmZSntE8oRs7UK2fBkx/RCoFIHut4szdaUUvvxLNXVq5hgEjxysvw8Pls86iC/kfu84fJL6Xc3z9vcw4j4H2u5iJS+XGCcW4Jk+2UNprkyLtTu3uWvz21ruIZTlPhDNXdMexr9DNf7WyIsIZ3zeM1/O/Jgx6j890jN7Tth+wIIqawWuSBjd5PT3WyAqOJNtc5u0EhKlNWkoMorHuf4hWjf07XMJuS3F7PMsOGYaEwHXpc1JNN2U94TKyyPBfEpecvlo84FjKW6cpb+PINQcOVh3UeXCInk32/T4/rv4Ro6DCFlNKGobOozRSTt9a8vnQ2zOgc8omZiQzpvU8o8i92tgYtY9bIrOmIBgD+Lm/eJctjcm/qFOcNwVqj6s9Zth/pMuW+/tLxV/bsdK0XNCjGV+zAqM=