當前位置:編程學習大全網 - 源碼下載 - Java中RSA的方式如何實現非對稱加密的示例

Java中RSA的方式如何實現非對稱加密的示例

代碼如下,需要依賴壹個jar包commons-codec-1.9.jar,用於Base64轉換,請自行下載。

import?org.apache.commons.codec.binary.Base64;

import?javax.crypto.BadPaddingException;

import?javax.crypto.Cipher;

import?javax.crypto.IllegalBlockSizeException;

import?java.io.ByteArrayOutputStream;

import?java.io.UnsupportedEncodingException;

import?java.security.*;

import?java.security.interfaces.RSAPrivateKey;

import?java.security.interfaces.RSAPublicKey;

import?java.security.spec.PKCS8EncodedKeySpec;

import?java.security.spec.X509EncodedKeySpec;

public?class?RSAUtils?{

//?加密方式

public?static?final?String?ALGORITHM?=?"RSA";

//?簽名算法

private?static?final?String?SIGNATURE_ALGORITHM?=?"SHA1WithRSA";

//?創建密鑰對初始長度

private?static?final?int?KEY_SIZE?=?512;

//?字符編碼格式

private?static?final?String?CHARSET?=?"UTF-8";

//?RSA最大加密明文大小

private?static?final?int?MAX_ENCRYPT_BLOCK?=?117;

//?RSA最大解密密文大小

private?static?final?int?MAX_DECRYPT_BLOCK?=?128;

private?KeyFactory?keyFactory;

public?RSAUtils()?throws?NoSuchAlgorithmException?{

keyFactory?=?KeyFactory.getInstance(ALGORITHM);

}

/**

*?私鑰加密

*

*?@param?content待加密字符串

*?@param?privateKey?私鑰

*?@return?加密後字符串(BASE64編碼)

*/

public?String?encryptByPrivateKey(String?content,?String?privateKey)?throws?Exception?{

String?result;

try?(ByteArrayOutputStream?out?=?new?ByteArrayOutputStream())?{

byte[]?keyBytes?=?new?Base64().decode(privateKey);

PKCS8EncodedKeySpec?pkcs8KeySpec?=?new?PKCS8EncodedKeySpec(keyBytes);

PrivateKey?pKey?=?keyFactory.generatePrivate(pkcs8KeySpec);

Cipher?cipher?=?Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.ENCRYPT_MODE,?pKey);

byte[]?data?=?content.getBytes(CHARSET);

write2Stream(cipher,?data,?out);

byte[]?resultBytes?=?out.toByteArray();

result?=?Base64.encodeBase64String(resultBytes);

}?catch?(Exception?e)?{

throw?new?Exception(e);

}

return?result;

}

/**

*?公鑰解密

*

*?@param?content已加密字符串(BASE64加密)

*?@param?publicKey?公鑰

*?@return

*/

public?String?decryptByPublicKey(String?content,?String?publicKey)?throws?Exception?{

String?result?=?"";

try?(ByteArrayOutputStream?out?=?new?ByteArrayOutputStream())?{

byte[]?keyBytes?=?new?Base64().decode(publicKey);

X509EncodedKeySpec?x509KeySpec?=?new?X509EncodedKeySpec(keyBytes);

PublicKey?pKey?=?keyFactory.generatePublic(x509KeySpec);

Cipher?cipher?=?Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE,?pKey);

byte[]?data?=?Base64.decodeBase64(content);

write2Stream(cipher,?data,?out);

byte[]?resultBytes?=?out.toByteArray();

result?=?new?String(resultBytes);

}?catch?(Exception?e)?{

throw?new?Exception(e);

}

return?result;

}

/**

*?公鑰加密

*

*?@param?content待加密字符串

*?@param?publicKey?公鑰

*?@return?加密後字符串(BASE64編碼)

*/

public?String?encryptByPublicKey(String?content,?String?publicKey)?throws?Exception?{

String?result?=?"";

try?(ByteArrayOutputStream?out?=?new?ByteArrayOutputStream())?{

byte[]?keyBytes?=?new?Base64().decode(publicKey);

X509EncodedKeySpec?x509KeySpec?=?new?X509EncodedKeySpec(keyBytes);

PublicKey?pKey?=?keyFactory.generatePublic(x509KeySpec);

Cipher?cipher?=?Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.ENCRYPT_MODE,?pKey);

byte[]?data?=?content.getBytes(CHARSET);

write2Stream(cipher,

data,?out);

byte[]?resultBytes?=?out.toByteArray();

result?=?Base64.encodeBase64String(resultBytes);

}?catch?(Exception?e)?{

throw?new?Exception(e);

}

return?result;

}

/**

*?私鑰解密

*

*?@param?content已加密字符串

*?@param?privateKey?私鑰

*?@return?解密後字符串

*/

public?String?decryptByPrivateKey(String?content,?String?privateKey)?throws?Exception?{

String?result?=?"";

try?(ByteArrayOutputStream?out?=?new?ByteArrayOutputStream())?{

byte[]?keyBytes?=?new?Base64().decode(privateKey);

PKCS8EncodedKeySpec?pkcs8KeySpec?=?new?PKCS8EncodedKeySpec(keyBytes);

PrivateKey?pKey?=?keyFactory.generatePrivate(pkcs8KeySpec);

Cipher?cipher?=?Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE,?pKey);

byte[]?data?=?Base64.decodeBase64(content);

write2Stream(cipher,?data,?out);

byte[]?resultBytes?=?out.toByteArray();

result?=?new?String(resultBytes);

}?catch?(Exception?e)?{

throw?new?Exception(e);

}

return?result;

}

private?static?void?write2Stream(Cipher?cipher,?byte[]?data,?ByteArrayOutputStream?out)?throws

BadPaddingException,?IllegalBlockSizeException?{

int?dataLen?=?data.length;

int?offSet?=?0;

byte[]?cache;

int?i?=?0;

//?對數據分段解密

while?(dataLen?-?offSet?>?0)?{

if?(dataLen?-?offSet?>?MAX_DECRYPT_BLOCK)?{

cache?=?cipher.doFinal(data,?offSet,?MAX_DECRYPT_BLOCK);

}?else?{

cache?=?cipher.doFinal(data,?offSet,?dataLen?-?offSet);

}

out.write(cache,?0,?cache.length);

i++;

offSet?=?i?*?MAX_DECRYPT_BLOCK;

}

}

/**

*?用私鑰對信息生成數字簽名

*

*?@param?data已加密數據

*?@param?privateKey?私鑰(BASE64編碼)

*?@return?sign

*/

public?String?sign(String?data,?String?privateKey)?throws?Exception?{

String?result?=?"";

try?{

byte[]?keyBytes?=?new?Base64().decode(privateKey);

PKCS8EncodedKeySpec?pkcs8KeySpec?=?new?PKCS8EncodedKeySpec(keyBytes);

PrivateKey?privateK?=?keyFactory.generatePrivate(pkcs8KeySpec);

Signature?signature?=?Signature.getInstance(SIGNATURE_ALGORITHM);

signature.initSign(privateK);

signature.update(parse2HexStr(data).getBytes(CHARSET));

result?=?new?Base64().encodeToString(signature.sign());

}?catch?(Exception?e)?{

throw?new?Exception(e);

}

return?result;

}

/**

*?校驗數字簽名

*

*?@param?data?已加密數據

*?@param?publicKey?公鑰(BASE64編碼)

*?@param?sign?數字簽名

*?@return

*?@throws?Exception

*/

public?boolean?verify(String?data,?String?publicKey,?String?sign)?throws?Exception?{

boolean?result;

try?{

byte[]?keyBytes?=?new?Base64().decode(publicKey);

X509EncodedKeySpec?keySpec?=?new?X509EncodedKeySpec(keyBytes);

PublicKey?publicK?=?keyFactory.generatePublic(keySpec);

Signature?signature?=?Signature.getInstance(SIGNATURE_ALGORITHM);

signature.initVerify(publicK);

signature.update(parse2HexStr(data).getBytes(CHARSET));

result?=?signature.verify(new?Base64().decode(sign));

}?catch?(Exception?e)?{

throw?new?Exception(e);

}

return?result;

}

/**

*?將二進制轉換成16進制

*

*?@param?data

*?@return

*/

public?static?String?parse2HexStr(String?data)?throws?Exception?{

String?result?=?"";

try?{

byte[]?buf?=?data.getBytes(CHARSET);

StringBuffer?sb?=?new?StringBuffer();

for?(int?i?=?0;?i?<?buf.length;?i++)?{

String?hex?=?Integer.toHexString(buf[i]?&?0xFF);

if?(hex.length()?==?1)?{

hex?=?'0'?+?hex;

}

sb.append(hex.toUpperCase());

}

result?=?sb.toString();

}?catch?(UnsupportedEncodingException?e)?{

throw?new?Exception(e);

}

return?result;

}

/**

*?生成公鑰與私鑰

*/

public?static?void?createKey()?throws?Exception?{

try?{

KeyPairGenerator?keyPairGenerator?=?KeyPairGenerator.getInstance(ALGORITHM);

keyPairGenerator.initialize(KEY_SIZE);

KeyPair?keyPair?=?keyPairGenerator.generateKeyPair();

RSAPublicKey?rsaPublicKey?=?(RSAPublicKey)?keyPair.getPublic();

RSAPrivateKey?rsaPrivateKey?=?(RSAPrivateKey)?keyPair.getPrivate();

String?publicKey?=?Base64.encodeBase64String(rsaPublicKey.getEncoded());

String?privateKey?=?Base64.encodeBase64String(rsaPrivateKey.getEncoded());

System.out.println("publicKey="?+?publicKey?+?"\nprivateKey="?+?privateKey);

}?catch?(NoSuchAlgorithmException?e)?{

throw?new?Exception(e);

}

}

public?static?void?main(String[]?args)?thro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/5zEjGwJbVdlvCfOiHwIDAQAB";

RSAUtils?rsaUtil?=?new?RSAUtils();

String?encryptByPublicKey?=?rsaUtil.encryptByPublicKey("妳好!",?PUBLIC_KEY);

System.out.println(encryptByPublicKey);

String?decryptByPrivateKey?=?rsaUtil.decryptByPrivateKey(encryptByPublicKey,?PRIVATE_KEY);

System.out.println(decryptByPrivateKey);

String?encryptByPrivateKey?=?rsaUtil.encryptByPrivateKey("妳好!",?PRIVATE_KEY);

System.out.println(encryptByPrivateKey);

String?decryptByPublicKey?=?rsaUtil.decryptByPublicKey(encryptByPrivateKey,?PUBLIC_KEY);

System.out.println(decryptByPublicKey);

String?sign?=?rsaUtil.sign("1234",?PRIVATE_KEY);

System.out.println("sign:"?+?sign);

System.out.println(rsaUtil.verify("1234",?PUBLIC_KEY,?sign));

}

}

  • 上一篇:向大俠求助壹段arm反匯編代碼的意思
  • 下一篇:貓起答鼓
  • copyright 2024編程學習大全網