Ubuntu 11.10 Solution Problem  • • •  2012 Plan       all posts in Archive

What is Digital Certification

之前做 Knowledge Share 的时候,整理了一下关于证书的东西,再精简一个文字版放这儿归档。

要说证书是什么似乎是太常见了,之前说  A Little HTTP 的时候提到的 HTTPS; 还有说起 SSH 也少不了证书。IE 里面的 "证书管理器" 估计你多少都曾经看到过了。可是要说证书是什么,明白肯定明白,不过似乎又有些说不清,道不明的样子。整理清楚什么是证书先需要先从 Key 说起。

Public Key & Private Key

用过 SSH 的人必然知道这两个东西了,他们的用处是什么呢?

  • Keys are used to encrypt information.
  • Encrypting information means "scrambling it up"
  • So that only a person with the appropriate key can make it readable again
  • Either one of the two keys can encrypt data, and the other key can decrypt it

比如 Bob 有这两把 Key,然后他将 Public Key 交给 Susan,那么Susan 就可以把发给 Bob 的消息用 Public Key 加密了。比如下面的信息:

"Hey Bob, how about lunch at Taco Bell. I hear they have free refills!"

用 public key 加密后,得到:

HNFmsEm6Un BejhhyCGKOK JUxhiygSBCEiC 0QYIh/Hn3xgiK lxx2lCFHDC/A

而 Bob 收到消息以后,能且只能用 Private Key  将信息还原。知道 encrypt / decrypt ,再看看数字签名是什么。

Digital Signature

  • 1. With his private key and the right software,
  • Bob can put digital signatures on documents and other data
  • 2. A digital signature is a "stamp" 
  • Bob places on the data which is unique to Bob, and is very difficult to forge
  • 3. In addition, the signature assures that any changes made to the data
  • that has been signed can not go undetected

所以要得到数字签名分 3 步走:

  1. 使用 SHA 或者 MD5 之类的算法,生成文件的数字摘要
  2. 将生成的数字摘要, 用 Private Key 做 Encrypt , 即可得到 Signature
  3. 然后将 Signature 附加到原消息上即可

证书的 decrypt 操作本身保证了文件来自正确的地方,因为只有对应的 Public Key 能解开其 Private Key;  decrypt 后得到的 Message Digest 保证了信息是原封原样的, 因为我们只需要再生成新的 Digest,就可以和之前的作对比。

但是,这里有一个严重的问题:如果有人恶意的,比如悄悄替换了 Susan 本机上 Bob's Public Key , 那么这个人就可以假冒 Bob 继续和 Susan 通信了。所以我们还需要证书。

Digital Certificate

数字证书是以 Public Key 为基础,加上一些附加信息组成的。比如 Bob's public key 再加上 Bob 的个人信息,和证书的有效期等组成证书的内容,对这个内容用 CA 的 Private Key 加密得到的就是 Digital Certificate 了。CA 的 public key 对所有的人都是开放的,而被封装的 Bob's Public Key 的有效性是由 CA 负责认证的。

使用时,我们只需要将 Digital Certificate 和之前的 Digital Signature 一起附加到原始信息里就可以了。和之前不同的是,需要从 Certificate 里面得到解开 Signature 的 Public Key.