非对称加密解密算法RSA的小实例,

package Encoder.notSymmetry;


import java.security.InvalidKeyException;
import java.security.Key;
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.spec.InvalidKeySpecException;
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;


/**
 * 非对称加密算法,RSA
 * @author CUICHUNCHI
 *
 */
public class RSAUtil {



/**
* 生成钥匙对
* @throws NoSuchAlgorithmException 
*/
public static KeyPair getKeyPair() throws NoSuchAlgorithmException{
//使用KeyPairGenerator生成器来生成keyPair对象
KeyPairGenerator pairGenerator = KeyPairGenerator.getInstance("RSA");
//初始位数 位数要大于等于512。越长,加密解密越慢,越安全
pairGenerator.initialize(512);
//生成钥匙对,包括公钥和私钥
KeyPair genKeyPair = pairGenerator.generateKeyPair();
return genKeyPair;
}
/**
* 返回公钥
* @throws InvalidKeySpecException 
* @throws NoSuchAlgorithmException 
*/
public static PublicKey getPubKey(KeyPair keyPair) throws InvalidKeySpecException, NoSuchAlgorithmException{
PublicKey public1 = keyPair.getPublic();//获取公钥
byte[] encoded = public1.getEncoded();
//公钥需要转换成X509EncodedKeySpec,然后通过keyFactory来生成public对象
/**
* * This class represents the ASN.1 encoding of a public key,
* encoded according to the ASN.1 type <code>SubjectPublicKeyInfo</code>.
* The <code>SubjectPublicKeyInfo</code> syntax is defined in the X.509
* standard as follows:
*/
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded);
KeyFactory factory = KeyFactory.getInstance("RSA");
PublicKey publicKey = factory.generatePublic(keySpec);
return publicKey;
}
/**
* 返回私钥
* @throws InvalidKeySpecException 
* @throws NoSuchAlgorithmException 
*/
public static PrivateKey getPrivateKey(KeyPair keyPair) throws InvalidKeySpecException, NoSuchAlgorithmException{
PrivateKey private1 = keyPair.getPrivate();
byte[] encoded = private1.getEncoded();
//私钥需要转换成PKCS8EncodedKeySpec,然后通过factory来生成私钥对象
/**
* * This class represents the ASN.1 encoding of a private key,
* encoded according to the ASN.1 type <code>PrivateKeyInfo</code>.
* The <code>PrivateKeyInfo</code> syntax is defined in the PKCS#8 standard
* as follows:
*/
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
KeyFactory factory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = factory.generatePrivate(keySpec);
return privateKey;
}

/**
* 使用公钥加密
* @return 
* @throws NoSuchPaddingException 
* @throws NoSuchAlgorithmException 
* @throws InvalidKeyException 
* @throws BadPaddingException 
* @throws IllegalBlockSizeException 
*/
public static  byte[] encryptPulicKey(String str,PublicKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(str.getBytes());
return doFinal;
}

/**
* 私钥解密
* @throws BadPaddingException 
* @throws IllegalBlockSizeException 
* @throws NoSuchPaddingException 
* @throws NoSuchAlgorithmException 
* @throws InvalidKeyException 
*/
public static String decodePrivate(byte [] aa,PrivateKey key) throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(aa);
return new String(doFinal);
}

/**
* 使用私钥加密
* @param args
* @return 
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws NoSuchPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] encryptPrivate(String str,PrivateKey key){
byte[] doFinal = null;
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
doFinal = cipher.doFinal(str.getBytes());
} catch (Exception e) {
// TODO: handle exception
}
return doFinal;
}

/**
* 使用公钥解密
* @param args
* @return 
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws NoSuchPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String decodePublicKey(byte[] b ,PublicKey key){
byte[] doFinal = null;
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
doFinal = cipher.doFinal(b);
} catch (Exception e) {
// TODO: handle exception
}
return new String(doFinal);
}

public static void main(String[] args) throws InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
String str = "崔春驰";
System.out.println("待RSA加密的字符串:"+str);
KeyPair keyPair = getKeyPair();//获取一对钥匙,包括公钥和私钥
PublicKey pubKey = getPubKey(keyPair);//获取公钥
//公钥加密
byte[] encryptPulicKey = encryptPulicKey(str, pubKey);
System.out.println("公钥加密后的:"+encryptPulicKey);
//获取私钥
PrivateKey privateKey = getPrivateKey(keyPair);
//私钥解密
String decodePrivate = decodePrivate(encryptPulicKey, privateKey);
System.out.println("私钥解密后:"+decodePrivate);

/***************使用公钥解密    私钥加密**********************/
String str1 = "十分放松放松";
KeyPair keyPair2 = getKeyPair();
System.out.println("待加密的文字:"+str1);
PrivateKey privateKey2 = getPrivateKey(keyPair2);
byte[] encryptPrivate = encryptPrivate(str1, privateKey2);
System.out.println("私钥加密后的:"+encryptPrivate);
PublicKey pubKey2 = getPubKey(keyPair2);
String decodePublicKey = decodePublicKey(encryptPrivate, pubKey2);
System.out.println("公钥解密后:"+decodePublicKey);

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值