java RSA公私鑰與其base64編碼之間的轉換
轉自http://byx5185.iteye.com/blog/540624
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 javax.crypto.Cipher;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class RSAHelper {
/**
* 得到公鑰
* @param key 金鑰字串(經過base64編碼)
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* 得到私鑰
* @param key 金鑰字串(經過base64編碼)
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
/**
* 得到金鑰字串(經過base64編碼)
* @return
*/
public static String getKeyString(Key key) throws Exception {
byte[] keyBytes = key.getEncoded();
String s = (new BASE64Encoder()).encode(keyBytes);
return s;
}
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
//金鑰位數
keyPairGen.initialize(1024);
//金鑰對
KeyPair keyPair = keyPairGen.generateKeyPair();
// 公鑰
PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私鑰
PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
String publicKeyString = getKeyString(publicKey);
System.out.println("public:\n" + publicKeyString);
String privateKeyString = getKeyString(privateKey);
System.out.println("private:\n" + privateKeyString);
//加解密類
Cipher cipher = Cipher.getInstance("RSA");//Cipher.getInstance("RSA/ECB/PKCS1Padding");
//明文
byte[] plainText = "我們都很好!郵件:@sina.com".getBytes();
//加密
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] enBytes = cipher.doFinal(plainText);
//通過金鑰字串得到金鑰
publicKey = getPublicKey(publicKeyString);
privateKey = getPrivateKey(privateKeyString);
//解密
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[]deBytes = cipher.doFinal(enBytes);
publicKeyString = getKeyString(publicKey);
System.out.println("public:\n" +publicKeyString);
privateKeyString = getKeyString(privateKey);
System.out.println("private:\n" + privateKeyString);
String s = new String(deBytes);
System.out.println(s);
}
}
通過modulus \public exponent \private exponent生成 RSA Key
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
public class RsaKey {
public PublicKey getPublicKey(String modulus,String publicExponent) throws Exception {
BigInteger m = new BigInteger(modulus);
BigInteger e = new BigInteger(publicExponent);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m,e);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
public PrivateKey getPrivateKey(String modulus,String privateExponent) throws Exception {
BigInteger m = new BigInteger(modulus);
BigInteger e = new BigInteger(privateExponent);
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m,e);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
public static void main(String[] args) throws Exception {
String modulus = "10103166745709600780215616551837697832816413714471062522342538060943596036859967333870827790358555455232243383580565187280643159050869924436081447583051139";
String publicExponent = "65537";
String privateExponet = "367979294475011322800474185715497882523349856362702385535371444397399388741997039894583483410120364529325888461124714276674612930833020362278754665756193";
RsaKey key = new RsaKey();
PublicKey publicKey = key.getPublicKey(modulus, publicExponent);
PrivateKey privateKey = key.getPrivateKey(modulus, privateExponet);
//加解密類
Cipher cipher = Cipher.getInstance("RSA");
//明文
byte[] plainText = "我們都很好!郵件:@sina.com".getBytes();
//加密
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] enBytes = cipher.doFinal(plainText);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[]deBytes = cipher.doFinal(enBytes);
String s = new String(deBytes);
System.out.println(s);
}
}
相關文章
- java編碼之間轉換Java
- C#和JAVA的RSA金鑰、公鑰轉換C#Java
- Https中公私鑰加密演算法和其使用的RSA演算法分析HTTP加密演算法
- Java生成公私鑰對Java
- UTF-8編碼與GBK編碼之間的轉換
- 把Java生成的RSA公鑰、私鑰轉換成.NET使用的XML格式JavaXML
- Java之Base64編碼解析Java
- Java中Array與ArrayList之間的轉換Java
- java程式設計之:生成rsa金鑰Java程式設計
- java工具類之編碼轉換工具類Java
- Java之時間轉換Java
- java字元編碼轉換Java字元
- netty系列之:java中的base64編碼器NettyJava
- 將img圖片轉換為base64位編碼
- NSData與UIImage之間的轉換UI
- JAVA進階:VO(DTO)與PO(DAO)之間的轉換Java
- ANSI與UTF8之間的轉換!std::string與UTF8之間的轉換
- Oracle blob型別資料轉換成 base64編碼Oracle型別
- Java 8中的Base64編碼和解碼Java
- C# Rsa加密(私鑰加密、公鑰解密、金鑰格式轉換、支援超大長度分段加密)C#加密解密
- [轉] jQuery物件與DOM物件之間的轉換jQuery物件
- Java 資料型別之間的轉換Java資料型別
- Java 中 CLOB 和字串之間的轉換Java字串
- javascript將img圖片轉換為base64位編碼形式JavaScript
- Base64與BLOB 轉換函式函式
- mysql時間與字串之間相互轉換MySql字串
- Javascript 將圖片的絕對路徑轉換為base64編碼JavaScript
- Java 正確的做字串編碼轉換Java字串編碼
- spark: RDD與DataFrame之間的相互轉換Spark
- 字串與資料流之間的轉換字串
- Jquery 陣列與字串之間的轉換jQuery陣列字串
- Java實現BCD編碼與十進位制轉換Java
- Java Base64編碼使用介紹Java
- Rust中字串的base64編碼與解碼Rust字串
- 現代密碼-公鑰密碼RSA密碼
- 計算機編碼規則之:Base64編碼計算機
- java基本型別和物件之間的轉換Java型別物件
- java時間的轉換Java