有两种基本的加解密算法类型:
1)对称加密 :密钥只有一个,加密解密为同一个密码,且加解密速度快,典型的对称加密算法有DES、AES等;
2)非对称加密 :密钥成对出现(且根据公钥无法推知私钥,根据私钥也无法推知公钥),加密解密使用不同密钥(公钥加密需要私钥解密,私钥加密需要公钥解密),相对对称加密速度较慢,典型的非对称加密算法有RSA、DSA等。
https的通信过程:
https通信的优点:
1)客户端产生的密钥只有客户端和服务器端能得到;
2)加密的数据只有客户端和服务器端才能得到明文;
3)客户端到服务端的通信是安全的。
2.在项目中实现
1)tomcat的配置
放开原先注释的一段代码:
Xml代码
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="c:/tomcat.keystore" keystorePass="tomcat" />
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="c:/tomcat.keystore" keystorePass="tomcat" />2)key的生成
键入命令并依次填入相应信息,
C代码
C:\>keytool -genkey -alias tomcat -keyalg RSA -keysize 1024 -validity 365 -keystore tomcat.keystore
Enter keystore password: tomcat
What is your first and last name?
[Unknown]: vincent
What is the name of your organizational unit?
[Unknown]: beijing
What is the name of your organization?
[Unknown]: peking
What is the name of your City or Locality?
[Unknown]: shanghai
What is the name of your State or Province?
[Unknown]: shanghai
What is the two-letter country code for this unit?
[Unknown]: sh
Is CN=vincent, OU=beijing, O=peking, L=shanghai, ST=shanghai, C=sh correct? (type "yes" or "no")
[no]: yes
Enter key password for <tomcat>:
(RETURN if same as keystore password):
C:\>keytool -genkey -alias tomcat -keyalg RSA -keysize 1024 -validity 365 -keystore tomcat.keystore
Enter keystore password: tomcat
What is your first and last name?
[Unknown]: vincent
What is the name of your organizational unit?
[Unknown]: beijing
What is the name of your organization?
[Unknown]: peking
What is the name of your City or Locality?
[Unknown]: shanghai
What is the name of your State or Province?
[Unknown]: shanghai
What is the two-letter country code for this unit?
[Unknown]: sh
Is CN=vincent, OU=beijing, O=peking, L=shanghai, ST=shanghai, C=sh correct? (type "yes" or "no")
[no]: yes
Enter key password for <tomcat>:
(RETURN if same as keystore password):
3)使用中发现的问题
4)此是单向认证,关于双向认证的知识还未补全。
TOMCAT @ SSL 写道
Tomcat6.0配置SSL
一、为了节约时间,我这里就只根据我的配置过程进行描述,读者根据各自情况自己分析。
1、在命令行中进入%CATALINA_HOME%/bin目录下执行以下命令:
(1)%CATALINA_HOME%/bin> keytool -genkey -alias tomcat -keyalg RSA -keypass changeit -storepass changeit -keystore server.keystore -validity 3600
此时会在%TOMCAT_HOME%/bin下生成server.keystore 文件。
注:参数 -validity 指证书的有效期(天),缺省有效期很短,只有90天。
(2)%CATALINA_HOME%/bin> keytool -export -trustcacerts -alias tomcat -file server.cer -keystore server.keystore -storepass changeit
这一步用于导出证书,此时会在%TOMCAT_HOME%/bin下生成server.cer 文件。
(3)%CATALINA_HOME%/bin> keytool -import -trustcacerts -alias tomcat -file server.cer -keystore %Java_HOME%/jre/lib/security/cacerts -storepass changeit
这一步是导入到证书信任库,大家可以观察%JAVA_HOME%/jre/lib/security/cacerts 这个文件,执行完此命令后,文件变大。
附:keytool其它命令(列出信任证书库中所有已有证书,删除库中某个证书):
keytool -list -v -keystore D:/sdks/jdk1.5.0_11/jre/lib/security/cacerts
keytool -delete -trustcacerts -alias tomcat -keystore D:/sdks/jdk1.5.0_11/jre/lib/security/cacerts -storepass changeit
2、修改%TOMCAT_HOME%\conf\server.xml
找到这段代码: Java代码
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
这段代码本来是注释掉的,把注释去掉,并且加上两个属性之后,如下:
Java代码
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="D:\tomcat6.0\bin\server.keystore"
keystorePass="changeit" />
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="D:\tomcat6.0\bin\server.keystore"
keystorePass="changeit" />
3、启动tomcat,访问 https://localhost:8443/,弹出一个安全警告的页面就OK了。
SSL认证详解
单向认证 SSL 协议的具体过程
①客户端的浏览器向服务器传送客户端 SSL 协议的版本号,加密算法的种类,产生的随机数,以及其他服务器和客户端之间通讯所需要的各种信息。
②服务器向客户端传送 SSL 协议的版本号,加密算法的种类,随机数以及其他相关信息,同时服务器还将向客户端传送自己的证书。