www-bjornjohansen-com-hqud3x.txt (12360B)
1 [1]Skip to content 2 3 [2]{bjørn:johansen} 4 5 ☆ Not an expert. Probably wrong. 6 7 Close collapsed 8 9 • [4]Home 10 • [5]About me 11 • [6]Privacy Policy 12 13 Menu expanded 14 15 Encrypt and decrypt a file using SSH keys 16 17 [encrypt] 18 19 If you have someone’s public SSH key, you can use OpenSSL to safely encrypt a 20 file and send it to them over an insecure connection (i.e. the internet). They 21 can then use their private key to decrypt the file you sent. 22 23 If you encrypt/decrypt files or messages on more than a one-off occasion, you 24 should really use GnuPGP as that is a much better suited tool for this kind of 25 operations. But if you already have someone’s public SSH key, it can be 26 convenient to use it, and it is safe. 27 28 There is a limit to the maximum length of a message – i.e. size of a file – 29 that can be encrypted using asymmetric RSA public key encryption keys (which is 30 what SSH keys are). For this reason, we’ll actually generate a 256 bit key to 31 use for symmetric AES encryption and then encrypt/decrypt that symmetric AES 32 key with the asymmetric RSA keys. This is how encrypted connections usually 33 work, by the way. 34 35 Encrypt a file using a public SSH key 36 37 Generate the symmetric key (32 bytes gives us the 256 bit key): 38 39 $ openssl rand -out secret.key 32 40 41 You should only use this key this one time, by the way. If you send something 42 to the recipient at another time, don’t reuse it. 43 44 Encrypt the file you’re sending, using the generated symmetric key: 45 46 $ openssl aes-256-cbc -in secretfile.txt -out secretfile.txt.enc -pass file:secret.key 47 48 In this example secretfile.txt is the unencrypted secret file, and 49 secretfile.txt.enc is the encrypted file. The encrypted file can be named 50 whatever you like. 51 52 Encrypt the symmetric key, using the recipient’s public SSH key: 53 54 $ openssl rsautl -encrypt -oaep -pubin -inkey <(ssh-keygen -e -f recipients-key.pub -m PKCS8) -in secret.key -out secret.key.enc 55 56 Replace recipients-key.pub with the recipient’s public SSH key. 57 58 Delete the unencrypted symmetric key, so you don’t leave it around: 59 60 $ rm secret.key 61 62 Now you can send the encrypted secret file (secretfile.txt.enc) and the 63 encrypted symmetric key (secret.key.enc) to the recipient. It is even safe to 64 upload the files to a public file sharing service and tell the recipient to 65 download them from there. 66 67 Decrypt a file encrypted with a public SSH key 68 69 First decrypt the symmetric.key: 70 71 $ openssl rsautl -decrypt -oaep -inkey ~/.ssh/id_rsa -in secret.key.enc -out secret.key 72 73 The recipient should replace ~/.ssh/id_rsa with the path to their secret key if 74 needed. But this is the path to where it usually is located. 75 76 Now the secret file can be decrypted, using the symmetric key: 77 78 $ openssl aes-256-cbc -d -in secretfile.txt.enc -out secretfile.txt -pass file:secret.key 79 80 Again, here the encrypted file is secretfile.txt.enc and the unencrypted file 81 will be named secretfile.txt 82 83 Posted by[8]Bjørn Johansen[9]January 5, 2017November 18, 2022Posted in[10] 84 SecurityTags:[11]encryption, [12]howto, [13]openssl, [14]security 85 86 Published by Bjørn Johansen 87 88 Bjørn has been a full-time web developer since 2001, and have during those 89 years touched many areas including consulting, training, project management, 90 client support, and DevOps. He has worked with WordPress for more than 16 91 years, and he is a plugin author, core contributor, WordCamp speaker, WordCamp 92 co-organizer and Translation Editor for Norwegian Bokmål. [15] View all posts 93 by Bjørn Johansen 94 95 Post navigation 96 97 [16]Previous Post Previous post: 98 Flexible Content Fields in Field Manager 99 [17]Next Post Next post: 100 Keep the internet healthy – Internet for people, not profit. 101 102 20 Comments 103 104 1. [84ea] bob says: 105 [18]May 10, 2017 at 23:39 106 107 * Why are you generating 192 bytes when only 32 are needed for the AES-256 108 symmetric key? 109 110 * Use OAEP (as PKCS#1 v1.5 is deterministic) when encrypting your symmetric 111 key, otherwise two identical keys will have the same ciphertext. (chosen 112 plaintext attack) 113 114 1. [21e2] Bjørn Johansen says: 115 [19]May 11, 2017 at 20:06 116 117 * I … I … have no other explanation that I must have had temporary 118 brain damage. I mixed up bits and bytes! :-o Well, at least generating 119 1536 bits for the “password” didn’t do any harm :-) 120 121 * You’re absolutely right. PKCS#1 v1.5 should only be used for signing, 122 not for encryption. I’ve updated the commands now. 123 124 Thank you so much for your comment, I really appreciate it! 125 126 2. [5226] [20]Rodrigo Siqueira says: 127 [21]September 2, 2022 at 16:04 128 129 I tried the suggested encryption command (openssl aes-256-cbc) but got 130 the warning result: 131 *** WARNING : deprecated key derivation used. 132 Using -iter or -pbkdf2 would be better. 133 134 2. [1863] guest says: 135 [22]July 30, 2017 at 11:37 136 137 $ openssl rand 32 -out secret.key 138 rand: Use -help for summary. 139 140 1. [1863] guest says: 141 [23]July 30, 2017 at 11:37 142 143 command not working. 144 145 3. [5e78] Stephen Fromm says: 146 [24]August 22, 2017 at 23:00 147 148 “-pass file:secret.key” 149 150 Reading around the web, plus looking at the docs, it seems to me that -pass 151 is not for inputting the key, but rather inputting a password, from which 152 both the key and the IV for CBC are derived. This isn’t good, insofar there 153 seems to be a consensus that OpenSSL’s key derivation isn’t all that good. 154 155 1. [21e2] Bjørn Johansen says: 156 [25]August 22, 2017 at 23:07 157 158 We are using the 256 bit symmetric “key” as the password. The key to 159 the file containing the password is the asymmetric SSH key. 160 161 1. [5e78] Stephen Fromm says: 162 [26]August 23, 2017 at 20:28 163 164 Right. I’m merely noting that the password is not the symmetric 165 key. Rather, OpenSSL uses the password to generate both the actual 166 symmetric key and the IV. (In that sense, the password does not 167 have to be 256 bits, except insofar as it’s probably a good idea 168 for it to have as much entropy as the actual key that will be 169 derived from it.) 170 171 This distinction isn’t entirely unimportant from a practical 172 standpoint, as apparently many people in the security community 173 don’t like OpenSSL’s method for deriving the key from the password. 174 175 2. [0808] Jarvis says: 176 [27]March 7, 2019 at 00:08 177 178 Exactly! That was my first thought when I saw it mentioned as the key 179 used for symmetric encryption. You are absolutely right Stephen. The 180 pass argument is not the symmetric encryption key. It is a password 181 from which key and IV are derived. 182 183 4. [5e78] Stephen Fromm says: 184 [28]August 28, 2017 at 16:12 185 186 I do want to add—don’t take my comment the wrong way. This page was 187 extremely useful to me. There was stuff on StackOverflow, but much of it 188 wasn’t quite as concrete as the solution you posted here. 189 190 1. [21e2] Bjørn Johansen says: 191 [29]September 2, 2017 at 05:51 192 193 Thank you! 194 195 5. [e853] Nidhi says: 196 [30]September 25, 2017 at 08:36 197 198 Here we are encrypting and decrypting a file. What if we need to encrypt 199 and decrypt a password saved in that file instead. Can we do it using the 200 same commands? 201 202 6. [dfc6] [31]Robert R says: 203 [32]February 28, 2018 at 18:27 204 205 Using: 206 openssl rand 32 -out secret.key 207 208 I sometimes got these errors: 209 bad decrypt 210 140625532782232:error:06065064:digital envelope 211 routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:531: 212 213 I did not get those errors if i base64 encode the random string using: 214 openssl rand 32 | base64 -w 0 > secret.key 215 216 (replace -w with -b on BSD/OSX) 217 218 7. [9ba4] Simon says: 219 [33]April 26, 2018 at 15:50 220 221 Thank you for this post! 222 I made a bash script to put this all together and easily encrypt/decrypt 223 files with ssh key: [34]https://github.com/S2-/sshencdec 224 225 8. [2e9b] Andy Gayton says: 226 [35]April 30, 2018 at 19:51 227 228 This is likely a terribly naive question. 229 230 What is the benefit to generating a one-off symmetric password and 231 encrypting that with the target’s public key, vs encrypting the desired 232 payload directly with the target’s public key? 233 234 Thanks! 235 236 1. [21e2] Bjørn Johansen says: 237 [36]April 30, 2018 at 21:19 238 239 Hi Andy 240 241 I tried to explain that in the beginning: 242 243 There is a limit to the maximum length of a message – i.e. size of 244 a file – that can be encrypted using asymmetric RSA public key 245 encryption keys (which is what SSH keys are). 246 247 The problem is that anything we want to encrypt probably is too large 248 to encrypt using asymmetric RSA public key encryption keys. 249 250 1. [2e9b] Andy Gayton says: 251 [37]April 30, 2018 at 22:02 252 253 Thank you for the reply. That makes sense! 254 255 9. [79e7] Olivier Cloirec says: 256 [38]June 12, 2018 at 07:07 257 258 Hi, thanks for the tip! 259 260 I got the following error message with 1.1.0h: 261 “` 262 openssl rand 32 -out secret.key 263 Extra arguments given. 264 rand: Use -help for summary. 265 “` 266 267 The command works when options are before the size: 268 “` 269 openssl rand -out secret.key 32 270 “` 271 272 1. [21e2] Bjørn Johansen says: 273 [39]June 12, 2018 at 11:28 274 275 Yeah, I’ve noticed that OpenSSL started being picky about that lately. 276 Updated the text now. 277 278 Thank you for leaving the comment, Olivier. 279 280 10. [583a] pierre says: 281 [40]April 9, 2019 at 17:40 282 283 Hi Bjørn 284 thank’s for your post ! 285 Realy simple and easy. 286 It can be used to start discover other features in openssl. 287 288 Comments are closed. 289 290 [41]{bjørn:johansen}, [42] Proudly powered by WordPress. [43]Privacy Policy 291 292 References: 293 294 [1] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#content 295 [2] https://www.bjornjohansen.com/ 296 [4] https://www.bjornjohansen.com/ 297 [5] https://www.bjornjohansen.com/about-me 298 [6] https://www.bjornjohansen.com/privacy-policy 299 [8] https://www.bjornjohansen.com/author/bjorn 300 [9] https://www.bjornjohansen.com/encrypt-file-using-ssh-key 301 [10] https://www.bjornjohansen.com/category/security 302 [11] https://www.bjornjohansen.com/tag/encryption 303 [12] https://www.bjornjohansen.com/tag/howto 304 [13] https://www.bjornjohansen.com/tag/openssl 305 [14] https://www.bjornjohansen.com/tag/security-2 306 [15] https://www.bjornjohansen.com/author/bjorn 307 [16] https://www.bjornjohansen.com/field-manager-flexible-content 308 [17] https://www.bjornjohansen.com/support-mozilla 309 [18] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-1256 310 [19] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-1276 311 [20] https://www.inbot.com.br/ 312 [21] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-60127 313 [22] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-2516 314 [23] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-2517 315 [24] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-2882 316 [25] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-2883 317 [26] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-2894 318 [27] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-24144 319 [28] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-2948 320 [29] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-2994 321 [30] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-3212 322 [31] http://nisosgroup.com/ 323 [32] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-5006 324 [33] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-6928 325 [34] https://github.com/S2-/sshencdec 326 [35] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-7079 327 [36] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-7081 328 [37] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-7085 329 [38] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-10189 330 [39] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-10190 331 [40] https://www.bjornjohansen.com/encrypt-file-using-ssh-key#comment-24648 332 [41] https://www.bjornjohansen.com/ 333 [42] https://wordpress.org/ 334 [43] https://www.bjornjohansen.com/privacy-policy