From 569bd8ddfc67810ff11daf86bd1f4138196ebc0c Mon Sep 17 00:00:00 2001 From: muriplz <46825658+muriplz@users.noreply.github.com> Date: Wed, 17 Jan 2024 18:28:03 +0100 Subject: [PATCH] Fix encoding issue --- src/main/java/com/kryeit/votifier/crypto/RSAIO.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/kryeit/votifier/crypto/RSAIO.java b/src/main/java/com/kryeit/votifier/crypto/RSAIO.java index 1e68b20..17ac657 100644 --- a/src/main/java/com/kryeit/votifier/crypto/RSAIO.java +++ b/src/main/java/com/kryeit/votifier/crypto/RSAIO.java @@ -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; @@ -55,7 +56,7 @@ 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(); @@ -63,7 +64,7 @@ public static void save(File directory, KeyPair keyPair) throws Exception { 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(); } @@ -84,8 +85,8 @@ 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. @@ -93,8 +94,8 @@ public static KeyPair load(File directory) throws Exception { 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.