接下来我们介绍非对称加密算法,RSA
RSA
这种算法1978年就出现了,它是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。算法的名字以发明者的名字命名:Ron Rivest, AdiShamir 和Leonard Adleman。
这种加密算法的特点主要是密钥的变化,上文我们看到DES只有一个密钥。相当于只有一把钥匙,如果这把钥匙丢了,数据也就不安全了。RSA同时有两把钥匙,公钥与私钥。
公钥公开,私钥保密。我们用公钥加密数据,用私钥解密数据,最常用作为数字签名。
通过java代码实现如下:
- import java.security.Key;
- import java.security.KeyFactory;
- import java.security.KeyPair;
- import java.security.KeyPairGenerator;
- 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;
-
-
-
-
-
-
-
-
- public class RSACoder extends Coder {
- public static final String ALGORITHM = "RSA";
- private static final String PUBLIC_KEY = "RSAPublicKey";
- private static final String PRIVATE_KEY = "RSAPrivateKey";
-
-
-
-
-
-
-
-
-
-
- public static byte[] decrypt(byte[] data, String key) throws Exception {
-
- byte[] keyBytes = decryptBASE64(key);
-
-
- PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
- KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
- Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
-
-
- Cipher cipher = Cipher.getInstance(ALGORITHM);
- cipher.init(Cipher.DECRYPT_MODE, privateKey);
-
- return cipher.doFinal(data);
- }
-
-
-
-
-
-
-
-
-
-
- public static byte[] encrypt(byte[] data, String key) throws Exception {
-
- byte[] keyBytes = decryptBASE64(key);
-
-
- X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
- KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
- Key publicKey = keyFactory.generatePublic(x509KeySpec);
-
-
- Cipher cipher = Cipher.getInstance(ALGORITHM);
- cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-
- return cipher.doFinal(data);
- }
-
-
-
-
-
-
-
-
- public static String getPrivateKey(Map<String, Object> keyMap)
- throws Exception {
- Key key = (Key) keyMap.get(PRIVATE_KEY);
-
- return encryptBASE64(key.getEncoded());
- }
-
-
-
-
-
-
-
-
- public static String getPublicKey(Map<String, Object> keyMap)
- throws Exception {
- Key key = (Key) keyMap.get(PUBLIC_KEY);
-
- return encryptBASE64(key.getEncoded());
- }
-
-
-
-
-
-
-
- public static Map<String, Object> initKey() throws Exception {
- KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM);
- keyPairGen.initialize(1024);
-
- 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;
- }
- }
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
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
* @since 1.0
*/
public class RSACoder extends Coder {
public static final String ALGORITHM = "RSA";
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
/**
* 解密<br>
* 用私钥解密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] data, String key) throws Exception {
// 对密钥解密
byte[] keyBytes = decryptBASE64(key);
// 取得私钥
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
// 对数据解密
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
/**
* 加密<br>
* 用公钥加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data, String key) throws Exception {
// 对公钥解密
byte[] keyBytes = decryptBASE64(key);
// 取得公钥
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
Key publicKey = keyFactory.generatePublic(x509KeySpec);
// 对数据加密
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
/**
* 取得私钥
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap)
throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return encryptBASE64(key.getEncoded());
}
/**
* 取得公钥
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap)
throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return encryptBASE64(key.getEncoded());
}
/**
* 初始化密钥
*
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGen.initialize(1024);
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;
}
}
再给出一个测试类:
- import static org.junit.Assert.*;
-
- import org.junit.Test;
-
- import org.zlex.commons.io.IOUtils;
-
- import java.util.Map;
-
-
-
-
-
-
-
- public class RSACoderTest {
- @Test
- public void test() throws Exception {
- String inputStr = "abc";
- byte[] data = inputStr.getBytes();
-
- Map<String, Object> keyMap = RSACoder.initKey();
-
- String publicKey = RSACoder.getPublicKey(keyMap);
- String privateKey = RSACoder.getPrivateKey(keyMap);
- System.err.println("publicKey: \n\r" + publicKey);
- System.err.println("privateKey: \n\r" + privateKey);
-
- byte[] encodedData = RSACoder.encrypt(data, publicKey);
-
- byte[] decodedData = RSACoder.decrypt(encodedData, privateKey);
- String outputStr = new String(decodedData);
- System.err.println("inputStr: " + inputStr + "\n\r" + "outputStr: "
- + outputStr);
- assertEquals(inputStr, outputStr);
- }
- }
import static org.junit.Assert.*;
import org.junit.Test;
import org.zlex.commons.io.IOUtils;
import java.util.Map;
/**
*
* @author 梁栋
* @version 1.0
* @since 1.0
*/
public class RSACoderTest {
@Test
public void test() throws Exception {
String inputStr = "abc";
byte[] data = inputStr.getBytes();
Map<String, Object> keyMap = RSACoder.initKey();
String publicKey = RSACoder.getPublicKey(keyMap);
String privateKey = RSACoder.getPrivateKey(keyMap);
System.err.println("publicKey: \n\r" + publicKey);
System.err.println("privateKey: \n\r" + privateKey);
byte[] encodedData = RSACoder.encrypt(data, publicKey);
byte[] decodedData = RSACoder.decrypt(encodedData, privateKey);
String outputStr = new String(decodedData);
System.err.println("inputStr: " + inputStr + "\n\r" + "outputStr: "
+ outputStr);
assertEquals(inputStr, outputStr);
}
}
控制台输出:
- publicKey:
-
- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCynMFQnRG1ZwfewDxaNDvFskU8zC1R6Uxq7hk+
- 0eaUHU/6Yi5kYWU1LJkkw23PvTq1JJ9XfunSWP2q2HlNCGQ+802MGXh5N3tikRIcA4XIehPwTTdN
- 5Gzvohh39N5lOL9X/TPhMIFtRzCaVWYpz/allDlB9SlJ+/pmYo37H5cIyQIDAQAB
-
- privateKey:
-
- MIICeQIBADANBgkqhkiG9w0BAQE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