签名、证书、非对称加密

问题来源

私钥加密的内容公钥可以解,而公钥又是公开的,那岂不是加密无效?

概述

1.非对称加密有公钥和私钥
2.公钥发布,私钥保存在自己这里
3.通过公钥加密的内容,只有自己可以用私钥解密查看
4.通过私钥加密的内容,只能用公钥才能解密

回到问题:私钥加密的内容公钥可以解,而公钥又是公开的,那岂不是加密无效?

所以,私钥加密,通常只用来认证这个加密的信息是私钥拥有者发布的,通常它会和明文的内容一起发布。用于验证消息的完整性。

而证书、签名就是这个原理,通过私钥签名,公钥认证。

RSA

http://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html

typedef struct {
    MPI n;      /* public modulus */
    MPI e;      /* public exponent */
    MPI d;      /* exponent */
    MPI p;      /* prime  p. */
    MPI q;      /* prime  q. */
    MPI u;      /* inverse of p mod q. */
} RSA_secret_key;

typedef struct {
    MPI n;      /* modulus */
    MPI e;      /* exponent */
} RSA_public_key;

openssl

https://www.openssl.org/

DESCRIPTION
       OpenSSL is a cryptography toolkit implementing the Secure Sockets Layer (SSL v2/v3) and Transport
       Layer Security (TLS v1) network protocols and related cryptography standards required by them.

       The openssl program is a command line tool for using the various cryptography functions of
       OpenSSL's crypto library from the shell.  It can be used for

        o  Creation and management of private keys, public keys and parameters
        o  Public key cryptographic operations
        o  Creation of X.509 certificates, CSRs and CRLs
        o  Calculation of Message Digests
        o  Encryption and Decryption with Ciphers
        o  SSL/TLS Client and Server Tests
        o  Handling of S/MIME signed or encrypted mail
        o  Time Stamp requests, generation and verification
生成RSA密钥对

Creation and management of private keys, public keys and parameters

genrsa:
Generation of RSA Private Key. Superseded by genpkey(1).
$ openssl genrsa  -help
Usage: genrsa [options]
Valid options are:
...
 -out outfile        Output the key to specified file
...

$ openssl genrsa -out private_key.key 
$ openssl rsa -in private_key.key -pubout -out public_key.key
$ openssl rsa -in private_key.key -text -noout

公钥加密私钥解密

Public key cryptographic operations

echo some secret > a.txt
openssl pkeyutl -encrypt -in a.txt -pubin -inkey public_key.key -out a.txt.enc
openssl pkeyutl -decrypt -in  a.txt.enc -inkey private_key.key
生成摘要(Digests)

Calculation of Message Digests

$ openssl dgst -sha512 a.c -out a.c.dgst
SHA512(a.c)= 970dc7b37290123a78d4af7cbaf11103f5ce30064bda6b27dc8cc8993f746f9056ae1564dcc8400dd40f7713cb2e02e97b3d949d576b5022dd065ffa2ae3d952


$ openssl dgst -sha512 -binary -out a.c.dgst a.c
$ hd a.c.dgst 
00000000  97 0d c7 b3 72 90 12 3a  78 d4 af 7c ba f1 11 03  |....r..:x..|....|
00000010  f5 ce 30 06 4b da 6b 27  dc 8c c8 99 3f 74 6f 90  |..0.K.k'....?to.|
00000020  56 ae 15 64 dc c8 40 0d  d4 0f 77 13 cb 2e 02 e9  |V..d..@...w.....|
00000030  7b 3d 94 9d 57 6b 50 22  dd 06 5f fa 2a e3 d9 52  |{=..WkP".._.*..R|
00000040


私钥签名公钥验证
# 测试效果
$ echo some secret > a.txt
$ openssl pkeyutl -sign -in a.txt -inkey private_key.key  -out a.txt.sig
$ openssl pkeyutl -verify -in a.txt  -sigfile a.txt.sig -pubin -inkey public_key.key
Signature Verified Successfully
$ openssl pkeyutl -verifyrecover -in a.txt.sig  -pubin -inkey public_key.key
some secret


echo some secret > a.txt
openssl pkeyutl -sign -in a.txt -inkey private_key.key  -out a.txt.sig
openssl pkeyutl -verify -in a.txt  -sigfile a.txt.sig -pubin -inkey public_key.key
# 公钥解密出私钥加密的内容
openssl pkeyutl -verifyrecover -in a.txt.sig  -pubin -inkey public_key.key


相关概念

SSL has been around for long enough you'd think that there would be agreed upon container formats. And you're right, there are. Too many standards as it happens. So this is what I know, and I'm sure others will chime in.

.csr - This is a Certificate Signing Request. Some applications can generate these for submission to certificate-authorities. The actual format is PKCS10 which is defined in RFC 2986. It includes some/all of the key details of the requested certificate such as subject, organization, state, whatnot, as well as the public key of the certificate to get signed. These get signed by the CA and a certificate is returned. The returned certificate is the public certificate (which includes the public key but not the private key), which itself can be in a couple of formats.
.pem - Defined in RFCs 1421 through 1424, this is a container format that may include just the public certificate (such as with Apache installs, and CA certificate files /etc/ssl/certs), or may include an entire certificate chain including public key, private key, and root certificates. Confusingly, it may also encode a CSR (e.g. as used here) as the PKCS10 format can be translated into PEM. The name is from Privacy Enhanced Mail (PEM), a failed method for secure email but the container format it used lives on, and is a base64 translation of the x509 ASN.1 keys.
.key - This is a PEM formatted file containing just the private-key of a specific certificate and is merely a conventional name and not a standardized one. In Apache installs, this frequently resides in /etc/ssl/private. The rights on these files are very important, and some programs will refuse to load these certificates if they are set wrong.
.pkcs12 .pfx .p12 - Originally defined by RSA in the Public-Key Cryptography Standards (abbreviated PKCS), the "12" variant was originally enhanced by Microsoft, and later submitted as RFC 7292. This is a passworded container format that contains both public and private certificate pairs. Unlike .pem files, this container is fully encrypted. Openssl can turn this into a .pem file with both public and private keys: openssl pkcs12 -in file-to-convert.p12 -out converted-file.pem -nodes
A few other formats that show up from time to time:

.der - A way to encode ASN.1 syntax in binary, a .pem file is just a Base64 encoded .der file. OpenSSL can convert these to .pem (openssl x509 -inform der -in to-convert.der -out converted.pem). Windows sees these as Certificate files. By default, Windows will export certificates as .DER formatted files with a different extension. Like...
.cert .cer .crt - A .pem (or rarely .der) formatted file with a different extension, one that is recognized by Windows Explorer as a certificate, which .pem is not.
.p7b .keystore - Defined in RFC 2315 as PKCS number 7, this is a format used by Windows for certificate interchange. Java understands these natively, and often uses .keystore as an extension instead. Unlike .pem style certificates, this format has a defined way to include certification-path certificates.
.crl - A certificate revocation list. Certificate Authorities produce these as a way to de-authorize certificates before expiration. You can sometimes download them from CA websites.
In summary, there are four different ways to present certificates and their components:

PEM - Governed by RFCs, its used preferentially by open-source software. It can have a variety of extensions (.pem, .key, .cer, .cert, more)
PKCS7 - An open standard used by Java and supported by Windows. Does not contain private key material.
PKCS12 - A Microsoft private standard that was later defined in an RFC that provides enhanced security versus the plain-text PEM format. This can contain private key material. Its used preferentially by Windows systems, and can be freely converted to PEM format through use of openssl.
DER - The parent format of PEM. It's useful to think of it as a binary version of the base64-encoded PEM file. Not routinely used very much outside of Windows.
I hope this helps.

https://serverfault.com/questions/9708/what-is-a-pem-file-and-how-does-it-differ-from-other-openssl-generated-key-file

PKCS
https://en.wikipedia.org/wiki/PKCS

PEM
https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343