Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 315|回复: 0

[算法学习]一个用来进行RSA加密的类

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-7 00:03:43 | 显示全部楼层 |阅读模式
    import java.io.*;
    import java.math.*;
    import java.security.*;
      public class RSA1
    implements Serializable
    {
    private final static BigInteger one = BigInteger.ONE;
    private final static SecureRandom random =
             new SecureRandom();
    private BigInteger privateKey = null;
    private BigInteger publicKey = null;
    private BigInteger modulus = null;  private BigInteger p1 = null;
    private BigInteger p2 = null;
      
      
    1. private BigInteger modulusE;
    2.   private BigInteger publicKeyE;
    3.   private int N;
    4.   private BigInteger phi0;
    5.   public RSA1(int N)
    6.   {
    7.     this.N = N;
    8.     p1 = BigInteger.probablePrime(N / 2, random);
    9.    
    10.     p2 = BigInteger.probablePrime(N / 2, random);
    11.       
    12.     phi0 = (p1.subtract(one)).multiply(p2.subtract(one));
    13.    
    14.     modulus = p1.multiply(p2);
    15.    
    16.     setPrivateKey();
    17.     //e
    18.     publicKey = privateKey.modInverse(phi0);
    19.     modulusE = modulus;
    20.     publicKeyE = publicKey;
    21.   }
    22. public BigInteger getModulus()
    23.   {
    24.     return modulus;
    25.   }
    26.   public BigInteger getPublicKey()
    27.   {
    28.     return publicKey;
    29.   }
    30.   public void setPublicKey(BigInteger p, BigInteger n)
    31.   {
    32.     publicKeyE = p;
    33.     modulusE = n;
    34.   }
    35.   
    36.   public void xOrClePrive(BigInteger xor)
    37.   {
    38.     xor = xor.pow(4);
    39.     privateKey = privateKey.xor(xor);
    40.   }
    41.   
    42.   private void setPrivateKey()
    43.   {
    44.     do
    45.     {
    46.       privateKey = BigInteger.probablePrime(N / 2, random);
    47.     }
    48.     while (privateKey.gcd(phi0).intValue() != 1 ||
    49.            privateKey.compareTo(modulus) != -1 ||
    50.            privateKey.compareTo(p1.max(p2)) == -1);
    51.   }
    52.   
    53.   public BigInteger encrypt(BigInteger message)
    54.   {
    55.     BigInteger rep = null;
    56.     String str_message = new String(message.toByteArray());
    57.     //System.out.println("--"+str_message.length());
    58.     if (message != null)
    59.     {
    60.       if (str_message.length() <= (N / 8))
    61.         if (publicKeyE != null && modulusE != null &&
    62.             message.toByteArray().length < Integer.MAX_VALUE)
    63.           rep = message.modPow(publicKeyE, modulusE);
    64.     }
    65.     return rep;
    66.   }
    67.   public BigInteger encrypt(BigInteger message, BigInteger publicKeyP,
    68.                             BigInteger modulusP)
    69.   {
    70.     BigInteger rep = null;
    71.     String str_message = new String(message.toByteArray());
    72.    
    73.     if (str_message.length() <= (N / 8))
    74.       if (publicKeyP != null && modulusP != null &&
    75.           message.toByteArray().length < Integer.MAX_VALUE)
    76.         rep = message.modPow(publicKeyP, modulusP);
    77.     return rep;
    78.   }
    79.   
    80. public BigInteger decrypt(BigInteger encrypted)
    81.   {
    82.     return encrypted.modPow(privateKey, modulus);
    83.   }
    84.   public String toString()
    85.   {
    86.     String s = "";
    87.     s += "public  = " + publicKey + "
    88. ";
    89.     s += "modulus = " + modulus+ "
    90. ";
    91.     return s;
    92.   }
    93.     public static void main(String args[]){
    94.        RSA1 R=new RSA1(240);
    95.        System.out.println(R.toString());
    96.        BigInteger x=R.encrypt(new BigInteger("222222222222222222222233333333333334444444444"));
    97.        System.out.println(x);
    98.        System.out.println(R.decrypt(x));
    99.     }
    100. }
    101. 运行:
    102. C:java>java  RSA1
    103. public  = 295499527141006173098887481464331780280623580059489695909599497377842439
    104. modulus = 902341935876083298397868636041367427179245394414893749663625421740246927
    105. 292899245381108253829844138919831774574256494250768214184444127012280469
    106. 222222222222222222222233333333333334444444444
    复制代码

       
         
         
          
          

            
          

            
          
         
       


      


    源码下载:http://file.javaxxz.com/2014/11/7/000343250.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 10:01 , Processed in 0.360878 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表