Skip to content

Commit

Permalink
Fix encoding issue
Browse files Browse the repository at this point in the history
  • Loading branch information
muriplz committed Jan 17, 2024
1 parent fe6c401 commit 569bd8d
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/java/com/kryeit/votifier/crypto/RSAIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import javax.xml.bind.DatatypeConverter;

Expand Down Expand Up @@ -55,15 +56,15 @@ public static void save(File directory, KeyPair keyPair) throws Exception {
X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(
publicKey.getEncoded());
FileOutputStream out = new FileOutputStream(directory + "/public.key");
out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded())
out.write(Base64.getEncoder().encodeToString(publicSpec.getEncoded())
.getBytes());
out.close();

// Store the private key.
PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(
privateKey.getEncoded());
out = new FileOutputStream(directory + "/private.key");
out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded())
out.write(Base64.getEncoder().encodeToString(privateSpec.getEncoded())
.getBytes());
out.close();
}
Expand All @@ -84,17 +85,17 @@ public static KeyPair load(File directory) throws Exception {
FileInputStream in = new FileInputStream(directory + "/public.key");
byte[] encodedPublicKey = new byte[(int) publicKeyFile.length()];
in.read(encodedPublicKey);
encodedPublicKey = DatatypeConverter.parseBase64Binary(new String(
encodedPublicKey));
encodedPublicKey = Base64.getEncoder().encodeToString(new String(
encodedPublicKey).getBytes()).getBytes();
in.close();

// Read the private key file.
File privateKeyFile = new File(directory + "/private.key");
in = new FileInputStream(directory + "/private.key");
byte[] encodedPrivateKey = new byte[(int) privateKeyFile.length()];
in.read(encodedPrivateKey);
encodedPrivateKey = DatatypeConverter.parseBase64Binary(new String(
encodedPrivateKey));
encodedPrivateKey = Base64.getEncoder().encodeToString(new String(
encodedPrivateKey).getBytes()).getBytes();
in.close();

// Instantiate and return the key pair.
Expand Down

0 comments on commit 569bd8d

Please sign in to comment.