packagecn.gdsoft;importjava.io.ByteArrayOutputStream;import java.security.*;importjava.security.interfaces.RSAPrivateKey;importjava.security.interfaces.RSAPublicKey;importjava.security.spec.PKCS8EncodedKeySpec;importjava.security.spec.X509EncodedKeySpec;import java.util.*;importjavax.crypto.Cipher;public classRSAUtils {/** */
/*** 加密算法RSA*/
public static final String KEY_ALGORITHM = "RSA";/** */
/*** 签名算法*/
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";/** */
/*** 获取公钥的key*/
private static final String PUBLIC_KEY = "RSAPublicKey";/** */
/*** 获取私钥的key*/
private static final String PRIVATE_KEY = "RSAPrivateKey";/** */
/*** RSA最大加密明文大小*/
private static final int MAX_ENCRYPT_BLOCK = 117;/** */
/*** RSA最大解密密文大小*/
private static final int MAX_DECRYPT_BLOCK = 128;/** */
/***
* 生成密钥对(公钥和私钥)
*
*
*@return*@throwsException*/
public static Map genKeyPair() throwsException {
KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
KeyPair keyPair=keyPairGen.generateKeyPair();
RSAPublicKey publicKey=(RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey=(RSAPrivateKey) keyPair.getPrivate();
Map keyMap = new HashMap(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);returnkeyMap;
}/** */
/***
* 用私钥对信息生成数字签名
*
*
*@paramdata 已加密数据
*@paramprivateKey 私钥(BASE64编码)
*
*@return*@throwsException*/
public static