[原创]RSA实现

import java.math.BigInteger;

/**
 * RSA算法非常简单,概述如下:
 * 1、找两素数p和q
 * 2、取n=p*q
 * 3、取t=(p-1)*(q-1)
 * 4、取任何一个数e,要求满足e<t并且e与t互素(就是最大公因数为1)
 * 5、取d*e%t==1
 *
 * 这样最终得到三个数: n d e
 *
 * 设消息为数M (M <n) 设c=(M**d)%n就得到了加密后的消息c
 * 设m=(c**e)%n则 m == M,从而完成对c的解密。
 * 注:**表示次方,上面两式中的d和e可以互换。
 *
 * 在对称加密中: n d两个数构成公钥,可以告诉别人; n e两个数构成私钥,e自己保留,不让任何人知道。
 * 给别人发送的信息使用e加密,只要别人能用d解开就证明信息是由你发送的,构成了签名机制。
 * 别人给你发送信息时使用d加密,这样只有拥有e的你能够对其解密。
 *
 * rsa的安全性在于对于一个大数n,没有有效的方法能够将其分解 从而在已知n d的情况下无法获得e;
 * 同样在已知n e的情况下无法 求得d。
 *
 * @author Administrator
 *
 */

public class RSA {

 /**
  * @uml.property name="generatorKey"
  * @uml.associationEnd multiplicity="(1 1)"
  *                     inverse="rSA:sam.rsa.GeneratorKey"
  */
 private GeneratorKey generatorKey = null;

 private static final int BITLENGTH = 256;

 public RSA() {
  generatorKey = new GeneratorKey();
  generatorKey.genKey(BITLENGTH);
 }

 private BigInteger swap;

 public void encrypt(String testString) {
  byte[] msg = testString.getBytes();
  swap = new BigInteger(msg).modPow(generatorKey.getPubkey(),
    generatorKey.getModulus());
 }

 public String descrpt() {
  return new String(swap.modPow(generatorKey.getPrikey(),
    generatorKey.getModulus()).toByteArray());
 }

}

=======================================================


import java.math.BigInteger;
import java.util.Random;

public class GeneratorKey {

 public void genKey(int bitLength) {
  Random random = new Random();
  BigInteger p, q, t;
  // get two prime
  p = BigInteger.probablePrime(bitLength, random);
  do {
   q = BigInteger.probablePrime(bitLength, random);
  } while (p.compareTo(q) == 0);
  t = p.subtract(BigInteger.valueOf(1));
  t = t.multiply(q.subtract(BigInteger.valueOf(1)));

  modulus = p.multiply(q);

  // get pubkey
  // pubkey<modulus and coprime modulus
  do {
   pubkey = BigInteger.probablePrime(bitLength, random);
  } while (pubkey.gcd(t).compareTo(BigInteger.valueOf(1)) != 0);

  prikey = pubkey.modInverse(t);
  System.out.println("pubkey : " + getPubkey());
  System.out.println("prikey : " + getPrikey());
  System.out.println("modulus : " + getModulus());
 }

 /**
  * @uml.property name="prikey" readOnly="true"
  */
 private BigInteger prikey;

 /**
  * Getter of the property <tt>prikey</tt>
  *
  * @return Returns the prikey.
  * @uml.property name="prikey"
  */
 public BigInteger getPrikey() {
  return prikey;
 }

 /**
  * @uml.property name="pubkey" readOnly="true"
  */
 private BigInteger pubkey;

 /**
  * Getter of the property <tt>pubkey</tt>
  *
  * @return Returns the pubkey.
  * @uml.property name="pubkey"
  */
 public BigInteger getPubkey() {
  return pubkey;
 }

 /**
  * @uml.property name="modulus" readOnly="true"
  */
 private BigInteger modulus;

 /**
  * Getter of the property <tt>modulus</tt>
  *
  * @return Returns the modulus.
  * @uml.property name="modulus"
  */
 public BigInteger getModulus() {
  return modulus;
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值