RSA非对称加密




import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;

/**
*
* @ClassName: CommonManufactureKeyRSA
* @Description: TODO(RSA加密解密以及公钥私钥生成)
* @author
* @date
*
*/
public class CommonManufactureKeyRSA {

public static final String KEY_ALGORITHM = "RSA";

// 公钥的model
String publicKeys = "844019afd33d77dbee444c1c209188c5199844fa97b6fbd1ff822e0322ac45332054963e3fadbade5fcfb0011d30afc8fd2fe057eae1f6ae7172c4a4d99ccffc571cf958b9677177272766fc78eacf8df1c4dc384e94d10718d992e9e20c37b2e687fd622a07c8ba560969cda6c457cf6b1202332fea3815713b57d996949595";
// 公钥的publicExper
int publicExper = 10001;

// 私钥
static String privateKeys = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIRAGa/TPXfb7kRMHCCRiMUZmET6l7b70f+CLgMirEUzIFSWPj+tut5fz7ABHTCvyP0v4Ffq4faucXLEpNmcz/xXHPlYuWdxdycnZvx46s+N8cTcOE6U0QcY2ZLp4gw3suaH/WIqB8i6VglpzabEV89rEgIzL+o4FXE7V9mWlJWVAgMBAAECgYBETCo30BqdbJn28WtRMnd0eoxViyiQminQDzWd7ukSb53RsSmCj0DDGK3UEi819oIQpwnvvWBmmZ4DDCSqMT6DKOhHja661WroL8P4pmICLtQLfxXVRRzzKCHC3+thQf4b1zFyiWb/S19uiS33H1eKqK6tHoiL2ayP5W5oCJP3wQJBAO+9IpEjD6YNo7ChWKf/+5ob2NgDRh0e2VA6x2Kh39AgfQRv9oIxxpi0uaKBh/6jEdN9gI2keYhNIcSsKDfuviUCQQCNOIS0QHdVvCy9I0M8046fgQmXHFGzm6Lq4qaS1D4vOcebP3gVj7RH7Z3sjaTY+UrLDrPgfC5P0uNSlq804UaxAkEA7sBKE4Z6SCHfOcGFXFIXTKOc8YtZIRkezMKs3HJfRZOFTYz/TyupH10kXk1nlVTrJNcg1m7NRcui7I2SxBk0LQJAAuKju3cRuRu2IkslJYoBAPtG8eJvPOcyJfBpjNhvSNG6ZU6fqnkcKtgNAPdH5fL59zoerLh8zb62TEqt27u6kQJBAOaTpeFXNbAf9lak6KyGH3EqRMOH47Scor4awRSDxlSXx8u2SkRfXFe4nh0njwU1XBbEMe88xEqvv0yTbqIegi0=";


/**
*
* @Title: publicEnrypy
* @Description: TODO(生成密钥对)
* @param @throws Exception 设定文件
* @return void 返回类型
* @throws
*/
public void publicEnrypy() throws Exception {

Cipher cipher = Cipher.getInstance("RSA");
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
// 生成钥匙对
keyPairGenerator.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 得到公钥
Key publicKey = keyPair.getPublic();

String publicKeys = encryptBASE64(publicKey.getEncoded());
// 得到私钥
Key privateKey = keyPair.getPrivate();
String privateKeys = encryptBASE64(privateKey.getEncoded());

//公钥为key类型页面需要的值为modulus:(密钥模数) 和 public exponent(10001)密钥
System.out.println("publicKey:"+publicKey);

//私钥以String类型保存到类中
System.out.println("privateKeys:"+privateKeys);
}

/**
* 解密方法
*
* @param map
* @param map
* (解密的字符串)
* @return
* @throws Exception
*/
public String RSAdecode(byte[] map) throws Exception {

Cipher cipher = Cipher.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] keyBytes = decryptBASE64(privateKeys);
// 取得私钥 ,使用PKCS#8标准作为密钥规范管理的编码格式
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateKey1 = keyFactory.generatePrivate(pkcs8KeySpec);
// 设置为解密模式,用私钥解密
cipher.init(Cipher.DECRYPT_MODE, privateKey1);

//进行解密
byte[] result = cipher.doFinal(map);

//将字节转为字符串
String results = new String(result);

//将String字符串转为StringBuffer
StringBuffer sb = new StringBuffer(results);

//反转
sb = sb.reverse();

//转为String
results = sb.toString();
//经base64转为byte[]
byte[] resu = decryptBASE64(results);

//得到加密前字符
results = new String(resu);

System.out.println(results);
return results;
}

/**
* 加密方法
*
* @param publicKey
* @param map
* @throws Exception
*/
public byte[] RSAencrypt(String publicKey, String map)
throws Exception {

byte[] keyBytes = decryptBASE64(publicKey);
// 取得公钥
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publics = keyFactory.generatePublic(x509KeySpec);
// 对数据加密
Cipher ciphers = Cipher.getInstance(keyFactory.getAlgorithm());
// 设置为加密模式
ciphers.init(Cipher.ENCRYPT_MODE, publics);
// 对数据进行加密
byte[] result = ciphers.doFinal((map).getBytes());
String s = encryptBASE64(result);
System.out.println(s);
return result;
}

/**
* BASE64解密
*
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptBASE64(String key) throws Exception {

//返回解密后的值
return (new BASE64Decoder()).decodeBuffer(key);
}

/**
* BASE64加密
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(byte[] key) throws Exception {

//返回加密后的值
return (new BASE64Encoder()).encodeBuffer(key);
}



}

你可能感兴趣的:(对称加密)