RSA密碼編譯演算法java簡單實現方法(必看)_java

來源:互聯網
上載者:User

簡單完整的代碼,通過這個代碼你將對RSA密碼編譯演算法在Java中的實現方法有一個初步的瞭解,這個類,你可以直接使用,水平高的,就自己修改完善下代碼。

package security;import java.security.*;import java.security.spec.*;import java.security.interfaces.*;import javax.crypto.spec.*;import javax.crypto.interfaces.*;import java.io.*;import java.math.*;public class RSADemo {public RSADemo() {}public static void generateKey() {try {KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");kpg.initialize(1024);KeyPair kp = kpg.genKeyPair();PublicKey pbkey = kp.getPublic();PrivateKey prkey = kp.getPrivate();// 儲存公開金鑰FileOutputStream f1 = new FileOutputStream("pubkey.dat");ObjectOutputStream b1 = new ObjectOutputStream(f1);b1.writeObject(pbkey);// 儲存私密金鑰FileOutputStream f2 = new FileOutputStream("privatekey.dat");ObjectOutputStream b2 = new ObjectOutputStream(f2);b2.writeObject(prkey);} catch (Exception e) {}}public static void encrypt() throws Exception {String s = "Hello World!";// 擷取公開金鑰及參數e,nFileInputStream f = new FileInputStream("pubkey.dat");ObjectInputStream b = new ObjectInputStream(f);RSAPublicKey pbk = (RSAPublicKey) b.readObject();BigInteger e = pbk.getPublicExponent();BigInteger n = pbk.getModulus();System.out.println("e= " + e);System.out.println("n= " + n);// 擷取明文mbyte ptext[] = s.getBytes("UTF-8");BigInteger m = new BigInteger(ptext);// 計算密文cBigInteger c = m.modPow(e, n);System.out.println("c= " + c);// 儲存密文String cs = c.toString();BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream("encrypt.dat")));out.write(cs, 0, cs.length());out.close();}public static void decrypt() throws Exception {// 讀取密文BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream("encrypt.dat")));String ctext = in.readLine();BigInteger c = new BigInteger(ctext);// 讀取私密金鑰FileInputStream f = new FileInputStream("privatekey.dat");ObjectInputStream b = new ObjectInputStream(f);RSAPrivateKey prk = (RSAPrivateKey) b.readObject();BigInteger d = prk.getPrivateExponent();// 擷取私密金鑰參數及解密BigInteger n = prk.getModulus();System.out.println("d= " + d);System.out.println("n= " + n);BigInteger m = c.modPow(d, n);// 顯示解密結果System.out.println("m= " + m);byte[] mt = m.toByteArray();System.out.println("PlainText is ");for (int i = 0; i < mt.length; i++) {System.out.print((char) mt[i]);}}public static void main(String args[]) {try {generateKey();encrypt();decrypt();} catch (Exception e) {System.out.println(e.toString());}}}

以上這篇RSA密碼編譯演算法java簡單實現方法(必看)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.