【Jsp精品源码栏目提醒】:网学会员为需要Jsp精品源码的朋友们搜集整理了java源程序利用非对称密码RSA算法实现加解密 - 其它资料相关资料,希望对各位网友有所帮助!
源程序:(RSA1.java 文件)import javax.swing.import java.awt.event.import java.math.import java.util.import java.awt.import java.io.public class RSA1 public static void mainString args MyFrame frame new MyFrame MyPanel_fbutton panel_fbutton newMyPanel_fbuttonframeframe.Pframe.Qframe.dframe.e FlowLayout fl new FlowLayout FlowLayout.CENTER00 frame.setLayoutfl frame.addpanel_fbutton frame.setBounds 150 100 500 480 frame.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE frame.setVisibletrue class MyFrame extends JFrame public MyFrame setTitlequotRSA 算法quot addwel MyPanel_p panel_p new MyPanel_pP addpanel_p MyPanel_q panel_q new MyPanel_qQ addpanel_q MyPanel_d panel_d new MyPanel_dd addpanel_d MyPanel_e panel_e new MyPanel_ee addpanel_e MyPanel_in panel_in new MyPanel_ininput addpanel_in MyPanel_out panel_out new MyPanel_outoutput addpanel_out MyPanel_out1 panel_out1 new MyPanel_out1output1 addpanel_out1 MyPanel_button panel_button newMyPanel_buttonPQdeinputoutputoutput1 addpanel_button private JLabel wel new JLabelquot RSA 算法演示 quot protected JTextField P new JTextField35 protected JTextField Q new JTextField35 protected JTextField d new JTextField35 protected JTextField e new JTextField35 protected JTextArea input new JTextArea435 protected JTextArea output new JTextArea435 protected JTextArea output1 new JTextArea435class MyPanel_fbutton extends JPanel public MyPanel_fbuttonFrame aframeJTextField aP JTextField aQ JTextField adJTextField ae frame aframe P aP Q aQ e ae d ad private Frame frame private JTextField P private JTextField Q private JTextField d private JTextField eclass MyPanel_p extends JPanel public MyPanel_pJTextField aP PaP addnew JLabelquot 质数 P:quot addP private JTextField Pclass MyPanel_q extends JPanel public MyPanel_qJTextField aQ QaQ addnew JLabelquot 质数 Q:quot addQ private JTextField Qclass MyPanel_d extends JPanel public MyPanel_dJTextField ad dad addnew JLabelquot 公 钥:quot addd private JTextField dclass MyPanel_e extends JPanel public MyPanel_eJTextField ae eae addnew JLabelquot 私 钥:quot adde private JTextField eclass MyPanel_in extends JPanel public MyPanel_inJTextArea ainput input ainput addnew JLabelquot 输入明文:quot JScrollPane jsp1 new JScrollPane inputvh addjsp1 private JTextArea input int vJScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED int hJScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDEDclass MyPanel_out extends JPanel public MyPanel_outJTextArea aoutput output aoutput addnew JLabelquot 生成的密文:quot JScrollPane jsp new JScrollPane outputvh addjsp private JTextArea output int vJScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED int hJScrollPane.HORIZONTAL_SCROLLBAR_NEVERclass MyPanel_out1 extends JPanel public MyPanel_out1JTextArea aoutput1 output1 aoutput1 addnew JLabelquot解密后的明文:quot JScrollPane jsp new JScrollPane output1vh addjsp private JTextArea output1 int vJScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED int hJScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDEDclass MyPanel_button extends JPanel public MyPanel_buttonJTextField aPJTextField aQJTextField adJTextField ae JTextArea ainputJTextArea aoutputJTextArea aoutput1 P aP Q aQ e ae d ad input ainput output aoutput output1 aoutput1 randProduce.addActionListenernew RandListener P Q addrandProduce randD.addActionListenernew RandDListener P Q d e addrandD encode.addActionListenernew EncodeListener P Q d e input output addencode decode.addActionListenernew DecodeListener P Q d e output output1 adddecode private JTextField P private JTextField Q private JTextField d private JTextField e private JTextArea input private JTextArea output private JTextArea output1 private JButton randProduce new JButtonquot生成质数 P 和 Qquot private JButton randD new JButtonquot生成公钥和私钥quot private JButton encode new JButtonquot加密quot private JButton decode new JButtonquot解密quotclass FileEncodeListener implements ActionListener public FileEncodeListenerFrame fJTextField ap JTextField aqJTextFieldaeJTextField ad P ap Q aq E ae D ad fr f public void actionPerformedActionEvent ee FileDialog fd new FileDialogfr fd.setVisibletrue String infileName fd.getDirectoryfd.getFile String inStr new String inStr PublicMethod.readinfileName BigInteger PrimeP new BigIntegerP.getText BigInteger PrimeQ new BigIntegerQ.getText BigInteger n PrimeP.multiplyPrimeQ int nLen n.bitLength int mintMath.ceildoublenLen/16.0 nLen nLen-1 / 16 String outStr new String outStr PublicMethod.EncodeinStrPrimePPrimeQnnLenmD foriinfileName.length-1igt0--i ifinfileName.charAti. break String outfileName infileName.substring0i outfileName outfileName new Stringquot.EncodeRsaquot infileName.substringiinfileName.length PublicMethod.outputoutfileNameoutStr private JTextField P private JTextField Q private JTextField E private JTextField D private Frame fr int iclass FileDecodeListener implements ActionListener public FileDecodeListenerFrame fJTextField ap JTextField aqJTextFieldaeJTextField ad P ap Q aq E ae D ad fr f public void actionPerformedActionEvent ee FileDialog fd new FileDialogfr fd.setVisibletrue String infileName fd.getDirectoryfd.getFile String inStr new String inStr PublicMethod.inputinfileName System.out.printlninStr BigInteger PrimeP new BigIntegerP.getText BigInteger PrimeQ new BigIntegerQ.getText BigInteger n PrimeP.multiplyPrimeQ int nLen n.bitLength int mintMath.ceildoublenLen/16.0 nLen nLen-1 / 16 String outStr new String outStr PublicMethod.DecodeinStrPrimePPrimeQnnLenmE foriinfileName.length-1igt0--i ifinfileName.charAti. break String outfileName infileName.substring0i outfileName outfileName new Stringquot.DecodeRsaquot infileName.substringiinfileName.length PublicMethod.writeoutfileNameoutStr private JTextField P private JTextField Q private JTextField E private JTextField D private Frame fr int iclass RandListener implements ActionListener public RandListenerJTextField aP JTextField aQ P aP Q aQ public void actionPerformedActionEvent e PublicMethod.GetPrime P PublicMethod.GetPrime Q private JTextField P private JTextField Qclass RandDListener implements ActionListener public RandDListenerJTextField aP JTextField aQ JTextField ad JTextField ae P aP Q aQ d ad e ae public void actionPerformedActionEvent ee BigInteger PP new BigIntegerP.getText BigInteger QQ new BigIntegerQ.getText BigInteger temp PP.subtractnewBigIntegerquot1quot.multiplyQQ.subtractnew BigIntegerquot1quot BigInteger temp1 do temp1new BigInteger100 new Random.modtemp whilePublicMethod.MillerRobintemp1false d.setTexttemp1.toString e.setTextPublicMethod.invmodtemp1 temp.toString private JTextField P private JTextField Q private JTextField d private JTextField eclass EncodeListener implements ActionListener public EncodeListenerJTextField aP JTextField aQ JTextField ad JTextFieldae JTextArea in JTextArea out P aP Q aQ d ad e ae input in output out public void actionPerformedActionEvent ee BigInteger PrimeP new BigIntegerP.getText BigInteger PrimeQ new BigIntegerQ.getText BigInteger n PrimeP.multiplyPrimeQ int nLen n.bitLength int mintMath.ceildoublenLen/16.0 nLen nLen-1 / 16 String inStr input.getText output.setTextPublicMethod.EncodeinStrPrimePPrimeQnnLenme private JTextField P private JTextField Q private JTextField d private JTextField e private JTextArea input private JTextArea outputclass DecodeListener implements ActionListener public DecodeListenerJTextField aP JTextField aQ JTextField ad JTextFieldae JTextArea out JTextArea out1 P aP Q aQ d ad e ae output out output1 out1 public void actionPerformedActionEvent ee BigInteger PrimeP new BigIntegerP.getText BigInteger PrimeQ new BigIntegerQ.getText BigInteger n PrimeP.multiplyPrimeQ int nLen n.bitLength int mintMath.ceildoublenLen/16.0 nLen nLen-1 / 16 String inStr output.getText output1.setTextPublicMethod.DecodeinStrPrimePPrimeQnnLenmd private JTextField P private JTextField Q private JTextField d private JTextField e private JTextArea output private JTextArea output1class PublicMethod public static void GetPrime JTextField prime BigInteger num new BigIntegerquot0quot Random rand new Random do int length intMath.random20100 System.out.printlnlength num new BigInteger length 5 rand prime.setTextnum.toString whileMillerRobinnumfalse public static boolean MillerRobinBigInteger num int time 1000 BigInteger mod num.modnew BigIntegerquot2quot ifmod.equalsnew BigIntegerquot0quot return false int s 0 j0 BigInteger tnum.subtractnew BigIntegerquot1quot while t.modnew BigIntegerquot2quot.equalsquot0quot t.dividenew BigIntegerquot2quot s forint i0 ilttime i BigInteger a new BigInteger100 new Random.modnum.subtractnewBigIntegerquot3quot.addnew BigIntegerquot2quot BigInteger y powmoda t num ify.equalsnew BigIntegerquot1quotfalse ampamp y.equalsnum.subtractnew BigIntegerquot1quotfalse j1 whilejsampampy.equalsnum.subtractnew BigIntegerquot1quotfalse y y.multiplyy.modnum ify.equalsnew BigIntegerquot1quot return false j ify.equalsnum.subtractnew BigIntegerquot1quotfalse return false return true public static BigInteger powmod BigInteger a BigInteger t BigInteger num BigInteger A new BigIntegerquot1quot whilet.equalsnew BigIntegerquot0quotfalse ift.modnew BigIntegerquot2quot.equalsnew BigIntegerquot1quot A A.multiplya.modnum a a.multiplya.modnum tt.dividenew BigIntegerquot2quot return Apublic static BigInteger invmodBigInteger a BigInteger b System.out.printlnaquot quotb BigInteger s0new BigIntegerquot1quot s1new BigIntegerquot0quot s2 q t b0b whileb.equalsnew BigIntegerquot0quotfalse qa.divideb s2s0.subtract q.multiplys1 ifs2.compareTonew BigIntegerquot0quot-1 s2s2.modb0 else s2b0.subtract s2.multiplynew BigIntegerquot-1quot.modb0 s0s1 s1s2 tb ba.modb at ifa.equalsnew BigIntegerquot1quot return s0 else return new BigIntegerquot0quot public static String EncodeString inStrBigInteger PrimePBigInteger PrimeQ BigInteger nint nLenint mJTextField d BigInteger res new BigIntegerquot0quot StringBuffer outBuf new StringBuffer int ikj fori0iltinStr.lengthij BigInteger t new BigIntegerquot0quot forjijltinLenampampjltinStr.lengthj tt.shiftLeft16 long num inStr.charAt j tt.addBigInteger.valueOfnum res PublicMethod.powmodtnew BigIntegerd.getTextn String buf new String fork0kltmk long num res.andBigInteger.valueOf65535.longValue res res.shiftRight16 buf charnumbuf outBuf outBuf.appendbuf return outBuf.toStringpublic static String DecodeString inStrBigInteger PrimePBigInteger PrimeQ BigInteger nint nLenint mJTextField e StringBuffer outBuf new StringBuffer BigInteger res new BigIntegerquot0quot int ij fori0iltinStr.lengthij BigInteger t new BigIntegerquot0quot forj0jltmampampjiltinStr.lengthj t t.shiftLeft16 long num longinStr.charAt ji tt.addBigInteger.valueOfnum res PublicMethod.powmodtnew BigIntegere.getTextn String buf new String whileres.compareTonew BigIntegerquot0quotgt0 long num res.andBigInteger.valueOf65535.longValue buf charnum buf res res.shiftRight16 outBuf outBuf.appendbuf return outBuf.toStringpublic static String readString infileName String ans new String try FileInputStream fis new FileInputStreaminfileName InputStreamReader isr new InputStreamReaderfis BufferedReader br new BufferedReader isr int t whiletrue .上一篇:经典木马后门工具