1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| import javax.crypto.Cipher; import java.security.*; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map; import java.math.*; public class RSAEncrypt{ public static final String KEY_ALGORITHM="RSA"; private static final int KEY_SIZE=512; private static final String PUBLIC_KEY="RSAPublicKey"; private static final String PRIVATE_KEY="RSAPrivateKey"; public static Map<String,Object> initKey() 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(); Map<String,Object>keyMap=new HashMap<String,Object>(); keyMap.put(PUBLIC_KEY,publicKey); keyMap.put(PRIVATE_KEY,privateKey); return keyMap; }; public static byte[] encryptByPrivateKey(byte[] data,byte[] key) throws Exception{ PKCS8EncodedKeySpec pkcs8KeySpec=new PKCS8EncodedKeySpec(key); KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey privateKey=keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE,privateKey); return cipher.doFinal(data); }; public static byte[] encryptByPublicKey(byte[] data,byte[] key) throws Exception{ KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM); X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec(key); PublicKey pubKey=keyFactory.generatePublic(x509KeySpec); Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE,pubKey); return cipher.doFinal(data); }; public static byte[] decryptByPrivateKey(byte[] data,byte[] key) throws Exception{ PKCS8EncodedKeySpec pkcs8KeySpec=new PKCS8EncodedKeySpec(key); KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey privateKey=keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE,privateKey); return cipher.doFinal(data); }; public static byte[] decryptByPublicKey(byte[] data,byte[] key) throws Exception{ KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM); X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec(key); PublicKey pubKey=keyFactory.generatePublic(x509KeySpec); Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE,pubkey); return cipher.doFinal(data); }; public static byte[] getPrivateKey(Map<String,Object> keyMap){ Key key=(Key)keyMap.get(PRIVATE_KEY); return key.getEncoded(); }; public static byte[] getPublicKey(Map<String,Object> keyMap) throws Exception{ Key key=(Key)keyMap.get(PUBLIC_KEY); return key.getEncoded(); }; public static void main(String[] args) throws Excpetion{ Map<String,Object> keyMap=RSAEncrypt.initKey(); byte[] publicKey=RSAEncrypt.getPublicKey(keyMap); byte[] privateKey=RSAEncrypt.getPrivateKey(keyMap); System.out.println(new BigInteger(1,publicKey).toString()); System.out.println(new BigInteger(1,privateKey).toString()); String str="..."; byte[] encodData1=RSAEncrypt.encryptByPrivateKey(str.getBytes(),privateKey); System.out.println(new BigInteger(1,encodData1).toString()); byte[] decodeData1=RSAEncrypt.decryptByPublicKey(encodData1,publicKey); System.out.println(new String(decodeData1)); byte[] encodData2=RSAEncrypt.encryptByPublicKey(str.getBytes(),publicKey); System.out.println(new BigInteger(1,encodData2).toString()); byte[] decodeData2=RSAEncrypt.decryptByPrivateKey(encodData2,privateKey); System.out.println(new String(decodeData2)); }; };
|