Java加密与解密的艺术~RSA实现
生活随笔
收集整理的這篇文章主要介紹了
Java加密与解密的艺术~RSA实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
RSA 實現(xiàn)
/*** 2008-6-11*/ package org.zlex.chapter08_2;import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map;import javax.crypto.Cipher;/*** RSA安全編碼組件* * @author 梁棟* @version 1.0*/ public abstract class RSACoder {/*** 非對稱加密密鑰算法*/public static final String KEY_ALGORITHM = "RSA";/*** 公鑰*/private static final String PUBLIC_KEY = "RSAPublicKey";/*** 私鑰*/private static final String PRIVATE_KEY = "RSAPrivateKey";/*** RSA密鑰長度 * 默認1024位,* 密鑰長度必須是64的倍數(shù), * 范圍在512至65536位之間。*/private static final int KEY_SIZE = 512;/*** 私鑰解密* * @param data* 待解密數(shù)據(jù)* @param key* 私鑰* @return byte[] 解密數(shù)據(jù)* @throws Exception*/public static byte[] decryptByPrivateKey(byte[] data, byte[] key)throws Exception {// 取得私鑰PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);// 生成私鑰PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);// 對數(shù)據(jù)解密Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());cipher.init(Cipher.DECRYPT_MODE, privateKey);return cipher.doFinal(data);}/*** 公鑰解密* * @param data* 待解密數(shù)據(jù)* @param key* 公鑰* @return byte[] 解密數(shù)據(jù)* @throws Exception*/public static byte[] decryptByPublicKey(byte[] data, byte[] key)throws Exception {// 取得公鑰X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);// 生成公鑰PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);// 對數(shù)據(jù)解密Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());cipher.init(Cipher.DECRYPT_MODE, publicKey);return cipher.doFinal(data);}/*** 公鑰加密* * @param data* 待加密數(shù)據(jù)* @param key* 公鑰* @return byte[] 加密數(shù)據(jù)* @throws Exception*/public static byte[] encryptByPublicKey(byte[] data, byte[] key)throws Exception {// 取得公鑰X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);// 對數(shù)據(jù)加密Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());cipher.init(Cipher.ENCRYPT_MODE, publicKey);return cipher.doFinal(data);}/*** 私鑰加密* * @param data* 待加密數(shù)據(jù)* @param key* 私鑰* @return byte[] 加密數(shù)據(jù)* @throws Exception*/public static byte[] encryptByPrivateKey(byte[] data, byte[] key)throws Exception {// 取得私鑰PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);// 生成私鑰PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);// 對數(shù)據(jù)加密Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());cipher.init(Cipher.ENCRYPT_MODE, privateKey);return cipher.doFinal(data);}/*** 取得私鑰* * @param keyMap* 密鑰Map* @return byte[] 私鑰* @throws Exception*/public static byte[] getPrivateKey(Map<String, Object> keyMap)throws Exception {Key key = (Key) keyMap.get(PRIVATE_KEY);return key.getEncoded();}/*** 取得公鑰* * @param keyMap* 密鑰Map* @return byte[] 公鑰* @throws Exception*/public static byte[] getPublicKey(Map<String, Object> keyMap)throws Exception {Key key = (Key) keyMap.get(PUBLIC_KEY);return key.getEncoded();}/*** 初始化密鑰* * @return Map 密鑰Map* @throws Exception*/public static Map<String, Object> initKey() throws Exception {// 實例化密鑰對生成器KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);// 初始化密鑰對生成器keyPairGen.initialize(KEY_SIZE);// 生成密鑰對KeyPair keyPair = keyPairGen.generateKeyPair();// 公鑰RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();// 私鑰RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();// 封裝密鑰Map<String, Object> keyMap = new HashMap<String, Object>(2);keyMap.put(PUBLIC_KEY, publicKey);keyMap.put(PRIVATE_KEY, privateKey);return keyMap;} }RSA 使用示例
/*** 2008-6-11*/ package org.zlex.chapter08_2;import static org.junit.Assert.*;import org.apache.commons.codec.binary.Base64; import org.junit.Before; import org.junit.Test;import java.util.Map;/*** RSA校驗* * @author 梁棟* @version 1.0*/ public class RSACoderTest {/*** 公鑰*/private byte[] publicKey;/*** 私鑰*/private byte[] privateKey;/*** 初始化密鑰* * @throws Exception*/@Beforepublic void initKey() throws Exception {// 初始化密鑰Map<String, Object> keyMap = RSACoder.initKey();publicKey = RSACoder.getPublicKey(keyMap);privateKey = RSACoder.getPrivateKey(keyMap);System.err.println("公鑰: \n" + Base64.encodeBase64String(publicKey));System.err.println("私鑰: \n" + Base64.encodeBase64String(privateKey));}/*** 校驗* * @throws Exception*/@Testpublic void test() throws Exception {System.err.println("\n---私鑰加密——公鑰解密---");String inputStr1 = "RSA加密算法";byte[] data1 = inputStr1.getBytes();System.err.println("原文:\n" + inputStr1);// 加密byte[] encodedData1 = RSACoder.encryptByPrivateKey(data1, privateKey);System.err.println("加密后:\n" + Base64.encodeBase64String(encodedData1));// 解密byte[] decodedData1 = RSACoder.decryptByPublicKey(encodedData1,publicKey);String outputStr1 = new String(decodedData1);System.err.println("解密后:\n" + outputStr1);// 校驗assertEquals(inputStr1, outputStr1);System.err.println("\n---公鑰加密——私鑰解密---");String inputStr2 = "RSA Encypt Algorithm";byte[] data2 = inputStr2.getBytes();System.err.println("原文:\n" + inputStr2);// 加密byte[] encodedData2 = RSACoder.encryptByPublicKey(data2, publicKey);System.err.println("加密后:\n" + Base64.encodeBase64String(encodedData2));// 解密byte[] decodedData2 = RSACoder.decryptByPrivateKey(encodedData2,privateKey);String outputStr2 = new String(decodedData2);System.err.println("解密后: " + outputStr2);// 校驗assertEquals(inputStr2, outputStr2);}}總結
以上是生活随笔為你收集整理的Java加密与解密的艺术~RSA实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java调用存储过程同时获取[返回参数]
- 下一篇: Java加密与解密的艺术~Provide