RSA公钥密码系统的Java实现
RSA公钥密码系统的Java实现 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package rsa; import java.math.BigInteger; import java.util.Scanner; import java.util.Random; /** * * @author Administrator */ public class RSABigInteger { private BigInteger bign,ukey,rkey,eulerPhi; private int bitBLength=32; /** * @return the bign */ public BigInteger getBign() { return bign; } public RSABigInteger() { this.generatingUAndRKey(); } public static void main(String[] args) { RSABigInteger rsa = new RSABigInteger(); rsa.test02(); } public void test() { long ptt=25; System.out.println("Before encripting plaintext= "+ptt); BigInteger pt = BigInteger.valueOf(ptt); BigInteger ct=this.encripting(pt); System.out.println("The ciphertext="+ct.longValue()); pt = this.decripting(ct); System.out.println("After decripting the plaintext = "+ pt.longValue()); } public void test02() { BigInteger[] bgPt,bgCt; byte[] btPt,btCt; //long[] lgPt,lgCt; long lgC; String strPt,strCt; int len,i; pstr("Input a plain text to be encripted:"); Scanner scanf = new Scanner(System.in); strPt=scanf.nextLine(); pstr("The plain text to be encripted:"+strPt); len=strPt.length(); bgPt=new BigInteger[len]; bgCt=new BigInteger[len]; //lgPt=new long[len]; //lgCt=new long[len]; btPt=strPt.getBytes(); btCt=new byte[len]; //Encripting pstr("Encripting...."); for(i=0;i<len;i++) { lgC =(long)btPt[i]; bgPt[i]=BigInteger.valueOf(lgC); bgCt[i]=this.encripting(bgPt[i]); lgC=bgCt[i].longValue(); btCt[i]=(byte)lgC; } strCt = new String(btCt); pstr("The ciphertext is "+strCt); //Decripting pstr("Decripting...."); for(i=0;i<len;i++) { bgPt[i]=this.decripting(bgCt[i]); lgC=bgPt[i].longValue(); btPt[i]=(byte)lgC; } strPt=new String(btPt); pstr("The refound plain text is:"+strPt); } public void pstr(String mess) { System.out.println(mess); } /** * @param bign the bign to set */ public void setBign(BigInteger bign) { this.bign = bign; } /** * @return the ukey */ public BigInteger getUkey() { return ukey; } /** * @param ukey the ukey to set */ public void setUkey(BigInteger ukey) { this.ukey = ukey; } /** * @return the rkey */ public BigInteger getRkey() { return rkey; } /** * @param rkey the rkey to set */ public void setRkey(BigInteger rkey) { this.rkey = rkey; } public void generatingUAndRKey() { Random ran = new Random(); BigInteger p = BigInteger.probablePrime(this.bitBLength,ran); BigInteger q = BigInteger.probablePrime(this.bitBLength,ran); this.bign =p.multiply(q); this.eulerPhi=(p.subtract(BigInteger.ONE)). multiply(q.subtract(BigInteger.ONE)); do { this.ukey=BigInteger.probablePrime(bitBLength,ran); }while(!this.ukey.gcd(this.eulerPhi).equals(BigInteger.ONE)); this.rkey=this.ukey.modInverse(this.eulerPhi); } public BigInteger encripting(BigInteger plaintext) { return plaintext.modPow(this.ukey, this.bign); } public BigInteger decripting(BigInteger ciphertext) { return ciphertext.modPow(this.rkey, this.bign); } /** * @return the eulerPhi */ public BigInteger getEulerPhi() { return eulerPhi; } /** * @param eulerPhi the eulerPhi to set */ public void setEulerPhi(BigInteger eulerPhi) { this.eulerPhi = eulerPhi; } /** * @return the bitBLength */ public int getBitBLength() { return bitBLength; } /** * @param bitBLength the bitBLength to set */ public void setBitBLength(int bitBLength) { this.bitBLength = bitBLength; } }
- 粉丝: 0
- 资源: 3
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 27页智慧街道信息化建设综合解决方案.pptx
- 计算机二级Ms-Office选择题汇总.doc
- 单链表的插入和删除实验报告 (2).docx
- 单链表的插入和删除实验报告.pdf
- 物联网智能终端项目设备管理方案.pdf
- 如何打造品牌的模式.doc
- 样式控制与页面布局.pdf
- 武汉理工Java实验报告(二).docx
- 2021线上新品消费趋势报告.pdf
- 第3章 Matlab中的矩阵及其运算.docx
- 基于Web的人力资源管理系统的必要性和可行性.doc
- 基于一阶倒立摆的matlab仿真实验.doc
- 速运公司物流管理模式研究教材
- 大数据与管理.pptx
- 单片机课程设计之步进电机.doc
- 大数据与数据挖掘.pptx
评论11