シンプルで完全なコード。このコードを通じて、JavaのRSA暗号化アルゴリズムの実装方法を予備的に理解することができます。このクラスを直接使用できます。高レベルのレベルがある場合は、自分でコードを変更して改善してください。
パッケージセキュリティ; java.securityをインポート。*; java.security.spec。*; import java.security.interfaces。*; import javax.crypto.spec。*; import javax.crypto.interfaces。 void generatekey(){try {keypairgenerator kpg = keypairgenerator.getInstance( "rsa"); kpg.initialize(1024); keypair kpg.genkeypair(); publickey pbkey = kp.getpublic(); privatekey prkey prkepputate(); f1 = new fileoutputStream( "pubkey.dat"); objectOutputStream b1 = new objectOutputStream(f1); b1.writeobject(pbkey); //秘密のキーfileoutputStream f2 = new fileoutputStream( "privatekey.dat"); objectoutputstream b2 = new objectutputstream(f2); e){}} public static void encrypt()スロー例外{string s = "hello world!"; // publicキーとパラメーターe、nfileinputStream f = new fileinputStream( "pubkey.dat"); objectinputStream b = new objectinputStream(f); rsapublickey pbk =(rsapublegekey()migintpublimekey() 'gigintpublimekey() pbk.getpublicexponent(); biginteger n = pbk.getmodulus(); system.out.println( "e =" + e); system.out.println( "n =" + n); // get pleantext mbyte ptext [] = s.getBytes( " ciphertext cbiginteger c = m.modpow(e、n); system.out.println( "c =" + c); // ciphertext string cs = c.toString(); bufferedwriter out = new Bufferedwriter(new outputStreamwriter(new fileoutputstream(cent.dat "); cs.length()); out.close();} public static void decrypt()throws exception {// ciphertext bufferedReader in = new inputStreamReader(new fileinputStream( "encrypt.dat"))) FileInputStream f = new FileInputStream( "privatekey.dat"); objectInputStream b = new objectInputStream(f); rsaprivateKey prk =(rsaprivatekey)b.readObject(); prk.getModulus(); system.out.println( "d =" + d); system.out.println( "n =" + n); biginteger m = c.modpow(d、n); //復号化結果System.out.println( "m =" + m); "); 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(exceps.out.println(e.toString());}}}}}Java RSA暗号化アルゴリズムの単純な実装方法に関する上記の記事(必須読み取り)は、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。