Skip to content

Instantly share code, notes, and snippets.

@itzg
Last active August 25, 2023 21:00
Show Gist options
  • Save itzg/6978c85dba7d73cfb8630832881d49a3 to your computer and use it in GitHub Desktop.
Save itzg/6978c85dba7d73cfb8630832881d49a3 to your computer and use it in GitHub Desktop.
Using ssh-keygen generated id_rsa to encrypt/decrypt text
package app;
import com.sshtools.common.publickey.InvalidPassphraseException;
import com.sshtools.common.publickey.SshKeyUtils;
import com.sshtools.common.ssh.SshException;
import com.sshtools.common.ssh.components.SshKeyPair;
import com.sshtools.common.ssh.components.SshPrivateKey;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
public class TrySshKeyEncryptDecrypt {
public static void main(String[] args)
throws IOException, InvalidPassphraseException, SshException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException {
final Path idRsaFile;
if (args.length > 0) {
idRsaFile = Paths.get(args[0]);
}
else {
idRsaFile = Paths.get(System.getProperty("user.home"), ".ssh", "id_rsa");
}
// throws InvalidPassphraseException is a passphrase was used on private key file
final SshKeyPair keyPair = SshKeyUtils.getPrivateKey(idRsaFile.toFile(), null);
System.out.printf("Loaded private key for fingerprint %s%n", keyPair.getPublicKey().getFingerprint());
final String cipherText = encrypt(keyPair, "this is a test");
System.out.println(cipherText);
final String clearText = decrypt(keyPair, cipherText);
System.out.println(clearText);
}
private static String decrypt(SshKeyPair keyPair, String cipherText)
throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, IOException {
final Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding", "BC");
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivateKey().getJCEPrivateKey());
final ByteArrayInputStream bytesIn = new ByteArrayInputStream(
Base64.getUrlDecoder().decode(cipherText)
);
final CipherInputStream cipherIn = new CipherInputStream(bytesIn, cipher);
return new String(cipherIn.readAllBytes(), StandardCharsets.UTF_8);
}
private static String encrypt(SshKeyPair keyPair, String s)
throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IOException {
final Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublicKey().getJCEPublicKey());
final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
final CipherOutputStream cipherOut = new CipherOutputStream(bytesOut, cipher);
cipherOut.write(s.getBytes(StandardCharsets.UTF_8));
cipherOut.flush();
cipherOut.close();
return Base64.getUrlEncoder().encodeToString(bytesOut.toByteArray());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment