Last active
April 4, 2022 02:28
-
-
Save TomMaddox/6382304 to your computer and use it in GitHub Desktop.
JuiceSSH Validate Key File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static boolean validateKeyFile(File fileHandle) { | |
| final int fileSizeLimitKB = 8; | |
| if (fileHandle.length() > fileSizeLimitKB * 1024) { | |
| return false; | |
| } | |
| try { | |
| InputStream is = new FileInputStream(fileHandle); | |
| BufferedReader reader = new BufferedReader(new InputStreamReader(is)); | |
| char[] header = new char[37]; | |
| reader.read(header, 0, 37); | |
| reader.close(); | |
| if(String.valueOf(header).contains("-----BEGIN RSA PRIVATE KEY-----")) { | |
| return true; | |
| } | |
| if(String.valueOf(header).contains("-----BEGIN DSA PRIVATE KEY-----")) { | |
| return true; | |
| } | |
| if(String.valueOf(header).contains("-----BEGIN PRIVATE KEY-----")) { | |
| return true; | |
| } | |
| if(String.valueOf(header).contains("-----BEGIN ENCRYPTED PRIVATE KEY-----")) { | |
| return true; | |
| } | |
| } catch (FileNotFoundException e) { | |
| Log.e(TAG, "File not found"); | |
| return false; | |
| } catch (IOException e) { | |
| Log.e(TAG, "Not a textfile"); | |
| return false; | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had been struggling with this import process, as well, not fully grasping what was acceptable to JuiceSSH; which encryption, key strengths, et cetera. A few tweaks should allow users to work around/through the difficulties using "commonly" generated

-----BEGIN OPENSSH PRIVATE KEY-----2048/4096-bit RSA keys (do NOT use 1024-bit, as these are not nearly strong enough, and haven't been in more than a decade). This appears to be the issue encountered most in this thread.SOURCE: JuiceSSH authentication FAQ.
Once you have confirmed the aforementioned settings are correct in your /etc/ssh/sshd_config file (et al), proceed with the following:
mv ~/.ssh/id_rsa ~/.ssh/id_rsa.BAKssh-keygen -p -m PEM -f ~/.ssh/id_rsa(this step may fail if you don't have permissions to overwrite your current private key, so be certain you've performed the previous step!)cat ~/.ssh/id_rsaand migrate this output to JuiceSSH). The app should now be able to automatically import your private keys using Smart Search.I'm sorry this doesn't help those of us trying to use elliptic-curve / EdDSA / ED25519 keys (yet). Hopefully @TomMaddox will be able to hook that up in the not too distant future. Thank you, please drive thru.