;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
publicclassRSAUtils{
/**
*RSA密钥长度,RSA算法的默认密钥长度是1024密钥长度必须是64的倍数,在512到65536位之间
*/
privatestaticfinalintKEY_SIZE=1024;
//RSA最大加密明文大小
privatestaticfinalintMAX_ENCRYPT_BLOCK=117;
privatestaticfinalintMAX_DECRYPT_BLOCK=KEY_SIZE/8;
privatestaticLoggerlogger=();
/**
*数字签名,密钥算法
*/
privatestaticfinalStringRSA_KEY_ALGORITHM="RSA";
/**
*生成密钥对
*/
privatestaticMapString,StringinitKey()throwsException{
KeyPairGeneratorkeygen=(RSA_KEY_ALGORITHM);
SecureRandomsecrand=newSecureRandom();
/**
*初始化随机产生器
*/
("initSeed".getBytes());
/**
*初始化密钥生成器
*/
(KEY_SIZE,secrand);
KeyPairkeys=();
byte[]pub_key=().getEncoded();
StringpublicKeyString=(pub_key);
byte[]pri_key=().getEncoded();
StringprivateKeyString=(pri_key);
MapString,StringkeyPairMap=newHashMap();
("publicKeyString",publicKeyString);
("privateKeyString",privateKeyString);
returnkeyPairMap;
}
/**
*密钥转成字符串
*
*@paramkey
*@return
*/
publicstaticStringencodeBase64String(byte[]key){
(key);
}
/**
*密钥转成byte[]
*
*@paramkey
*@return
*/
publicstaticbyte[]decodeBase64(Stringkey){
(key);
}
/**
*公钥加密
*
*@paramdata加密前的字符串
*@parampublicKey公钥
*@return加密后的字符串
*@throwsException
*/
publicstaticStringencryptByPubKey(Stringdata,StringpublicKeyStr)throwsException{
byte[]pubKey=(publicKeyStr);
X509EncodedKeySpecx509KeySpec=newX509EncodedKeySpec(pubKey);
KeyFactorykeyFactory=(RSA_KEY_ALGORITHM);
PublicKeypublicKey=(x509KeySpec);
Ciphercipher=(());
(_MODE,publicKey);
byte[]bytes=("UTF-8");
intinputLen=;
intoffLen=0;//偏移量
inti=0;
ByteArrayOutputStreambops=newByteArrayOutputStream();
while(inputLen-offLen0){
byte[]cache;
if(inputLen-offLenMAX_ENCRYPT_BLOCK){
cache=(bytes,offLen,MAX_ENCRYPT_BLOCK);
}else{
cache=(bytes,offLen,inputLen-offLen);
}
(cache);
i++;
offLen=MAX_ENCRYPT_BLOCK*i;
}
();
byte[]encryptedData=();
(encryptedData);
}
/**
*私钥加密
*
*@paramdata加密前的字符串
*@paramprivateKey私钥
*@return加密后的字符串
*@throwsException
*/
publicstaticStringencryptByPriKey(Stringdata,StringprivateKeyStr)throwsException{
byte[]priKey=(privateKeyStr);
PKCS8EncodedKeySpecpkcs8KeySpec=newPKCS8EncodedKeySpec(priKey);
KeyFactorykeyFactory=(RSA_KEY_ALGORITHM);
PrivateKeyprivateKey=(pkcs8KeySpec);
Ciphercipher=(());
(_MODE,privateKey);
byte[]bytes=("UTF-8");
intinputLen=;
intoffLen=0;//偏移量
inti=0;
ByteArrayOutputStreambops=newByteArrayOutputStream();
while(inputLen-offLen0){
byte[]cache;
if(inputLen-offLenMAX_ENCRYPT_BLOCK){
cache=(bytes,offLen,MAX_ENCRYPT_BLOCK);
}else{
cache=(bytes,offLen,inputLen-offLen);
}
(cache);
i++;
offLen=MAX_ENCRYPT_BLOCK*i;
}
();
byte[]encryptedData=();
(encryptedData);
}
/**
*公钥解密
*
*@paramdata解密前的字符串
*@parampublicKey公钥
*@return解密后的字符串
*@throwsException
*/
publicstaticStringdecryptByPubKey(Stringdata,StringpublicKeyStr)throwsException{
byte[]pubKey=(publicKeyStr);
X509EncodedKeySpecx509KeySpec=newX509EncodedKeySpec(pubKey);
KeyFactorykeyFactory=(RSA_KEY_ALGORITHM);
PublicKeypublicKey=(x509KeySpec);
Ciphercipher=(());
(_MODE,publicKey);
byte[]bytes=(data);
intinputLen=;
intoffLen=0;
inti=0;
ByteArrayOutputStreambyteArrayOutputStream=newByteArrayOutputStream();
while(inputLen-offLen0){
byte[]cache;
if(inputLen-offLenMAX_DECRYPT_BLOCK){
cache=(bytes,offLen,MAX_DECRYPT_BLOCK);
}else{
cache=(bytes,offLen,inputLen-offLen);
}
(cache);
i++;
offLen=MAX_DECRYPT_BLOCK*i;
}
();
byte[]byteArray=();
returnnewString(byteArray,"UTF-8");
}
/**
*私钥解密
*
*@paramdata解密前的字符串
*@paramprivateKey私钥
*@return解密后的字符串
*@throwsException
*/
publicstaticStringdecryptByPriKey(Stringdata,StringprivateKeyStr)throwsException{
byte[]priKey=(privateKeyStr);
PKCS8EncodedKeySpecpkcs8KeySpec=newPKCS8EncodedKeySpec(priKey);
KeyFactorykeyFactory=(RSA_KEY_ALGORITHM);
PrivateKeyprivateKey=(pkcs8KeySpec);
Ciphercipher=(());
(_MODE,privateKey);
byte[]bytes=(data);
intinputLen=;
intoffLen=0;
inti=0;
ByteArrayOutputStreambyteArrayOutputStream=newByteArrayOutputStream();
while(inputLen-offLen0){
byte[]cache;
if(inputLen-offLenMAX_DECRYPT_BLOCK){
cache=(bytes,offLen,MAX_DECRYPT_BLOCK);
}else{
cache=(bytes,offLen,inputLen-offLen);
}
(cache);
i++;
offLen=MAX_DECRYPT_BLOCK*i;
}
();
byte[]byteArray=();
returnnewString(byteArray,"UTF-8");
}
publicstaticvoidmain(String[]args){
try{
MapString,StringkeyMap=initKey();
StringpublicKeyString="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDSUmOXyQmYYSnZacp0btvAZCOvCNPtzixAp7eJmzmAG4mgy/VgrY/s1BDLh9qTNHIRWXepUtwMrf1kYul/A45qE/2oxIbeeq4238YDWQ7ModOVXR9ytEHsT0jpCFvoYfYXYZnnoWRrLIBylQeXzqxbLDxxBxGCs4AjoRKh5S7nNQIDAQAB";//("publicKeyString");
StringprivateKeyString="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANJSY5fJCZhhKdlpynRu28BkI68I0+3OLECnt4mbOYAbiaDL9WCtj+zUEMuH2pM0chFZd6lS3Ayt/WRi6X8DjmoT/ajEht56rjbfxgNZDsyh05VdH3K0QexPSOkIW+hh9hdhmeehZGssgHKVB5fOrFssPHEHEYKzgCOhEqHlLuc1AgMBAAECgYEAqTB9zWx7u4juEWd45ZEIVgw4aGXBllt0Xc6NZrTn3JZKcH+iNNNqJCm0GQaAXkqiODKwgBWXzttoK4kmLHa/6D7rXouWN8PGYXj7DHUNzyOe3IgmzYanowp/A8gu99mJQJzyhZGQ+Uo9dZXAgUDin6HAVLaxF3yWD8/yTKWN4UECQQD8Q72r7qdAfzdLMMSQl50VxRmbdhQYbo3D9FmwUw6W1gy2jhJyPXMi0JZKdKaqhxMZIT3zy4jYqw8/0zF2xc5/AkEA1W+n24Ef3ucbPgyiOu+XGwW0DNpJ9F8D3ZkEKPBgjOMojM7oqlehRwgy52hU+HaL4Toq9ghL1SwxBQPxSWCYSwJAGQUO9tKAvCDh9w8rL7wZ1GLsG0Mm0xWD8f92NcrHE6a/NAv7QGFf3gAaJ+BR92/WMRPe9SMmu3ab2JS1vzX3OQJAdN70/T8RYo8N3cYxNzBmf4d59ee5wzQb+8WD/57QX5UraR8LS+s8Bpc4uHnqvTq8kZG2YI5eZ9YQ6XwlLVbVTQJAKOSXNT+XEPWaol1YdWZDvr2m/ChbX2uwz52s8577Tey96O4Z6S/YA7V6Fr7hZEzkNF+K0LNUd79EOB6m2eQq5w==";//("privateKeyString");
("公钥:"+publicKeyString);
("私钥:"+privateKeyString);
//待加密数据
Stringdata="{}";
("加密前:"+data);
//公钥加密
Stringencrypt=(data,publicKeyString);
("加密后(公钥):"+encrypt);
//私钥解密
Stringdecrypt=(encrypt,privateKeyString);
("解密后(私钥):"+decrypt);
//私钥加密
StringencryptPriv=(data,privateKeyString);
("加密后(私钥):"+encryptPriv);
//公钥解密
StringdecryptPub=(encryptPriv,publicKeyString);
("解密后(公钥):"+decryptPub);
}catch(Exceptione){
();
}
}
}