gpt4 book ai didi

java - 使用JAVA进行RSA加密/解密

转载 作者:行者123 更新时间:2023-12-01 16:24:24 28 4
gpt4 key购买 nike

我正在做一个像这样的简单程序:

package rsaexample;

import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class RSAExample {

private static final String PUBLIC_KEY_FILE = "Public.key";
private static final String PRIVATE_KEY_FILE = "Private.key";

public static void main(String[] args) throws IOException {

try {
System.out.println("-------Generate public and private key------");
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
System.out.println("\n-------Pulling out parameters------\n");
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec rsaPubKeySpec = keyFactory.getKeySpec (publicKey, RSAPublicKeySpec.class);
RSAPrivateKeySpec rsaPrivKeySpec = keyFactory.getKeySpec (privateKey, RSAPrivateKeySpec.class);

System.out.println("\n---saving keys---\n");
RSAExample rsaObj = new RSAExample ();
rsaObj.saveKeys(PUBLIC_KEY_FILE, rsaPubKeySpec.getModulus(), rsaPubKeySpec.getPublicExponent());
rsaObj.saveKeys(PRIVATE_KEY_FILE, rsaPrivKeySpec.getModulus(), rsaPrivKeySpec.getPrivateExponent());

byte[] encryptedData = rsaObj.encryptData ("Data to Encrypt");

rsaObj.decryptData(encryptedData);

} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
System.out.println(e);
}
}

private void savekeys (String fileName, BigInteger mod, BigInteger exp) throws IOException {
FileOutputStream fos = null;
ObjectOutputStream oos = null;

try {

System.out.println("Generating" + fileName + "...");
fos = new FileOutputStream(fileName);
oos = new ObjectOutputStream(new BufferedOutputStream(fos));
oos.writeObject (mod);
oos.writeObject (exp);
System.out.println(fileName + "generated successfully");

} catch (Exception e) {
e.printStackTrace();

} finally {
if (oos != null) {
oos.close();

if (fos != null)
fos.close();
}
}
}
private byte[] encryptData (String data) throws IOException {
System.out.println("\n---ENC STARTED---");
System.out.println("Data Before Encryption :" + data);
byte[] dataToEncrypt = data.getBytes();
byte[] encryptedData = null;

try {
PublicKey pubKey = readPublicKeyFromFile(this.PUBLIC_KEY_FILE);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
encryptedData = cipher.doFinal(dataToEncrypt);
System.out.println("Encrypted Data : " + encryptedData);

} catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}

System.out.println("---enc complete--");
return encryptedData;
}

private void decryptData (byte[] data) throws IOException {
System.out.println("\n---DEC STARTED---");
byte[] decryptedData = null;

try {
PrivateKey privateKey = readPrivateKeyFromFile (this.PRIVATE_KEY_FILE);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
decryptedData = cipher.doFinal(data);
System.out.println("DECRYPTED DATA : " + new String(decryptedData));

} catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}

System.out.println("---dec complete--");
}
public PrivateKey readPrivateKeyFromFile(String fileName) throws IOException {
FileInputStream fis = null;
ObjectInputStream ois = null;

try {
fis = new FileInputStream(new File(fileName));
ois = new ObjectInputStream (fis);
BigInteger modulus = (BigInteger) ois.readObject();
BigInteger exponent = (BigInteger) ois.readObject();

RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec (modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey privateKey = fact.generatePrivate(rsaPrivateKeySpec);
return privateKey;
}

catch (IOException | ClassNotFoundException | NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();

} finally {
if (ois != null) {
ois.close();

if (fis != null)
fis.close();
}

}
}

}

但是它不起作用。输出应该是:

enter image description here

最佳答案

首先,您的方法 savekeys 称为小写的 savekeys,因此在调用 rsaObj.saveKeys 时会出现编译错误。并且您正在调用一个名为 readPublicKeyFromFile 的方法,但您没有该方法的实现。

这应该可以解决问题。

private PublicKey readPublicKeyFromFile(String fileName) throws IOException
{
FileInputStream fis = null;
ObjectInputStream ois = null;

try {
fis = new FileInputStream(new File(fileName));
ois = new ObjectInputStream (fis);
BigInteger modulus = (BigInteger) ois.readObject();
BigInteger exponent = (BigInteger) ois.readObject();

RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec (modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey privateKey = fact.generatePublic(rsaPublicKeySpec);
return privateKey;
}

catch (IOException | ClassNotFoundException | NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();

} finally {
if (ois != null) {
ois.close();
if (fis != null)
fis.close();
}

}
return null;
}

关于java - 使用JAVA进行RSA加密/解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62180184/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com