JAVA 中的 RSA 初学

阅读更多
package com.xjh.util.rsa;

/**
 * 
 * RSAStudy1.java
 * 
 * 产生公钥和私钥字符串  (2013-04-26 16:05)
 * 
 * @author xiejiaohui
 *
 */
public class RSAStudy1 {
	
	public static void main(String[] args) {
		try {
			int keySize = 512;
			java.security.KeyPairGenerator pairgen = java.security.KeyPairGenerator.getInstance("RSA");
			java.security.SecureRandom random = new java.security.SecureRandom();
			pairgen.initialize(keySize, random);
			java.security.KeyPair keyPair = pairgen.generateKeyPair();
			// publicKey
			java.security.interfaces.RSAPublicKey publicKey = (java.security.interfaces.RSAPublicKey)keyPair.getPublic();		
			System.out.println("publicKey modulus:    " + publicKey.getModulus().toString(16));
			System.out.println("publicKey publicExponent:    " + publicKey.getPublicExponent().toString(16));
			// privateKey
			java.security.interfaces.RSAPrivateKey privateKey = (java.security.interfaces.RSAPrivateKey)keyPair.getPrivate();
			System.out.println("privateKey modulus:    " + privateKey.getModulus().toString(16));
			System.out.println("privateKey privateExponent:    " + privateKey.getPrivateExponent().toString(16));
		} catch (java.security.NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}
}

// this is the result:
// publicKey modulus:    dfd0a7a4b889617a527d7e3367b2b570113343253b94a9e7a96904b5bfc8590453172436290fc24e3439228831f0416a1cefc850ccf93938f23a8bf51bb14993
// publicKey publicExponent:    10001
// privateKey modulus:    dfd0a7a4b889617a527d7e3367b2b570113343253b94a9e7a96904b5bfc8590453172436290fc24e3439228831f0416a1cefc850ccf93938f23a8bf51bb14993
// privateKey privateExponent:    3d498bbe8571c9298fd1216fbe23a79f6a76591aa6f80c968d435eb0ce35747a4195e443c3ca69a809f0f08741a5e543a9b8aec179c07000359e012efff17321




package com.xjh.util.rsa;

/**
 * 
 * RSAStudy2.java
 * 
 * 用产生的公钥和私钥对字符串加密和解密(2013-04-26 16:06
 * 
 * @author xiejiaohui
 *
 */
public class RSAStudy2 {
	
	public static void main(String[] args) {
		try {
			String privateModulus = "dfd0a7a4b889617a527d7e3367b2b570113343253b94a9e7a96904b5bfc8590453172436290fc24e3439228831f0416a1cefc850ccf93938f23a8bf51bb14993";
			String privateExponent = "3d498bbe8571c9298fd1216fbe23a79f6a76591aa6f80c968d435eb0ce35747a4195e443c3ca69a809f0f08741a5e543a9b8aec179c07000359e012efff17321";
			String publicModulus = "dfd0a7a4b889617a527d7e3367b2b570113343253b94a9e7a96904b5bfc8590453172436290fc24e3439228831f0416a1cefc850ccf93938f23a8bf51bb14993";
			String publicExponent = "10001";
			java.math.BigInteger bigIntPrivateModulus = new java.math.BigInteger(privateModulus, 16);
			java.math.BigInteger bigIntPrivateExponent = new java.math.BigInteger(privateExponent, 16);
			java.security.Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
			java.security.KeyFactory keyFactory = java.security.KeyFactory.getInstance("RSA", provider);
			
			java.security.spec.RSAPrivateKeySpec rsaPrivateKeySpec = new java.security.spec.RSAPrivateKeySpec(bigIntPrivateModulus, bigIntPrivateExponent);
			java.security.PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec);
			
			java.math.BigInteger bigIntPublicModulus = new java.math.BigInteger(publicModulus, 16);
			java.math.BigInteger bigIntPublicEXponent = new java.math.BigInteger(publicExponent, 16);
			java.security.spec.RSAPublicKeySpec rsaPublicKeySpec = new java.security.spec.RSAPublicKeySpec(bigIntPublicModulus, bigIntPublicEXponent);
			java.security.PublicKey publicKey = keyFactory.generatePublic(rsaPublicKeySpec);
			String plaintext = "xiejiaohui 农商银行  123456";
			System.out.println("plaintext:    " + plaintext);
			byte[] inputBytes = plaintext.getBytes();
			byte[] encryptedBytes = RSAStudy2.encrypt(publicKey, inputBytes);
			String encrypted = new String(encryptedBytes);
			System.out.println("encrypted:    " + encrypted);
			byte[] decryptedBytes = RSAStudy2.decrypt(privateKey, encryptedBytes);
			String decrypted = new String(decryptedBytes);
			System.out.println("decrypted:    " + decrypted);
		} catch (java.security.NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (java.security.spec.InvalidKeySpecException e) {
			e.printStackTrace();
		}
			
	}
	
	public static byte[] encrypt(java.security.PublicKey publicKey, byte[] inputBytes) {
		try {
			javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("RSA");
			cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, publicKey);
			byte[] resultBytes = cipher.doFinal(inputBytes);
			return resultBytes;
		} catch (java.security.NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (java.security.InvalidKeyException e) {
			e.printStackTrace();
		} catch (javax.crypto.IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (javax.crypto.BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static byte[] decrypt(java.security.PrivateKey privateKey, byte[] inputBytes) {
		try {
			javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("RSA");
			cipher.init(javax.crypto.Cipher.DECRYPT_MODE, privateKey);
			byte[] resultBytes = cipher.doFinal(inputBytes);
			return resultBytes;
		} catch (java.security.NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (java.security.InvalidKeyException e) {
			e.printStackTrace();
		} catch (javax.crypto.IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (javax.crypto.BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}
}

// this is the result:  ( 每次加密产生的结果不一样的 )
// plaintext:    xiejiaohui 农商银行  123456
// encrypted:    iXg瞞?#??ら繛軞s栊DㄛU?影C@睹A?访媓y鎄?@楞咝壩鸕r
// decrypted:    xiejiaohui 农商银行  123456

你可能感兴趣的:(java,rsa)