日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

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

Java 進行 RSA 加解密的例子

來源:程序員人生   發布時間:2015-01-18 09:56:26 閱讀次數:2677次
加密是保證數據安全的手段之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/。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 日韩视频免费 | 成人在线 | 亚洲男人在线 | 国产精品视频播放 | 这里只有精品视频 | 成人91 | 国产91视频在线 | 欧洲成人午夜免费大片 | 黄色短视频在线播放 | 直接看av| 欧美999| 黄色在线免费 | 欧洲精品一区 | 亚洲福利在线观看 | 日韩福利一区二区 | 亚洲一区在线观看视频 | 欧美成人综合在线 | 亚洲福利小视频 | 国内外成人在线视频 | 国产精品69久久久久水密桃 | 99精品国产高清一区二区麻豆 | 91视频国产精品 | 日韩不卡在线观看 | 国产一区福利 | 久久久国产精品入口麻豆 | 成人毛片网站 | 国产精品久久久久久福利一牛影视 | 久久全国免费视频 | 好叼妞| av一二三区| 久久亚洲影视 | 久久精品夜夜夜夜夜久久 | 国产一区二区三区四区五区美女 | 精品国产31久久久久久 | 国产成人99久久亚洲综合精品 | 精品一区久久 | 国产高清无密码一区二区三区 | 超碰2021 | jizz亚洲女人高潮大叫 | 91久久精品视频 | 国产精品久久久久永久免费观看 |