隨筆-17  評(píng)論-6  文章-1  trackbacks-0

          keytool -genkey -dname "CN=demo, OU=softDept, O=company, L=puddong,S=shanghai, C=cn" -alias demo -keyalg RSA -keysize 1024 -keystore demoKeystore -validity 3650 -storepass storePwd -keypass demoPwd
          生成保存公鑰和私鑰的密鑰倉(cāng)庫(kù),保存在demoKeystore文件中。這里storepass 和 keypass 不要有java 正則表達(dá)式中的特殊字符,否則程序里要轉(zhuǎn)義麻煩。

          keytool -export -alias demo -keystore demoKeystore -rfc -file demo.cer //從密鑰倉(cāng)庫(kù)中導(dǎo)出保存公鑰的證書(shū)
          輸入keypass 即demoPwd 


            try{     
             //密鑰倉(cāng)庫(kù)
             KeyStore ks = KeyStore.getInstance("JKS");
          //讀取密鑰倉(cāng)庫(kù)
             FileInputStream ksfis = new FileInputStream("demoKeystore");
             BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
             char[] storePwd = "storePwd".toCharArray();
             ks.load(ksbufin, storePwd);
             ksbufin.close();
             char[] keyPwd = "demoPwd".toCharArray();
          //從密鑰倉(cāng)庫(kù)得到私鑰
             PrivateKey priK = (PrivateKey) ks.getKey("demo", keyPwd);  
          //生成cipher
             Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding",new org.bouncycastle.jce.provider.BouncyCastleProvider());
          //用私鑰初始化cipher
             cipher.init(Cipher.ENCRYPT_MODE, priK);
             byte[] plain = "This is plain text".getBytes("UTF-8");
             
             //因?yàn)橛玫?024位rsa算法,一次只能加密1024/8-11字節(jié)數(shù)據(jù),分開(kāi)加密
             byte[] code = new byte[(((plain.length-1)/117+1))*128];  
                      int ixplain = 0;
                      int ixcode = 0;
                      while((plain.length - ixplain) > 117) {//每117字節(jié)做一次加密
                          ixcode += cipher.doFinal(plain, ixplain, 117, code, ixcode);
                          ixplain += 117;
                      }
                      cipher.doFinal(plain, ixplain, plain.length - ixplain, code, ixcode);
                      //加密后的code
                      System.out.println(Arrays.toString(code));
                      //通常會(huì)用base64編碼
                     String base64 = encoder.encode(code);

             CertificateFactory certificatefactory = CertificateFactory
               .getInstance("X.509");
             //讀取證書(shū)
             FileInputStream fin = new FileInputStream("demo.cer");
             X509Certificate certificate = (X509Certificate) certificatefactory
               .generateCertificate(fin);
             fin.close();
             //得到公鑰
             PublicKey pubK = certificate.getPublicKey();
                   //初始化cipher
                      cipher.init(Cipher.DECRYPT_MODE, pubK);
                //base64解碼
                      code = decoder.decodeBuffer(base64);
                      System.out.println(Arrays.toString(code));
                      byte[] plain2 = new byte[code.length];
                      int ixplain2 = 0;
                      int ixcode2 = 0;
                      while((code.length - ixcode2) > 128) {//每128字節(jié)做一次解密
                          ixplain2 += cipher.doFinal(code, ixcode2, 128, plain2, ixplain2);
                          ixcode2 += 128;
                      }
                      ixplain2 += cipher.doFinal(code, ixcode2, code.length - ixcode2, plain2, ixplain2);
                      String s2 = new String(plain2, 0, ixplain2, "UTF-8");
                      System.out.println(s2);
             
            }catch(Exception ex){
             ex.printStackTrace();
            }

          keytool使用方法可以參考jdk文檔
          Java keytool工具的作用及使用方法

          posted on 2006-03-02 14:32 小鐵匠 閱讀(3431) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 曲沃县| 易门县| 纳雍县| 福鼎市| 临清市| 城固县| 敦煌市| 黄浦区| 泾川县| 淮滨县| 高要市| 郑州市| 辰溪县| 黎川县| 祁连县| 睢宁县| 庆阳市| 通榆县| 台山市| 宽城| 浦城县| 鸡泽县| 怀宁县| 天祝| 吉林省| 龙江县| 中牟县| 渑池县| 永宁县| 广西| 景东| 台北县| 若羌县| 正安县| 阳新县| 秦皇岛市| 通渭县| 天全县| 克东县| 曲阳县| 周至县|