RSA密钥的数据类型转换:由合法的string到PublicKey或PrivateKey
给定base64编码的RSA公钥和私钥,下面两段代码可以将string类型转换为PublicKey和PrivateKey类型,后面会给出完整的测试程序。至于如何将其他形式(如16进制编码string或byte之类的)转换为base64 string就很简单了,可以使用Base64那个库,自己下个jar包,里面有encode和decode之类的方法,hex转base64网上也可以搜到。
转换代码如下:
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; }
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; }
注意:不是随便敲一串字母就可以作为密钥string使用的,需要用严格的代码生成公钥串和私钥串。
完整的程序如下:
程序中包含密钥转换、加密和签名三个部分。
1 package pack1; 2 3 import java.security.Key; 4 import java.security.KeyFactory; 5 import java.security.KeyPair; 6 import java.security.KeyPairGenerator; 7 import java.security.PrivateKey; 8 import java.security.PublicKey; 9 import java.security.Signature; 10 import java.security.interfaces.RSAPrivateKey; 11 import java.security.interfaces.RSAPublicKey; 12 import java.security.spec.PKCS8EncodedKeySpec; 13 import java.security.spec.X509EncodedKeySpec; 14 15 import javax.crypto.Cipher; 16 17 import org.apache.commons.codec.binary.Base64; 18 19 import sun.misc.BASE64Decoder; 20 import sun.misc.BASE64Encoder; 21 22 23 public class RSAtest{ 24 public static final String KEY_ALGORITHM="RSA"; 25 public static final String SIGNATURE_ALGORITHM="MD5withRSA"; 26 private static final int KEY_SIZE=1024; 27 private static final String PUBLIC_KEY="RSAPublicKey"; 28 private static final String PRIVATE_KEY="RSAPrivateK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egPtPROHbhIvVUmNQTbuP10Yjg59+8="; 31 /** 32 * 使用getPublicKey得到公钥,返回类型为PublicKey 33 * @param base64 String to PublicKey 34 * @throws Exception 35 */ 36 public static PublicKey getPublicKey(String key) throws Exception { 37 byte[] keyBytes; 38 keyBytes = (new BASE64Decoder()).decodeBuffer(key); 39 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); 40 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 41 PublicKey publicKey = keyFactory.generatePublic(keySpec); 42 return publicKey; 43 } 44 /** 45 * 转换私钥 46 * @param base64 String to PrivateKey 47 * @throws Exception 48 */ 49 public static PrivateKey getPrivateKey(String key) throws Exception { 50 byte[] keyBytes; 51 keyBytes = (new BASE64Decoder()).decodeBuffer(key); 52 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); 53 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 54 PrivateKey privateKey = keyFactory.generatePrivate(keySpec); 55 return privateKey; 56 } 57 58 //***************************签名和验证******************************* 59 public static byte[] sign(byte[] data) throws Exception{ 60 PrivateKey priK = getPrivateKey(str_priK); 61 Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM); 62 sig.initSign(priK); 63 sig.update(data); 64 return sig.sign(); 65 } 66 67 public static boolean verify(byte[] data,byte[] sign) throws Exception{ 68 PublicKey pubK = getPublicKey(str_pubK); 69 Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM); 70 sig.initVerify(pubK); 71 sig.update(data); 72 return sig.verify(sign); 73 } 74 75 //************************加密解密************************** 76 public static byte[] encrypt(byte[] bt_plaintext)throws Exception{ 77 PublicKey publicKey = getPublicKey(str_pubK); 78 Cipher cipher = Cipher.getInstance("RSA"); 79 cipher.init(Cipher.ENCRYPT_MODE, publicKey); 80 byte[] bt_encrypted = cipher.doFinal(bt_plaintext); 81 return bt_encrypted; 82 } 83 84 public static byte[] decrypt(byte[] bt_encrypted)throws Exception{ 85 PrivateKey privateKey = getPrivateKey(str_priK); 86 Cipher cipher = Cipher.getInstance("RSA"); 87 cipher.init(Cipher.DECRYPT_MODE, privateKey); 88 byte[] bt_original = cipher.doFinal(bt_encrypted); 89 return bt_original; 90 } 91 //********************main函数:加密解密和签名验证********************* 92 public static void main(String[] args) throws Exception { 93 String str_plaintext = "这是一段用来测试密钥转换的明文"; 94 System.err.println("明文:"+str_plaintext); 95 byte[] bt_cipher = encrypt(str_plaintext.getBytes()); 96 System.out.println("加密后:"+Base64.encodeBase64String(bt_cipher)); 97 98 byte[] bt_original = decrypt(bt_cipher); 99 String str_original = new String(bt_original); 100 System.out.println("解密结果:"+str_original); 101 102 String str="被签名的内容"; 103 System.err.println("\n原文:"+str); 104 byte[] signature=sign(str.getBytes()); 105 System.out.println("产生签名:"+Base64.encodeBase64String(signature)); 106 boolean status=verify(str.getBytes(), signature); 107 System.out.println("验证情况:"+status); 108 } 109 110 }
曾经在百度知道上寻求过这个问题,一直都没能很好的解决。今日coding测试成功,来分享一下,技术有限,不足之处望指正。。。。。。by SHADOW.Delta
posted on 2014-11-28 14:34 KKatherine 阅读(31468) 评论(1) 编辑 收藏 举报