获取字符串的公钥私钥,用字符串形式的公钥进行字符串加密,然后用字符串形式的私钥进行解密;
package com.security.rsa;
import java.io.IOException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class MRSA {
public static final String KEY_ALGORITHM = "RSA";
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
public static void main(String[] args) {
Map<String, Object> keyMap;
String source = "nihao 呵呵呵呵呵";
try {
keyMap = initKey();
String publicKey = getPublicKey(keyMap);
System.out.println("公钥:" + publicKey);
String privateKey = getPrivateKey(keyMap);
System.out.println("私钥:" + privateKey);
System.err.println("源字符:" + source);
byte[] encodered = encoder(getPubKey(publicKey), source.getBytes());
System.err.println("加密后:" + javax.xml.bind.DatatypeConverter.printBase64Binary(encodered));
byte[] decodered = decoder(getPrivateKey(privateKey), encodered);
System.err.println("解密后:" + new String(decodered));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成公钥 字符串
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
byte[] publicKey = key.getEncoded();
return encryptBASE64(key.getEncoded());
}
/**
* 生成私钥 字符串
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
byte[] privateKey = key.getEncoded();
return encryptBASE64(key.getEncoded());
}
public static byte[] decryptBASE64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
public static String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* 创建私钥公钥对
*
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_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;
}
/**
* 以一个字符串公钥去 实例化公钥对象
*
* @return
*/
public static PublicKey getPubKey(String pubKey) {
PublicKey publicKey = null;
try {
java.security.spec.X509EncodedKeySpec bobPubKeySpec = new java.security.spec.X509EncodedKeySpec(
new BASE64Decoder().decodeBuffer(pubKey));
// RSA对称加密算法
KeyFactory keyFactory;
keyFactory = java.security.KeyFactory.getInstance("RSA");
// 取公钥匙对象
publicKey = keyFactory.generatePublic(bobPubKeySpec);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return publicKey;
}
/**
* 以一个字符串私钥去实例化私钥
*
* @return
*/
public static PrivateKey getPrivateKey(String priKey) {
PrivateKey privateKey = null;
PKCS8EncodedKeySpec priPKCS8;
try {
priPKCS8 = new PKCS8EncodedKeySpec(new BASE64Decoder().decodeBuffer(priKey));
KeyFactory keyf = KeyFactory.getInstance("RSA");
privateKey = keyf.generatePrivate(priPKCS8);
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return privateKey;
}
/**
* 公钥加密一个byte数组
*
* @param publicKey
* 公钥
* @param src
* 加密的字符串
* @return
*/
public static byte[] encoder(PublicKey publicKey, byte[] src) {
try {
// Cipher类为加密和解密提供密码功能,通过getinstance实例化对象
Cipher cipher = Cipher.getInstance("RSA");
// 初始化加密
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(src);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 私钥解密
*
* @param privateKey
* 私钥
* @param src
* 公钥加密过的字符串
* @return
*/
public static byte[] decoder(PrivateKey privateKey, byte[] src) {
try {
// Cipher类为加密和解密提供密码功能,通过getinstance实例化对象
Cipher cipher = Cipher.getInstance("RSA");
// 初始化解密
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(src);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}