<p>I'm going to be implementing a <code>PHP/mySQL</code> setup to store credit card information.</p>
<p>It seems like <code>AES_ENCRYPT/AES_DECRYPT</code> is the way to go,</p>
<p><strong>but I'm still confused on one point:</strong></p>
<p><strong>How do I keep the encryption key secure?</strong></p>
<p>Hardwiring it into my PHP scripts (which will live on the same server as the db) seems like a major security hole.</p>
<p><strong>What's the &quot;best practice&quot; solution here?</strong></p>

[{'answer_id': 55647, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 5, 'selected': True, 'text': "<p>You should think long and hard about whether you REALLY need to keep the CC#. If you don't have a great reason, DON'T! Every other week you hear about some company being compromised and CC#'s being stolen. All these companies made a fatal flaw - they kept too much information. Keep the CC# until the transaction clears. After that, delete it.</p>\n\n<p>As far as securing the server, the best course of action is to secure the hardware and use the internal system socket to MySQL, and make sure to block any network access to the MySQL server. Make sure you're using both your system permissions and the MySQL permissions to allow as little access as needed. For some scripts, you might consider write-only authentication. There's really no encryption method that will be foolproof (as you will always need to decrypt, and thus must store the key). This is not to say you shouldn't - you can store your key in one location and if you detect system compromise you can destroy the file and render the data useless.</p>\n"}, {'answer_id': 55690, 'author': 'Eric Hogue', 'author_id': 4137, 'author_profile': 'https://Stackoverflow.com/users/4137', 'pm_score': 0, 'selected': False, 'text': "<p>I agree, but don't the cc if you don't need too. But if you really have too, make sure the file that have it is not accessible on the web. You can write a binary that would return the key. This way it's not store in clear text. But if your server is compromise it's still easy to get it. </p>\n"}, {'answer_id': 64118, 'author': 'longneck', 'author_id': 8250, 'author_profile': 'https://Stackoverflow.com/users/8250', 'pm_score': 0, 'selected': False, 'text': "<p>the security you need depends on your application. for example, if the only time the cc# will be used is when the user is logged in (thin online store type scenario), then you can encrypt the cc# with the a hash of the user's plain-text password, a per-user salt, and a dedicated cc# salt. do not store this value permanently.</p>\n\n<p>since you're not storing this value, the only time you can get this value is when the user enters their password to log in. just make sure you have good session expiration and garbage collection policies in place.</p>\n\n<p>if this situation does not apply to you, please describe your situation in more detail so we can provide a more appropriate answer.</p>\n"}, {'answer_id': 7537788, 'author': 'Joseph David', 'author_id': 961179, 'author_profile': 'https://Stackoverflow.com/users/961179', 'pm_score': 2, 'selected': False, 'text': '<p>MySQL, there is six easy steps you can do to secure your sensitive data. </p>\n\n<p>Step 1: Remove wildcards in the grant tables</p>\n\n<p>Step 2: Require the use of secure passwords</p>\n\n<p>Note: Use the MySQL “--secure-auth” option to prevent the use of older, less secure MySQL password formats.</p>\n\n<p>Step 3: Check the permissions of configuration files</p>\n\n<p>Step 4: Encrypt client-server transmissions</p>\n\n<p>Step 5: Disable remote access</p>\n\n<p>Step 6: Actively monitor the MySQL access log</p>\n\n<p>Security Tools</p>\n'}, {'answer_id': 59839883, 'author': 'Josef Marianek', 'author_id': 3202570, 'author_profile': 'https://Stackoverflow.com/users/3202570', 'pm_score': 0, 'selected': False, 'text': '<p>Put your database files outside computer lets say external hdd and keep it at safe place. Works only if you can develop this project at only place where this external drive is placed :) \nOr you can at least protect those files using file system encryption tools like\n<a href="https://itsfoss.com/password-protect-folder-linux/" rel="nofollow noreferrer">https://itsfoss.com/password-protect-folder-linux/</a></p>\n\n<p>In case of production environment I agree with Kyle Cronin.</p>\n'}]
['https://Stackoverflow.com/questions/55643', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3578/']