diff --git a/src/main/java/com/laytonsmith/PureUtilities/Common/RSAEncrypt.java b/src/main/java/com/laytonsmith/PureUtilities/Common/RSAEncrypt.java deleted file mode 100644 index c9f84e2b80..0000000000 --- a/src/main/java/com/laytonsmith/PureUtilities/Common/RSAEncrypt.java +++ /dev/null @@ -1,249 +0,0 @@ -package com.laytonsmith.PureUtilities.Common; - -import java.io.ByteArrayOutputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.security.InvalidKeyException; -import java.security.Key; -import java.security.KeyFactory; -import java.security.KeyPair; -import java.security.KeyPairGenerator; -import java.security.NoSuchAlgorithmException; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.security.interfaces.RSAPublicKey; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.PKCS8EncodedKeySpec; -import java.util.Objects; -import javax.crypto.BadPaddingException; -import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; -import org.apache.commons.codec.binary.Base64; - -/** - * Given a public/private key pair, this class uses RSA to encrypt/decrypt data. - * - *

Keys are stored in standard formats: - *

- * These formats are interoperable with OpenSSH, Node.js crypto, and other standard tools. - */ -public class RSAEncrypt { - - private static final String ALGORITHM = "RSA"; - private static final int KEY_SIZE = 2048; - - /** - * Generates a new RSA key pair. - * - * @param label The label that will be associated with the public key - * (e.g. "user@host") - * @return A new RSAEncrypt instance with both keys - */ - public static RSAEncrypt generateKey(String label) { - KeyPairGenerator keyGen; - try { - keyGen = KeyPairGenerator.getInstance(ALGORITHM); - } catch(NoSuchAlgorithmException ex) { - throw new RuntimeException(ex); - } - keyGen.initialize(KEY_SIZE); - KeyPair key = keyGen.generateKeyPair(); - RSAEncrypt enc = new RSAEncrypt( - privateKeyToPem(key.getPrivate()), - publicKeyToSsh(key.getPublic(), label)); - return enc; - } - - /** - * Encodes a private key as a PKCS#8 PEM string. - */ - private static String privateKeyToPem(PrivateKey key) { - String base64 = Base64.encodeBase64String(key.getEncoded()); - StringBuilder sb = new StringBuilder(); - sb.append("-----BEGIN PRIVATE KEY-----"); - for(int i = 0; i < base64.length(); i++) { - if(i % 64 == 0) { - sb.append(StringUtils.nl()); - } - sb.append(base64.charAt(i)); - } - sb.append(StringUtils.nl()).append("-----END PRIVATE KEY-----").append(StringUtils.nl()); - return sb.toString(); - } - - /** - * Encodes a public key in OpenSSH format: {@code ssh-rsa