多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > Java 進行 RSA 加解密的例子

Java 進行 RSA 加解密的例子

來源:程序員人生   發布時間:2015-01-18 09:56:26 閱讀次數:2673次
加密是保證數據安全的手段之1。加密是將純文本數據轉換難堪以理解的密文;解密是將密文轉換回純文本。
數據的加解密屬于密碼學的范疇。通常,加密和解密都需要使用1些秘密信息,這些秘密信息叫做密鑰,將純文本轉為密文或轉回的時候都要用到這些密鑰。
對稱加密指的是發送者和接收者共用同1個密鑰的加解密方法。
非對稱加密(又稱公鑰加密)指的是需要1個私有密鑰1個公然密鑰,兩個不同的密鑰的加解密體系。雖然不同,這個密鑰對的這兩個部份在算法上是有關聯的。1個密鑰將純文本加密,另外一個將密文解密。沒有1個密鑰能夠把加密和加密的功能全部自己完成。公鑰,或用于加密數據的密鑰,可以被自由分發。
RSA 是基于大整數分解的公鑰加密的算法之1。RSA 代表了 Ron Rivest、Adi Shamir 和 Leonard Adleman 3人,RSA 就是他們1起提出的。
以下示例就展現了如何在 Java 中使用 RSA 算法對信息進行加解密。
java.security.KeyPairGenerator 類的實例用于產生1個 RSA 算法的公鑰和私鑰對,以后將其保存到文件。
javax.crypto.Cipher 類的實例用于使用上面產生的密鑰對對信息進行加解密。
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import javax.crypto.Cipher; /** * @author JavaDigest * */ public class EncryptionUtil { /** * String to hold name of the encryption algorithm. */ public static final String ALGORITHM = "RSA"; /** * String to hold the name of the private key file. */ public static final String PRIVATE_KEY_FILE = "C:/keys/private.key"; /** * String to hold name of the public key file. */ public static final String PUBLIC_KEY_FILE = "C:/keys/public.key"; /** * Generate key which contains a pair of private and public key using 1024 * bytes. Store the set of keys in Prvate.key and Public.key files. * * @throws NoSuchAlgorithmException * @throws IOException * @throws FileNotFoundException */ public static void generateKey() { try { final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM); keyGen.initialize(1024); final KeyPair key = keyGen.generateKeyPair(); File privateKeyFile = new File(PRIVATE_KEY_FILE); File publicKeyFile = new File(PUBLIC_KEY_FILE); // Create files to store public and private key if (privateKeyFile.getParentFile() != null) { privateKeyFile.getParentFile().mkdirs(); } privateKeyFile.createNewFile(); if (publicKeyFile.getParentFile() != null) { publicKeyFile.getParentFile().mkdirs(); } publicKeyFile.createNewFile(); // Saving the Public key in a file ObjectOutputStream publicKeyOS = new ObjectOutputStream( new FileOutputStream(publicKeyFile)); publicKeyOS.writeObject(key.getPublic()); publicKeyOS.close(); // Saving the Private key in a file ObjectOutputStream privateKeyOS = new ObjectOutputStream( new FileOutputStream(privateKeyFile)); privateKeyOS.writeObject(key.getPrivate()); privateKeyOS.close(); } catch (Exception e) { e.printStackTrace(); } } /** * The method checks if the pair of public and private key has been generated. * * @return flag indicating if the pair of keys were generated. */ public static boolean areKeysPresent() { File privateKey = new File(PRIVATE_KEY_FILE); File publicKey = new File(PUBLIC_KEY_FILE); if (privateKey.exists() && publicKey.exists()) { return true; } return false; } /** * Encrypt the plain text using public key. * * @param text * : original plain text * @param key * :The public key * @return Encrypted text * @throws java.lang.Exception */ public static byte[] encrypt(String text, PublicKey key) { byte[] cipherText = null; try { // get an RSA cipher object and print the provider final Cipher cipher = Cipher.getInstance(ALGORITHM); // encrypt the plain text using the public key cipher.init(Cipher.ENCRYPT_MODE, key); cipherText = cipher.doFinal(text.getBytes()); } catch (Exception e) { e.printStackTrace(); } return cipherText; } /** * Decrypt text using private key. * * @param text * :encrypted text * @param key * :The private key * @return plain text * @throws java.lang.Exception */ public static String decrypt(byte[] text, PrivateKey key) { byte[] dectyptedText = null; try { // get an RSA cipher object and print the provider final Cipher cipher = Cipher.getInstance(ALGORITHM); // decrypt the text using the private key cipher.init(Cipher.DECRYPT_MODE, key); dectyptedText = cipher.doFinal(text); } catch (Exception ex) { ex.printStackTrace(); } return new String(dectyptedText); } /** * Test the EncryptionUtil */ public static void main(String[] args) { try { // Check if the pair of keys are present else generate those. if (!areKeysPresent()) { // Method generates a pair of keys using the RSA algorithm and stores it // in their respective files generateKey(); } final String originalText = "Text to be encrypted "; ObjectInputStream inputStream = null; // Encrypt the string using the public key inputStream = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE)); final PublicKey publicKey = (PublicKey) inputStream.readObject(); final byte[] cipherText = encrypt(originalText, publicKey); // Decrypt the cipher text using the private key. inputStream = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE)); final PrivateKey privateKey = (PrivateKey) inputStream.readObject(); final String plainText = decrypt(cipherText, privateKey); // Printing the Original, Encrypted and Decrypted Text System.out.println("Original: " + originalText); System.out.println("Encrypted: " +cipherText.toString()); System.out.println("Decrypted: " + plainText); } catch (Exception e) { e.printStackTrace(); } } }


原文鏈接:https://javadigest.wordpress.com/2012/08/26/rsa-encryption-example/。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲一区二区三区精品国产 | 看亚洲色图 | 国内精品久久影视 | 日本不卡视频一区二区三区 | 色污淫免费 | 中文字幕一区在线观看视频 | 精品久久九九 | 亚洲乱仑 | 久久精品国产精品亚洲精品 | 亚洲视频一区二区在线观看 | 伊人毛片| 看看黄色一级片 | 成人看片毛片免费播放器 | 欧美一欧美一级毛片 | 欧美另类videosbestsex日本 | 日本欧美一区二区三区不卡视频 | 国产成人免费永久播放视频平台 | 欧美最爽乱淫视频播放黑人 | 国产精品久久久久久久免费大片 | 91嫩草国产在线观看免费 | 日本中文字幕视频 | 国产高清在线精品免费不卡 | 色噜噜视频影院 | 精品久久久久久中文字幕欧美 | 偷拍区自拍区 | 日韩亚洲一区中文字幕 | 性做久久久久久久免费看 | 91精品国产高清91久久久久久 | 伊人啪啪网 | 成人a网 | 欧美第一福利 | 极品色影视 | 久久99精品久久久久久国产越南 | 欧美日韩高清观看一区二区 | 日本xxxx护士hd | 亚洲人成伊人成综合网久久久 | 国产二区三区毛片 | 亚洲免费三级 | 国产精品成人一区二区三区 | 久久性生大片免费观看性 | www.九色.com|