简单RSA加密(Java)
这只是为了好玩。这不会用于任何实际加密。我是唯一一名一年级的理工科学生,热爱密码学简单RSA加密(Java),java,cryptography,rsa,Java,Cryptography,Rsa,这只是为了好玩。这不会用于任何实际加密。我是唯一一名一年级的理工科学生,热爱密码学 import java.math.BigInteger; import java.security.SecureRandom; /** * Cryptography. * * Generates public and private keys used in encryption and * decryption * */ public class RSA { private final
import java.math.BigInteger;
import java.security.SecureRandom;
/**
* Cryptography.
*
* Generates public and private keys used in encryption and
* decryption
*
*/
public class RSA
{
private final static BigInteger one = new BigInteger("1");
private final static SecureRandom random = new SecureRandom();
// prime numbers
private BigInteger p;
private BigInteger q;
// modulus
private BigInteger n;
// totient
private BigInteger t;
// public key
private BigInteger e;
// private key
private BigInteger d;
private String cipherText;
/**
* Constructor for objects of class RSA
*/
public RSA(int N)
{
p = BigInteger.probablePrime(N/2, random);
q = BigInteger.probablePrime(N/2, random);
// initialising modulus
n = p.multiply(q);
// initialising t by euclid's totient function (p-1)(q-1)
t = (p.subtract(one)).multiply(q.subtract(one));
// initialising public key ~ 65537 is common public key
e = new BigInteger("65537");
}
public int generatePrivateKey()
{
d = e.modInverse(t);
return d.intValue();
}
public String encrypt(String plainText)
{
String encrypted = "";
int j = 0;
for(int i = 0; i < plainText.length(); i++){
char m = plainText.charAt(i);
BigInteger bi1 = BigInteger.valueOf(m);
BigInteger bi2 = bi1.modPow(e, n);
j = bi2.intValue();
m = (char) j;
encrypted += m;
}
cipherText = encrypted;
return encrypted;
}
public String decrypt()
{
String decrypted = "";
int j = 0;
for(int i = 0; i < cipherText.length(); i++){
char c = cipherText.charAt(i);
BigInteger bi1 = BigInteger.valueOf(c);
BigInteger bi2 = bi1.modPow(d, n);
j = bi2.intValue();
c = (char) j;
decrypted += c;
}
return decrypted;
}
}
这花了很长时间才开始工作。大约在N=18时,它开始分解。在此之后,它将无法正确加密消息。我不知道为什么。有什么见解吗?我也很感激你们能给我提供任何关于密码学的教程或有趣的阅读资料的链接
import java.math.BigInteger;
import java.security.SecureRandom;
/**
* Cryptography.
*
* Generates public and private keys used in encryption and
* decryption
*
*/
public class RSA
{
private final static BigInteger one = new BigInteger("1");
private final static SecureRandom random = new SecureRandom();
// prime numbers
private BigInteger p;
private BigInteger q;
// modulus
private BigInteger n;
// totient
private BigInteger t;
// public key
private BigInteger e;
// private key
private BigInteger d;
private String cipherText;
/**
* Constructor for objects of class RSA
*/
public RSA(int N)
{
p = BigInteger.probablePrime(N/2, random);
q = BigInteger.probablePrime(N/2, random);
// initialising modulus
n = p.multiply(q);
// initialising t by euclid's totient function (p-1)(q-1)
t = (p.subtract(one)).multiply(q.subtract(one));
// initialising public key ~ 65537 is common public key
e = new BigInteger("65537");
}
public int generatePrivateKey()
{
d = e.modInverse(t);
return d.intValue();
}
public String encrypt(String plainText)
{
String encrypted = "";
int j = 0;
for(int i = 0; i < plainText.length(); i++){
char m = plainText.charAt(i);
BigInteger bi1 = BigInteger.valueOf(m);
BigInteger bi2 = bi1.modPow(e, n);
j = bi2.intValue();
m = (char) j;
encrypted += m;
}
cipherText = encrypted;
return encrypted;
}
public String decrypt()
{
String decrypted = "";
int j = 0;
for(int i = 0; i < cipherText.length(); i++){
char c = cipherText.charAt(i);
BigInteger bi1 = BigInteger.valueOf(c);
BigInteger bi2 = bi1.modPow(d, n);
j = bi2.intValue();
c = (char) j;
decrypted += c;
}
return decrypted;
}
}
import java.math.biginger;
导入java.security.SecureRandom;
/**
*密码学。
*
*生成用于加密和加密的公钥和私钥
*解密
*
*/
公共类RSA
{
私有最终静态BigInteger one=新的BigInteger(“1”);
private final static SecureRandom random=新SecureRandom();
//素数
私有整数bigp;
私有大整数q;
//模数
私有大整数n;
//托蒂
私有大整数t;
//公钥
私有大整数;
//私钥
私有大整数d;
私有字符串密文;
/**
*RSA类对象的构造函数
*/
公共RSA(int N)
{
p=BigInteger.probablePrime(N/2,随机);
q=BigInteger.probablePrime(N/2,随机);
//初始模量
n=p.乘以(q);
//用欧几里得toticent函数(p-1)(q-1)初始化t
t=(p.减去(一))。乘(q.减去(一));
//初始化公钥~65537是公共公钥
e=新的大整数(“65537”);
}
public int generatePrivateKey()
{
d=e.modInverse(t);
返回d.intValue();
}
公共字符串加密(字符串明文)
{
字符串加密=”;
int j=0;
for(int i=0;i
由于您只有2^16条不同的消息,因此加密可能会被破坏。只有使用正确的填充(OEP)时,RSA才是安全的。当然,由于将一个字符映射到一个RSA块,所以密码文本占用的空间是纯文本的100倍
j = bi2.intValue();
m = (char) j;
这两种操作都严重溢出。bi2是一个大整数是有原因的。它不适合32位整数/16位字符。由于截断整数会丢失大部分位,解密将无法工作,因为您破坏了密码文本。您需要更具体地说明“分解”的含义,没有“正确”加密消息,以及它是否适用于N18。另外,目前你在ECB模式下使用RSA,而你应该使用混合方案。哦,至于要阅读的文本——Schneier、Ferguson和Kohno的加密工程。加密是有效的,但在N>18时解密是无效的。谢谢你的阅读建议,我会尽快从图书馆拿到的!啊,这很有道理。谢谢我会仔细阅读正确的填充。我有很多东西要学。