Java加密技术(五) - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-23
ey() throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator .getInstance(ALGORITHM); keyPairGenerator.initialize(KEY_SIZE); KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 遮圭巷埒 DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic(); // 遮圭暴埒 DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate(); Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } /** * 兜兵晒厰圭畜埒 * * @param key * 遮圭巷埒 * @return * @throws Exception */ public static Map<String, Object> initKey(String key) throws Exception { // 盾裂遮圭巷埒 byte[] keyBytes = decryptBASE64(key); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); // 喇遮圭巷埒更秀厰圭畜埒 DHParameterSpec dhParamSpec = ((DHPublicKey) pubKey).getParams(); KeyPairGenerator keyPairGenerator = KeyPairGenerator .getInstance(keyFactory.getAlgorithm()); keyPairGenerator.initialize(dhParamSpec); KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 厰圭巷埒 DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic(); // 厰圭暴埒 DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate(); Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } /** * 紗畜<br> * * @param data * 棋紗畜方象 * @param publicKey * 遮圭巷埒 * @param privateKey * 厰圭暴埒 * @return * @throws Exception */ public static byte[] encrypt(byte[] data, String publicKey, String privateKey) throws Exception { // 伏撹云仇畜埒 SecretKey secretKey = getSecretKey(publicKey, privateKey); // 方象紗畜 Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, secretKey); return cipher.doFinal(data); } /** * 盾畜<br> * |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于Java加密技术(五) - 编程入门网的所有评论