Java 试图理解RSA加密代码示例
我试图理解这段代码,但我无法理解它。 因此,程序接收一个值,并使用“RSA”算法将其加密为输入值 我不明白的是代码的bytesToString部分。程序是否将输入值转换为字节,然后对字节进行加密Java 试图理解RSA加密代码示例,java,encryption,cryptography,rsa,Java,Encryption,Cryptography,Rsa,我试图理解这段代码,但我无法理解它。 因此,程序接收一个值,并使用“RSA”算法将其加密为输入值 我不明白的是代码的bytesToString部分。程序是否将输入值转换为字节,然后对字节进行加密 public RSA() { r = new Random(); p = BigInteger.probablePrime(bitlength, r); q = BigInteger.probablePrime(bitlength, r); N = p.multiply(
public RSA() {
r = new Random();
p = BigInteger.probablePrime(bitlength, r);
q = BigInteger.probablePrime(bitlength, r);
N = p.multiply(q);
phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(bitlength/2, r);
while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0 ) {
e.add(BigInteger.ONE);
}
d = e.modInverse(phi);
}
public static void main (String[] args) throws IOException
{
RSA rsa = new RSA();
DataInputStream in=new DataInputStream(System.in);
String teststring ;
System.out.println("Enter the plain text:");
teststring=in.readLine();
System.out.println("Encrypting String: " + teststring);
System.out.println("String in Bytes: " + bytesToString(teststring.getBytes()));
// encrypt
byte[] encrypted = rsa.encrypt(teststring.getBytes());
System.out.println("Encrypted String in Bytes: " + bytesToString(encrypted));
// decrypt
byte[] decrypted = rsa.decrypt(encrypted);
System.out.println("Decrypted String in Bytes: " + bytesToString(decrypted));
System.out.println("Decrypted String: " + new String(decrypted));
}
这是公钥还是私钥
程序是否将输入值转换为字节,然后对字节进行加密
public RSA() {
r = new Random();
p = BigInteger.probablePrime(bitlength, r);
q = BigInteger.probablePrime(bitlength, r);
N = p.multiply(q);
phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(bitlength/2, r);
while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0 ) {
e.add(BigInteger.ONE);
}
d = e.modInverse(phi);
}
public static void main (String[] args) throws IOException
{
RSA rsa = new RSA();
DataInputStream in=new DataInputStream(System.in);
String teststring ;
System.out.println("Enter the plain text:");
teststring=in.readLine();
System.out.println("Encrypting String: " + teststring);
System.out.println("String in Bytes: " + bytesToString(teststring.getBytes()));
// encrypt
byte[] encrypted = rsa.encrypt(teststring.getBytes());
System.out.println("Encrypted String in Bytes: " + bytesToString(encrypted));
// decrypt
byte[] decrypted = rsa.decrypt(encrypted);
System.out.println("Decrypted String in Bytes: " + bytesToString(decrypted));
System.out.println("Decrypted String: " + new String(decrypted));
}
是的,加密通常在二进制数据上执行。另一方面,RSA原语对大整数使用模运算。您展示的RSA
在内部使用biginger
,它提供了从字节数组创建大整数的方法
还有一种是采用字符串的,但假定该字符串仅包含以10为基数的符号加密的数字,而不是任意数据
这是公钥还是私钥
不,这些值都不是公钥或私钥的表示形式。密钥对隐藏在RSA=new RSA()后面代码>
公钥由模数N
和公共指数e
组成。私钥由模数N
和私钥指数d
组成。通常,私钥还包含公钥指数,以便可以从私钥创建公钥
优化的实现在私钥中有其他中间值,而您的实现不使用这些值。谢谢您的帮助。你看过完整的代码了吗?我应该打印e作为公钥,打印d作为私钥吗?是的,没错,但不要在项目中使用它进行实际加密。这是一个原始的RSA,它真的坏了。它没有经过优化,也没有使用填充。改用新的。