我正在做一个像这样的简单程序:
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();
}
}
}
}
但这不起作用。输出应为:
最佳答案
首先,您的方法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;
}