I wish to encrypt credit card numbers one by one using asymmetric encryption on the command line. My current approach is this…


/usr/bin/openssl rsautl -encrypt -inkey 'myKey.pub' -pubin | /usr/bin/openssl enc -base64


openssl enc -base64 -d | openssl rsautl -decrypt -inkey myKey.pem

It is my understanding that this is secure, even when using the same key for each number, because rsautl introduces randomization to prevent finding patterns in the cyphertext.

Am I correct, and is the approach therefore secure?


As long as you use a secure padding mode (i.e. -pkcs or -oaep, not -raw). The default padding mode for openssl rsautl is -pkcs (i.e. PKCS#1 v1.5), so you should be OK. That said, OAEP is recommended over PKCS#1 v1.5 padding, so you might want to use the -oaep switch.

  • 2
    $\begingroup$ Just making this comment because it wasn't specified in the question - a 2048-bit key should be used as a minimum (but I'd go even higher if I were encrypting something as sensitive as credit card numbers). $\endgroup$ – hunter Nov 7 '13 at 17:30
  • $\begingroup$ @hunter The actual key management is not specified. The key management procedures around the private key are very important. Key size is important, but it is only a very small part of the key management. Then again, those would be more at place at security.stackoverflow.com. For credit card numbers I would certainly first check the requirements of the credit card companies themselves. $\endgroup$ – Maarten Bodewes Nov 7 '13 at 19:03
  • $\begingroup$ @owlstead - yup, key size is only a small part of key management - but one that often gets overlooked with RSA - I'm amazed at the number of people still using 1024-bit keys in the wild. Thought it was worthwhile making a PSA. $\endgroup$ – hunter Nov 7 '13 at 19:13
  • $\begingroup$ Great! We use a 4096 key with the public key in the codebase, and the private key on our local-network payment server. To decrypt the log, we login to the payment server and paste the cyphertext into openssl's stdin. We intend to change the key every couple years. $\endgroup$ – cmc Nov 11 '13 at 10:48
  • 2
    $\begingroup$ The phrase "We intend to change the key" implies imprecision. You really should be creating a full key management lifecycle policy to comply with PCI 3.5 and 3.6, something that specifies how you will generate, distribute, store, use, retire, and destroy the keys, how long each of those states last, plans if they're compromised, etc. See pcisecuritystandards.org/documents/pci_dss_v2.pdf for the requirement, and csrc.nist.gov for advice. nvlpubs.nist.gov/nistpubs/SpecialPublications/… deals directly with creating a key management strategy. $\endgroup$ – John Deters Nov 11 '13 at 16:55

Be aware that your solution will touch much more than cryptography. Your command shell, the account it runs on, the swap file, the whole machine falls under the purview of PCI DSS regulation and auditing.

If you can avoid storing or even handling the number, so much the better.

  • $\begingroup$ We are PCI certified so that's fine. But I agree it shouldn't be taken lightly; we decided payment processing is a core competency for us, because of the market edge it gives us in responding to customer needs combined with a software customization framework. $\endgroup$ – cmc Nov 11 '13 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.