waysun一路陽光

          不輕易服輸,不輕言放棄.--心是夢(mèng)的舞臺(tái),心有多大,舞臺(tái)有多大。踏踏實(shí)實(shí)做事,認(rèn)認(rèn)真真做人。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 64 評(píng)論 :: 0 Trackbacks
          http://developer.51cto.com/art/200612/36279.htm

          1、RSA算法的原理

          1.1原理

          假設(shè)我們需要將信息從機(jī)器A傳到機(jī)器B,首先由機(jī)器B隨機(jī)確定一個(gè)Key,我們稱之為密匙private_key,將這個(gè)可KEY始終保存在機(jī)器B中而不發(fā)出來;然后,由這個(gè)private_key計(jì)算出另一個(gè)Key,我們稱之為公匙Public_key。這個(gè)Public_key的特性是幾乎不可能通過該Key計(jì)算生成它的private_key。接下來通過網(wǎng)絡(luò)把這個(gè)Public_key傳給機(jī)器A,機(jī)器A受到Public_key后,利用該key,將信息加密,并把加密后的信息通過網(wǎng)絡(luò)發(fā)送到機(jī)器B,最后機(jī)器B利用已知的private_key,就可以解開加密信息。

          1.2步驟

          RSA算法的安全性依賴于大數(shù)因數(shù)分解的困難性。公匙和私匙都是兩個(gè)大素?cái)?shù)的函數(shù)。

          1.2.1首先選擇兩個(gè)大素?cái)?shù)p、q,計(jì)算n=p*q; m=(p-1)*(q-1);
          1.2.2而后隨機(jī)選擇加密密匙Public_key,要求和m互質(zhì),比如Public_key=m-1;
          1.2.3利用歐幾里德算法計(jì)算解密密匙private_key,使private_key滿足

                      

          Public_key*private_key三1(mod m)

          其中Public_key,n是公匙,private_key是密匙。
          1.2.4加密信息text時(shí),利用公式secretword=text^Public_key (mod n)得到密文secretword。
          1.2.5解密時(shí)利用公式word=text^private_key(mod n)得到原文word=text。

          2、程序

          本算法用JAVA編程語言實(shí)現(xiàn),開發(fā)環(huán)境為Eclipse。

                      

          //BJTU 軟件0404 
          import java.io.*;

          public class Rsa
          {
          private int p=0;
          private int q=0;
          private long n=0;
          private long m=0;

          private long public_key=0;//公匙
          private long private_key=0;//密匙

          private long text=0;//明文
          private long secretword=0;//密文
          private long word=0;//解密后明文

          //判斷是否為素?cái)?shù)
          public boolean primenumber(long t)
          {
          long k=0;
          k=(long)Math.sqrt((double)t);
          boolean flag=true;
          outer:for(int i=2;i<=k;i++)
          {
          if((t%i)==0)
          {
          flag = false;
          break outer;
          }
          }
          return flag;
          }
          //輸入PQ
          public void inputPQ()throws Exception
          {
          do{
          System.out.println("請(qǐng)輸入素?cái)?shù)p: ");
          BufferedReader stdin=new BufferedReader(new InputStreamReader

          (System.in));
          String br=stdin.readLine();
          this.p=Integer.parseInt(br);
          }
          while(!primenumber(this.p));
          do{
          System.out.println("請(qǐng)輸入素?cái)?shù)q: ");
          BufferedReader stdin=new BufferedReader(new InputStreamReader

          (System.in));
          String br=stdin.readLine();
          this.q=Integer.parseInt(br);
          }
          while(!primenumber(this.q));
          this.n=this.p*this.q;
          this.m=(p-1)*(q-1);
          System.out.println("這兩個(gè)素?cái)?shù)的乘積為p*q:"+this.n);
          System.out.println("所得的小于N并且與N互素的整數(shù)的個(gè)數(shù)為m=(p-1)(q-1)

          :"+this.m);
          }
          //求最大公約數(shù)
          public long gcd(long a,long b)
          {
          long gcd;
          if(b==0)
          gcd=a;
          else
          gcd=gcd(b,a%b);
          System.out.println("gcd:"+gcd);
          return gcd;

          }
          //輸入公匙
          public void getPublic_key()throws Exception
          {
          do{
          System.out.println("請(qǐng)輸入一個(gè)公鑰的值,這個(gè)值要求小于m并且和m互質(zhì)

          : ");
          BufferedReader stdin=new BufferedReader(new InputStreamReader

          (System.in));
          String br=stdin.readLine();
          this.public_key=Long.parseLong(br);
          }while((this.public_key >= this.m) || (this.gcd

          (this.m,this.public_key)!=1));
          System.out.println("公鑰為:"+this.public_key);
          }
          //計(jì)算得到密匙
          public void getPrivate_key()
          {
          long value=1;
          outer:for(long i=1;;i++)
          {
          value=i*this.m+1;
          System.out.println("value:  "+value);
          if((value%this.public_key==0)&& (value/this.public_key < this.m))
          {
          this.private_key=value/this.public_key;
          break outer;
          }
          }
          System.out.println("產(chǎn)生的一個(gè)私鑰為:"+this.private_key);
          }
          //輸入明文
          public void getText()throws Exception
          {
          System.out.println("請(qǐng)輸入明文:");
          BufferedReader stdin=new BufferedReader(new InputStreamReader

          (System.in));
          String br=stdin.readLine();
          this.text=Long.parseLong(br);
          }
          //加密、解密計(jì)算
          public long colum(long y,long n,long key)
          {
          long mul;
          if(key==1)
          mul=y%n;
          else
          mul=y*this.colum(y,n,key-1)%n;
          return mul;
          }

          //加密后解密
          public void pascolum()throws Exception
          {
          this.getText();
          System.out.println("輸入明文為: "+this.text);
          //加密
          this.secretword=this.colum(this.text,this.n,this.public_key);
          System.out.println("所得的密文為:"+this.secretword);
          //解密
          this.word=this.colum(this.secretword,this.n,this.private_key);
          System.out.println("解密后所得的明文為:"+this.word);

          }
          public static void main(String []args)throws Exception
          {
          Rsa t = new Rsa();
          t.inputPQ();
          t.getPublic_key();
          t.getPrivate_key();
          t.pascolum();
          }

          }

          3、試驗(yàn)介紹

          2.1輸入PQ,計(jì)算m、n 
          3.2輸入公匙,產(chǎn)生密匙 
          3.3輸入明文,產(chǎn)生密文,并解密

          此處時(shí)間限制,明文暫時(shí)用個(gè)數(shù)字代替,有興趣的可以改變程序,變成一段數(shù)字。

          請(qǐng)輸入素?cái)?shù)p:
          23
          請(qǐng)輸入素?cái)?shù)q:
          29
          這兩個(gè)素?cái)?shù)的乘積為p*q:667
          所得的小于N并且與N互素的整數(shù)的個(gè)數(shù)為m=(p-1)(q-1):616
          請(qǐng)輸入一個(gè)公鑰的值,這個(gè)值要求小于m并且和m互質(zhì): 
          611
          gcd:1
          gcd:1
          gcd:1
          gcd:1
          公鑰為:611
          產(chǎn)生的一個(gè)私鑰為:123
          請(qǐng)輸入明文:
          311
          輸入明文為: 311
          所得的密文為:653
          解密后所得的明文為:311

          posted on 2009-04-15 22:47 weesun一米陽光 閱讀(770) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA源碼總結(jié)備用
          主站蜘蛛池模板: 远安县| 麦盖提县| 昭觉县| 开远市| 久治县| 赤峰市| 阳曲县| 囊谦县| 南部县| 金门县| 万载县| 鸡东县| 富蕴县| 廉江市| 泌阳县| 博乐市| 胶南市| 甘德县| 克山县| 浦北县| 保定市| 吉水县| 莎车县| 普兰店市| 宁明县| 江安县| 温泉县| 肃北| 阿合奇县| 新绛县| 藁城市| 安新县| 宁河县| 清水河县| 屏东县| 玉溪市| 梁山县| 临清市| 山阴县| 达州市| 浮山县|