美文网首页
RSA非对称数字加密

RSA非对称数字加密

作者: kevinp | 来源:发表于2014-09-30 14:03 被阅读108次

/*************************************************************************

  • Compilation: javac RSA.java
  • Execution: java RSA N
  • Generate an N-bit public and private RSA key and use to encrypt
  • and decrypt a random message.
  • % java RSA 50
  • public = 65537
  • private = 553699199426609
  • modulus = 825641896390631
  • message = 48194775244950
  • encrpyted = 321340212160104
  • decrypted = 48194775244950
  • Known bugs (not addressed for simplicity)

    • It could be the case that the message >= modulus. To avoid, use
  • a do-while loop to generate key until modulus happen to be exactly N bits.
    • It's possible that gcd(phi, publicKey) != 1 in which case
  • the key generation fails. This will only happen if phi is a
  • multiple of 65537. To avoid, use a do-while loop to generate
  • keys until the gcd is 1.

*************************************************************************/

import java.math.BigInteger;
import java.security.SecureRandom;

public class RSA {
private final static BigInteger one = new BigInteger("1");
private final static SecureRandom random = new SecureRandom();

private BigInteger privateKey;
private BigInteger publicKey;
private BigInteger modulus;

// generate an N-bit (roughly) public and private key
RSA(int N) {
BigInteger p = BigInteger.probablePrime(N/2, random);
BigInteger q = BigInteger.probablePrime(N/2, random);
BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));

  modulus    = p.multiply(q);                                  
  publicKey  = new BigInteger("65537");     // common value in practice = 2^16 + 1
  privateKey = publicKey.modInverse(phi);

}

BigInteger encrypt(BigInteger message) {
return message.modPow(publicKey, modulus);
}

BigInteger decrypt(BigInteger encrypted) {
return encrypted.modPow(privateKey, modulus);
}

public String toString() {
String s = "";
s += "public = " + publicKey + "\n";
s += "private = " + privateKey + "\n";
s += "modulus = " + modulus;
return s;
}

public static void main(String[] args) {
int N = 50;
RSA key = new RSA(N);
System.out.println(key);

  // create random message, encrypt and decrypt
  //BigInteger message = new BigInteger(N-1, random);
  BigInteger message = new BigInteger("3080152");
  //// create message by converting string to integer
  // String s = "test";
  // byte[] bytes = s.getBytes();
  // BigInteger message = new BigInteger(s);

  BigInteger encrypt = key.encrypt(message);
  BigInteger decrypt = key.decrypt(encrypt);
  System.out.println("message   = " + message);
  System.out.println("encrpyted = " + encrypt);
  System.out.println("decrypted = " + decrypt);

}
}

相关文章

  • kotlin版本RSA非对称加密解密与分段加密解密

    基于kotlin语言的RSA非对称加密解密与分段加密解密 RSA非对称加密 RSA非对称加密的具体算法与来源我就不...

  • RSA 加密解密

    RSA简介 RSA算法是一种非对称加密算法,常被用于加密传输数据。如果配合数字摘要算法,也可用于文件签名。RSA算...

  • 非对称加密算法RSA 学习

    非对称加密算法RSA 学习 RSA加密算法是一种非对称加密算法。RSA是1977年由罗纳德·李维斯特(Ron Ri...

  • RSA加密

    RSA加密为非对称加密实现 对称加密:加密解密使用同一个算法 非对称加密:加密和解密使用不同算法 rsa加密原理 ...

  • 3.2 RSA算法简介

    非对称加密技术 -- RSA算法 RSA算法是流行最广泛的非对称加密算法,也是唯一的基于因式分解的非对称加密算法。...

  • 非对称加密

    非对称加密 非对称加密算法有:RSA,DSA,ECC,DH.其中RSA最为常用. 非对称加密一般有一对公钥和私钥,...

  • RSA非对称加密

    RSA非对称加密 RSA非对称加密, 适用于Java和iOS 应用场景:用户登录时对登录密码进行加密 启动终端, ...

  • iOSRSA加密和SHA验签

    一: 原理 1. 什么是RSA? RSA是一种非对称加密算法,常用来对传输数据进行加密,配合上数字摘要算法,也可以...

  • 6.1 密码学专题 - 非对称加密算法 - RSA 算法

    密码学专题 - 非对称加密算法 - RSA 算法 6.1 RSA 算法 第一个较完善的非对称加密算法 RSA,它既...

  • RSA非对称数字加密

    /********************************************************...

网友评论

      本文标题:RSA非对称数字加密

      本文链接:https://www.haomeiwen.com/subject/vbwutttx.html