0

My scenario is a little different to existing Q&As on this topic and perhaps belongs into more than one category but as the following code is AppleScript / Shell based, I figured this is the place to start.

Scenario
I am working on an authentication method that involves encrypting an authentication password on the client side using an RSA public key that matches my (the server's) private key. A random salt is hashed with the authentication password to reduce the possibility of duplicate encrypted text.
The encrypted string is then passed as a parameter into a PHP page where it is decrypted, the salt discarded and authentication password read.

So far...

set public_key to "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDoq9oD/YlxvkvYcKAd06Pn0nKb4bwGYiXFVkL6LGI3gzDFwxAKjt4AVE3pRHgi0tTPVHkGZRfH723+mF0Fe5Vdq7mGDzKB7EAWzkPLThxIRmvCbnbI3o/OCcRlDuBa7+GIjlnqRoQywg7PTYWXUEls5P/12TC7znhwmjk++9KWOwIDAQAB"

set theData to "This is my auth password."
set salt to generateSalt(random number from 10 to 50)
set dataToEnc to salt & "-" & theData

--set encrypt to do shell script ("openssl rsautl -encrypt -inkey public_key -pubin -in \"" & dataToEnc & "\" -out encrypt.dat")
--log encrypt

on generateSalt(numChars)
    do shell script "generateSalt() {
[[ -c /dev/urandom ]] || { echo 'ERROR: Random source /dev/urandom not available.' >&2; return 1; }
LC_ALL=C tr -cd '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' < /dev/urandom | head -c $1
}
generateSalt " & numChars
end generateSalt

Generating the salt and concatenating the string to it is simple, but this is where I am stuck:

  1. How do I implement the correct encryption script with the hardcoded key defined at the top, INSTEAD OF referencing the .pem file that was generated through RSA gen creation?
  2. How do I store the encrypted outcome into a variable INSTEAD OF a file?

I know there are variations of my questions which have already been answered but after hours of researching I am still no closer and am hopeful that somebody could assist. Please let me know if anything requires clarification.

  • @klanomath I think I misworded it, the secret key I am referring to is simply the content I want encrypted with the public key. The result of which will then be sent to the server where it is decrypted and the so called secret key (content) retried. Apologies for the confusion there. – ProGrammer Nov 25 '17 at 9:58
  • 1
    So basically you are asking „how can I pass the key to OpenSSL from a variable instead from a file (and get the result on stdout)“, correct? – nohillside Nov 25 '17 at 12:38
  • @patrix Yes, that's right, key from variable and result to another variable. – ProGrammer Nov 25 '17 at 19:23
  • I advise you to reword your question more acurately as suggested by @patrix . I think you can't achieve this with openssl because -in and -out need files arguments to get direct access to. – dan Nov 28 '17 at 12:39
  • I advise you to ask your OQ on stackoverflow.com and not on crypto.stackexchange.com . – dan Nov 28 '17 at 12:45

You must log in to answer this question.

Browse other questions tagged .