Auto commit

上级 7733c45a
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
import static javax.crypto.Cipher.getInstance;
/**
* RSA算法
*/
public class RSA {
public class Main {
private static final BigInteger a = BigInteger.valueOf('a');
private static final Scanner sc = new Scanner(System.in);
......@@ -16,10 +27,21 @@ public class RSA {
// 私钥
public static BigInteger d;
// 帮助信息
private static final String help = "1. 生成密钥 2. RSA加密 3. RSA解密 4.重置数据 5.输入密钥 -1. 退出";
// 帮助消息1
private static final String help1 = "1.自己书写的 2.javaAPI生成";
// 帮助信息2
private static final String help = "1. 生成密钥 2. RSA加密 3. RSA解密 4.重置数据 5.输入密钥 -1. 退出";
public static void main(String[] args) {
while(true){
print.println(help1);
switch(sc.nextInt()){
case 1 -> MyRSA();
case 2 -> javaRSA();
}
}
}
public static void MyRSA(){
while (true) {
print.println("当前数据" + (n == null ? "" : " n = "+n) + (e == null ? "" : " e = "+e) + (d == null ? "" : " d = "+d));
print.println(help);
......@@ -35,10 +57,36 @@ public class RSA {
e = sc.nextBigInteger();
d = sc.nextBigInteger();
}
case -1 -> System.exit(0);
case -1 -> {
return;
}
}
}
}
public static void javaRSA(){
print.println("1.输入指定的公钥和私钥 2.随机生成器生成");
RSA rsa = null;
switch(sc.nextInt()){
case 1->{
print.println("输入密钥n、e和d");
rsa = new RSA(sc.nextBigInteger(),sc.nextBigInteger(),sc.nextBigInteger());
}
case 2->{
print.println("输入密钥长度");
rsa = new RSA(sc.nextInt());
}
}
while(true){
print.println("1.加密 2.解密 -1 退出 然后一行输入文本");
switch(sc.nextInt()){
case 1->print.println(rsa.encode(sc.next()));
case 2 -> print.println(rsa.decode(sc.next()));
case -1 ->{
return;
}
default -> print.println("输入不正确");
}
}
}
private static void init() {
......@@ -90,3 +138,79 @@ public class RSA {
getModPow(e);
}
}
class RSA {
private final PublicKey publicKey;
private final PrivateKey privateKey;
/**
* 自定义公钥和私钥
* @param n 公钥和私钥的n
* @param e 公钥的e
* @param d 私钥的d
*/
public RSA(BigInteger n,BigInteger e,BigInteger d){
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(n, e);
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(n, d);
try {
KeyFactory rsa = KeyFactory.getInstance("RSA");
publicKey = rsa.generatePublic(publicKeySpec);
privateKey = rsa.generatePrivate(privateKeySpec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
throw new RuntimeException(ex);
}
}
/**
* 指定密钥大小生成公钥和私钥
* @param keySize 密钥大小
*/
public RSA(int keySize){
try {
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");
keyGenerator.initialize(keySize);
KeyPair keyPair = keyGenerator.generateKeyPair();
publicKey = keyPair.getPublic();
privateKey = keyPair.getPrivate();
print.println("publicKey = " + Base64.getEncoder().encodeToString(publicKey.getEncoded()));
print.println("privateKey = " + Base64.getEncoder().encodeToString(privateKey.getEncoded()));
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
}
}
/**
* 获取密文
* @param plainText 明文
* @return 密文
*/
public String encode(String plainText){
try {
Cipher cipher = getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(bytes);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException |
BadPaddingException ex) {
throw new RuntimeException(ex);
}
}
/**
* 获取明文
* @param cipherText 密文
* @return 明文
*/
public String decode(String cipherText){
try {
Cipher cipher = getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes = cipher.doFinal(Base64.getDecoder().decode(cipherText));
return new String(bytes);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException |
BadPaddingException ex) {
throw new RuntimeException(ex);
}
}
}
public class RSA {
private final PublicKey publicKey;
private final PrivateKey privateKey;
/**
* 自定义公钥和私钥
* @param n 公钥和私钥的n
* @param e 公钥的e
* @param d 私钥的d
*/
public RSA(BigInteger n,BigInteger e,BigInteger d){
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(n, e);
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(n, d);
try {
KeyFactory rsa = KeyFactory.getInstance("RSA");
publicKey = rsa.generatePublic(publicKeySpec);
privateKey = rsa.generatePrivate(privateKeySpec);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/**
* 指定密钥大小生成公钥和私钥
* @param keySize 密钥大小
*/
public RSA(int keySize){
try {
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");
keyGenerator.initialize(keySize);
KeyPair keyPair = keyGenerator.generateKeyPair();
publicKey = keyPair.getPublic();
privateKey = keyPair.getPrivate();
System.out.println("publicKey = " + Base64.getEncoder().encodeToString(publicKey.getEncoded()));
System.out.println("privateKey = " + Base64.getEncoder().encodeToString(privateKey.getEncoded()));
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
}
}
/**
* 获取密文
* @param plainText 明文
* @return 密文
*/
public String encode(String plainText){
try {
Cipher cipher = getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(bytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 获取明文
* @param cipherText 密文
* @return 明文
*/
public String decode(String cipherText){
try {
Cipher cipher = getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes = cipher.doFinal(Base64.getDecoder().decode(cipherText));
return new String(bytes);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册