String encryptSha256Data = this.eccrypt(JsonUtil.objectToSting(data));
        RSAPrivateKey rsaPrivateKey = this.getPrivateKey(wxPublicKey);
        String encryptRSAData = this.encrypt(rsaPrivateKey, encryptSha256Data.getBytes());

/** 
        * 加密 
        * @param info 
        * @return 
        * @throws NoSuchAlgorithmException 
        */  
       public static String eccrypt(String info) throws  
           NoSuchAlgorithmException  
       {  
           MessageDigest sha = MessageDigest.getInstance("SHA-256");  
           byte[] srcBytes = info.getBytes();  
           // 使用 srcBytes 更新摘要  
           sha.update(srcBytes);  
           // 完成哈希計算,得到 result  
           byte[] resultBytes = sha.digest();  
           Base64 base64 = new Base64();
           String s = base64.encodeToString(resultBytes).toString();
           return s;  
       }  

 

 public static RSAPrivateKey getPrivateKey(String key) throws Exception{
           try {  
               Base64 base64 = new Base64();
               DerParser parser = new DerParser(base64.decode(key));
    
               Asn1Object sequence = parser.read();
                 if (sequence.getType() != DerParser.SEQUENCE)
                   throw new IOException("Invalid DER: not a sequence"); //$NON-NLS-1$
    
                 // Parse inside the sequence
                 parser = sequence.getParser();
    
                 parser.read(); // Skip version
                 BigInteger modulus = parser.read().getInteger();
                 BigInteger publicExp = parser.read().getInteger();
                 BigInteger privateExp = parser.read().getInteger();
                 BigInteger prime1 = parser.read().getInteger();
                 BigInteger prime2 = parser.read().getInteger();
                 BigInteger exp1 = parser.read().getInteger();
                 BigInteger exp2 = parser.read().getInteger();
                 BigInteger crtCoef = parser.read().getInteger();
    
                 RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(
                     modulus, publicExp, privateExp, prime1, prime2,
                     exp1, exp2, crtCoef);
                 KeyFactory keyFactory = KeyFactory.getInstance("RSA");  
    
                 return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
            } catch (NoSuchAlgorithmException e) {  
                throw new Exception("无此算法");  
            } catch (InvalidKeySpecException e) {  
                throw new Exception("私钥非法");  
            } catch (NullPointerException e) {  
                throw new Exception("私钥数据为空");  
            }  
       }

 

/** 
         * 私钥加密过程 
         *  
         * @param privateKey 
         *            私钥 
         * @param plainTextData 
         *            明文数据 
         * @return 
         * @throws Exception 
         *             加密过程中的异常信息 
         */  
        public static String encrypt(RSAPrivateKey privateKey, byte[] plainTextData)  
                throws Exception {  
            if (privateKey == null) {  
                throw new Exception("加密私钥为空, 请设置");  
            }  
            Cipher cipher = null;  
            try {  
                // 使用默认RSA  
                cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");  
                cipher.init(Cipher.ENCRYPT_MODE, privateKey);  
                byte[] output = cipher.doFinal(plainTextData);  
                Base64 base64 = new Base64();
                return base64.encodeToString(output);  
            } catch (NoSuchAlgorithmException e) {  
                throw new Exception("无此加密算法");  
            } catch (NoSuchPaddingException e) {  
                e.printStackTrace();  
                return null;  
            } catch (InvalidKeyException e) {  
                throw new Exception("加密私钥非法,请检查");  
            } catch (IllegalBlockSizeException e) {  
                throw new Exception("明文长度非法");  
            } catch (BadPaddingException e) {  
                throw new Exception("明文数据已损坏");  
            }  
        }

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JoshS 的頭像
    JoshS

    JoshS的部落格

    JoshS 發表在 痞客邦 留言(0) 人氣()