package net.yun10000.zf.util; import Java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; public class RSAKeys { private RSAPublicKey publicKey; private RSAPrivateKey privateKey; public RSAPublicKey getPublicKey() { return publicKey; } public RSAPrivateKey getPrivateKey() { return privateKey; } public RSAKeys(RSAPublicKey publicKey, RSAPrivateKey privateKey) { this.publicKey = publicKey; this.privateKey = privateKey; } }
package net.yun10000.zf.util; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.Signature; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.StringUtils; /** * rsa工具類 * @author ydy * */ public class RSAUtil { private static final String SIGN_SHA1="SHA1WithRSA"; /** * 初始化rsa鑰匙 * * */ public static RSAKeys initkeys(){ try { //rsa工廠 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); //長度 keyPairGenerator.initialize(1024); KeyPair keyPair = keyPairGenerator.generateKeyPair(); //公鑰 RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); //私鑰 RSAPrivateKey rsaPrivateKey=(RSAPrivateKey) keyPair.getPrivate(); RSAKeys rsaKeys=new RSAKeys(rsaPublicKey, rsaPrivateKey); return rsaKeys; } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 獲取公鑰 * @param publicKeyStr * */ public static RSAPublicKey getPublicKey(String publicKeyStr){ try { KeyFactory keyFactory=KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(Base64.decodeBase64(publicKeyStr)); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(pkcs8EncodedKeySpec); return rsaPublicKey; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 獲取私鑰 * @param privateKeyStr * */ public static RSAPrivateKey getPrivateKey(String privateKeyStr){ try { KeyFactory keyFactory=KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyStr)); RSAPrivateKey privateKey=(RSAPrivateKey) keyFactory.generatePrivate(pkcs8EncodedKeySpec); return privateKey; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 私鑰解密 * @param contentBytes * @param privateKey * */ public static byte[] decrypt(byte[] contentBytes,RSAPrivateKey privateKey){ try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(contentBytes); }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 公鑰加密 * @param contentBytes * @param rsaPublicKey * */ public static byte[] encrypt(byte[] contentBytes,RSAPublicKey rsaPublicKey){ try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE,rsaPublicKey); return cipher.doFinal(contentBytes); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 數字簽名 * @param contentBytes 待簽名數據 * @param privateKey 私鑰 * */ public static byte[] signSHA1(byte[] contentBytes,RSAPrivateKey privateKey){ try { Signature signature=Signature.getInstance(SIGN_SHA1); signature.initSign(privateKey); signature.update(contentBytes); return signature.sign(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 驗證數字簽名 * @param contentBytes 數據 * @param signBytes 簽名數據 * @param publicKey 公鑰 * */ public boolean verifySHA1(byte[] contentBytes,byte[] signBytes,RSAPublicKey publicKey){ try { Signature signature=Signature.getInstance(SIGN_SHA1); signature.initVerify(publicKey); signature.update(contentBytes); return signature.verify(signBytes); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } public static void main(String[] args) { RSAKeys rsakeys = initkeys(); RSAPublicKey rsaPublicKey = rsakeys.getPublicKey(); String publicKeyStr=Base64.encodeBase64String(rsaPublicKey.getEncoded()); System.out.println("公鑰"); System.out.println(publicKeyStr); System.out.println("-------------------------------------------------------------------------------------------------"); RSAPrivateKey rsaPrivateKey = rsakeys.getPrivateKey(); String privateKeyStr = Base64.encodeBase64String(rsaPrivateKey.getEncoded()); System.out.println("私鑰"); System.out.println(privateKeyStr); String str="個人測試"; System.out.println("開始加密"); byte[] decodeBase64 = Base64.encodeBase64(StringUtils.getBytesUtf8(str)); //加密 byte[] enByte= encrypt(decodeBase64, rsaPublicKey); //解密 byte[] deByte=decrypt(enByte, rsaPrivateKey); byte[] strByte = Base64.decodeBase64(deByte); String strResult= StringUtils.newString(strByte, "utf-8"); System.out.println(strResult); } }
參考:java加密與解密的藝術java