import java.security.InvalidKeyException; 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.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import org.apache.commons.codec.binary.Base64; public class test { public static final String CIPHER_ALGORITHM = "RSA/ECB/PKCS1Padding"; public static final String PLAIN_TEXT = "张三丰,您的2016年10月份工资金额为:8000元。"; public static final String KEY_ALGORITHM = "RSA"; /** RSA密钥长度必须是64的倍数,在512~65536之间。默认是1024 */ public static final int KEY_SIZE = 2048; public static final String PUBLIC_KEY = "publicKey"; public static final String PRIVATE_KEY = "privateKey"; public static void main(String[] args) throws Exception { createKeyPairs(); } public static void createKeyPairs() throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyPairGenerator.initialize(KEY_SIZE); KeyPair keyPair = keyPairGenerator.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); //字符串化 byte[] pk = publicKey.getEncoded(); byte[] privk = privateKey.getEncoded(); String strpk = new String(Base64.encodeBase64(pk)); String strprivk = new String(Base64.encodeBase64(privk)); System.out.println("公钥:" + strpk); System.out.println("私钥:" + strprivk); //从字符串还原到对象 X509EncodedKeySpec pubX509 = new X509EncodedKeySpec(Base64.decodeBase64(strpk.getBytes())); PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(strprivk.getBytes())); KeyFactory keyf = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey pubkey2 = keyf.generatePublic(pubX509); PrivateKey privkey2 = keyf.generatePrivate(priPKCS8); //公钥加密 byte[] encodedText = RSAEncode(pubkey2, PLAIN_TEXT.getBytes()); System.out.println("加密后的密文: " + Base64.encodeBase64String(encodedText)); //私钥解密 System.out.println("解密后的明文: " + RSADecode(privkey2, encodedText)); } public static byte[] RSAEncode(PublicKey key, byte[] plainText) { try { Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(plainText); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { e.printStackTrace(); } return null; } public static String RSADecode(PrivateKey key, byte[] encodedText) { try { Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); return new String(cipher.doFinal(encodedText)); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { e.printStackTrace(); } return null; } }
时间: 2024-10-28 21:35:45