http://www.us-webhosting.com/compare.shtml?wmbhf
DevOps
http://www.us-webhosting.com/compare.shtml?wmbhf
使用openssl生成密钥对:
1 2 3 4 5 6 |
>openssl genrsa -out private.key >openssl rsa -in private.key -pubout -outform PEM -out public.key // 因为java里面不识别x509格式的私钥,所以必须转换为 pkcs8格式方可使用 // java异常描述为: java.security.spec.InvalidKeySpecException: Only RSAPrivate(Crt)KeySpec and PKCS8EncodedKeySpec supported for RSA private keys // 虽然RSAPrivate(Crt)KeySpec 也支持,但是目前还不知道怎么用 >openssl pkcs8 -topk8 -inform PEM -outform PEM -in private.key -out pkcs8_priv.pem -nocrypt |
java代码:
1. 注意: java 语言本身没有实现base64编码,而openssl生成的密钥对一般做base64编码,便于维护,所以这里引用了 org.apache.commons.codec.binary.Base64;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Arrays; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; //下载地址: http://commons.apache.org/codec/download_codec.cgi public static class MyRsa { /** * String to hold name of the encryption algorithm. */ public static final String ALGORITHM = "RSA"; /** * String to hold the name of the private key file. */ public static final String PRIVATE_KEY_FILE = "D:/rsa/pkcs8_priv.pem"; /** * String to hold name of the public key file. */ public static final String PUBLIC_KEY_FILE = "D:/rsa/public.key"; /** * Encrypt the plain text using public key. * * @param text * : original plain text * @param key * :The public key * @return Encrypted text * @throws java.lang.Exception */ public static byte[] encrypt(String text, PublicKey key) { byte[] cipherText = null; try { // get an RSA cipher object and print the provider final Cipher cipher = Cipher.getInstance(ALGORITHM); // encrypt the plain text using the public key cipher.init(Cipher.ENCRYPT_MODE, key); cipherText = cipher.doFinal(text.getBytes()); } catch (Exception e) { e.printStackTrace(); } return cipherText; } /** * Decrypt text using private key. * * @param text * :encrypted text * @param key * :The private key * @return plain text * @throws java.lang.Exception */ public static String decrypt(byte[] text, PrivateKey key) { byte[] dectyptedText = null; try { // get an RSA cipher object and print the provider final Cipher cipher = Cipher.getInstance(ALGORITHM); // decrypt the text using the private key cipher.init(Cipher.DECRYPT_MODE, key); dectyptedText = cipher.doFinal(text); } catch (Exception ex) { ex.printStackTrace(); } return new String(dectyptedText); } public static void test() { String s = "Hello world"; try { BufferedReader privateKey = new BufferedReader(new FileReader( PRIVATE_KEY_FILE)); BufferedReader publicKey = new BufferedReader(new FileReader( PUBLIC_KEY_FILE)); String strPrivateKey = ""; String strPublicKey = ""; String line = ""; while((line = privateKey.readLine()) != null){ strPrivateKey += line; } while((line = publicKey.readLine()) != null){ strPublicKey += line; } privateKey.close(); publicKey.close(); // 私钥需要使用pkcs8格式的,公钥使用x509格式的 String strPrivKey = strPrivateKey.replace("-----BEGIN PRIVATE KEY-----", "") .replace("-----END PRIVATE KEY-----", ""); String strPubKey = strPublicKey.replace("-----BEGIN PUBLIC KEY-----", "") .replace("-----END PUBLIC KEY-----", ""); //System.out.print(strPrivKey); //System.out.println(strPubKey); byte [] privKeyByte = Base64.decodeBase64(strPrivKey); byte [] pubKeyByte = Base64.decodeBase64(strPubKey); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyByte); //PKCS8EncodedKeySpec pubKeySpec = new PKCS8EncodedKeySpec(pubKeyByte); //X509EncodedKeySpec privKeySpec = new X509EncodedKeySpec(privKeyByte); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyByte); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey privKey = kf.generatePrivate(privKeySpec); PublicKey pubKey = kf.generatePublic(pubKeySpec); byte [] encryptByte = encrypt(s, pubKey); System.out.println(decrypt(encryptByte, privKey)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeySpecException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
稍后提供一个PHP加密Java解密的实现
参考资料:
http://stackoverflow.com/questions/11787571/how-to-read-pem-file-to-get-private-and-public-key
http://stackoverflow.com/questions/8647165/how-to-sign-a-generic-text-with-rsa-key-and-encode-with-base64-in-java
http://www.javamex.com/tutorials/cryptography/rsa_encryption.shtml
http://snowolf.iteye.com/blog/381767
密钥结构及格式: https://polarssl.org/kb/cryptography/asn1-key-structures-in-der-and-pem
ANT简明教程[转载]: http://www.cnblogs.com/philander/articles/1782254.html
ANT命令总结: http://www.blogjava.net/sutao/articles/133961.html
ANT基本使用指南: http://www.iteye.com/topic/78973
Eclipse中配置ANT: http://www.cnblogs.com/QQParadise/articles/1622154.html
相关资源:
http://www.cernet2.edu.cn/
http://test-ipv6.com/
http://support.microsoft.com/kb/99686
1. WebSocket rfc 中文翻译: http://blog.csdn.net/stoneson/article/details/8063802
2. http://o0211oo.iteye.com/blog/1671973
脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class CookieExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); // print out cookies Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { Cookie c = cookies[i]; String name = c.getName(); String value = c.getValue(); out.println(name + " = " + value); } // set a cookie String name = request.getParameter("cookieName"); if (name != null && name.length() > 0) { String value = request.getParameter("cookieValue"); Cookie c = new Cookie(name, value); response.addCookie(c); } } } |
如果cookie的name中含有 “;” 则会抛出异常,出现500错误,这个是没有问题的,但是,如果value中含有 “;” 会怎么处理呢?在PHP中setcookie()函数会将value做url编码,如果是setrawcookie()则会报错误。而在JSP中却出现了如下现象:
1 2 3 4 5 |
Content-Length:779 Content-Type:text/html;charset=ISO-8859-1 Date:Thu, 17 Jan 2013 15:01:41 GMT Server:Apache-Coyote/1.1 Set-Cookie:m="M;n"; Version=1 |
浏览器会因为给value添加了双引号而将cookie m的只是为 “M;n”吗? 不会的:
java线程管理是JVM的一部分,虽然大部分JVM直接映射java线程到底层系统线程,但是还是java的线程管理决定谁有机会运行
1 |
The thread scheduler is the part of the JVM (although most JVMs map Java threads directly to native threads on the underlying OS) that decides which thread should run at any given moment, and also takes threads out of the run state. Assuming a single processor machine, only one thread can actually run at a time. Only one stack can ever be executing at one time. And it's the thread scheduler that decides which thread—of all that are eligible—will actually run. |
1. Java线程学习和总结: http://blog.csdn.net/fantian830211/article/details/784597
1. 安装tomcat
http://tomcat.apache.org/download-70.cgi
2. 安装eclipse的tomcat插件
http://www.eclipsetotale.com/tomcatPlugin.html
3. Servlet总结
http://www.iteye.com/topic/766418 Servlet的一些基本概念
http://www.iteye.com/topic/80171 从Servlet的各个部件、功能、框架方面介绍的深入浅出,需要看看
http://www.iteye.com/topic/952866 主要介绍了Servlet的生命周期