仅调用scanner一个库写
时间: 2024-01-21 19:20:12 浏览: 23
好的,以下是一个使用Java中的Scanner类读取用户输入并输出的例子:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入您的姓名:");
String name = scanner.nextLine();
System.out.print("请输入您的年龄:");
int age = scanner.nextInt();
System.out.println("您好," + name + ",您的年龄是" + age + "岁。");
}
}
```
这个例子中,我们首先导入了java.util.Scanner类,然后在main方法中创建了一个Scanner对象scanner,它可以从标准输入流System.in中读取用户的输入。接下来,我们通过调用scanner对象的nextLine()方法和nextInt()方法分别读取用户输入的姓名和年龄,并将它们存储在变量name和age中。最后,我们将这些信息输出到控制台上。
相关问题
仅调用scanner一个库写RSA加密解密算法
RSA加密解密算法需要用到大数运算,Java中已经提供了BigInteger类用于处理大数运算,因此我们需要同时引入Scanner和BigInteger两个库。下面是一个简单的RSA加密解密代码示例:
```java
import java.util.Scanner;
import java.math.BigInteger;
public class RSA {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 1. 选取两个不同的质数p和q
System.out.print("Enter a prime number p: ");
BigInteger p = sc.nextBigInteger();
System.out.print("Enter another prime number q: ");
BigInteger q = sc.nextBigInteger();
// 2. 计算n = p * q 和φ(n) = (p - 1) * (q - 1)
BigInteger n = p.multiply(q);
BigInteger phi_n = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
// 3. 选择一个小于φ(n)的整数e,使得e与φ(n)互质
System.out.print("Choose a number e that is relatively prime to " + phi_n + ": ");
BigInteger e = sc.nextBigInteger();
// 4. 计算e关于φ(n)的模反元素d
BigInteger d = e.modInverse(phi_n);
// 输出公钥和私钥
System.out.println("Public key: (" + n + ", " + e + ")");
System.out.println("Private key: (" + n + ", " + d + ")");
// 加密
System.out.print("Enter a message to encrypt: ");
BigInteger message = sc.nextBigInteger();
BigInteger encrypted = message.modPow(e, n);
System.out.println("Encrypted message: " + encrypted);
// 解密
System.out.print("Enter a message to decrypt: ");
BigInteger cipher = sc.nextBigInteger();
BigInteger decrypted = cipher.modPow(d, n);
System.out.println("Decrypted message: " + decrypted);
}
}
```
注意,在输入p、q和e时,需要保证它们是质数或者与φ(n)互质的整数。此外,为了保证加密后的结果不超过n,输入的明文也需要是一个小于n的正整数。
仅调用scanner一个库并且只需输入明文的写RSA加密解密算法
RSA加密解密算法需要使用Java的BigInteger类库来处理大数运算,下面是一个简单的示例代码:
```java
import java.math.BigInteger;
import java.util.Scanner;
public class RSADemo {
private static final BigInteger ONE = BigInteger.ONE;
private static final BigInteger ZERO = BigInteger.ZERO;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入明文:");
String plaintext = scanner.nextLine();
// 生成RSA公私钥对
BigInteger[] keys = generateKeys(1024);
BigInteger n = keys[0];
BigInteger e = keys[1];
BigInteger d = keys[2];
// 显示公钥和私钥
System.out.println("公钥:(" + n + ", " + e + ")");
System.out.println("私钥:(" + n + ", " + d + ")");
// 加密明文
BigInteger plaintextNum = new BigInteger(plaintext.getBytes());
BigInteger ciphertextNum = encrypt(plaintextNum, e, n);
System.out.println("密文:" + ciphertextNum.toString());
// 解密密文
BigInteger decryptedNum = decrypt(ciphertextNum, d, n);
String decryptedText = new String(decryptedNum.toByteArray());
System.out.println("解密结果:" + decryptedText);
}
// 生成RSA公私钥对
private static BigInteger[] generateKeys(int keySize) {
BigInteger p = BigInteger.probablePrime(keySize / 2, new java.util.Random());
BigInteger q = BigInteger.probablePrime(keySize / 2, new java.util.Random());
BigInteger n = p.multiply(q);
BigInteger phi = p.subtract(ONE).multiply(q.subtract(ONE));
BigInteger e, d;
do {
e = new BigInteger(keySize, new java.util.Random());
} while (e.compareTo(phi) != -1 || !e.gcd(phi).equals(ONE));
d = e.modInverse(phi);
return new BigInteger[] { n, e, d };
}
// RSA加密
private static BigInteger encrypt(BigInteger plaintext, BigInteger e, BigInteger n) {
return plaintext.modPow(e, n);
}
// RSA解密
private static BigInteger decrypt(BigInteger ciphertext, BigInteger d, BigInteger n) {
return ciphertext.modPow(d, n);
}
}
```
该程序首先要求用户输入明文,然后生成一个大小为1024位的RSA公私钥对,并显示出来。接着使用公钥对明文进行加密,将密文输出到控制台。最后使用私钥对密文进行解密,并将解密结果输出到控制台。