Trying to Store SecretKey, Encrypted Password in MySQL through JDBC
<p>I'm trying to create my own little program for fun that allows the user to create a username/password which will then be encrypted using AES, and then store the username, encrypted password, and the key in a MySQL database. My future plan is to allow the user to "log in" by retrieving this information from the database and decrypting it to check the login information against the database.</p>

<p>Currently, I've got all of the encryption/decryption functionality working in Java. I've tested it using a variety of inputs and I know the methods all work correctly. The issue I'm running into involves storing this information in a MySQL database. I'm able to connect to the database and execute SELECT queries just fine, but when I go to store the information using an INSERT statement, I'm getting errors.</p>

<p>Here is the setup of my MySQL database:</p>

<pre><code>mysql&gt; SHOW COLUMNS FROM Accounts;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| username | varchar(45) | NO   | UNI | NULL    |                |
| password | blob        | NO   |     | NULL    |                |
| key      | blob        | NO   |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
</code></pre>

<p>Here is the code in question:</p>

<pre><code>public static void main(String[] args) throws Exception {

    String plainText = "test"; 
    SecretKey secKey = getSecretEncryptionKey(); // working correctly
    byte[] cipherText = encryptText(plainText, secKey); // working correctly
    String decryptedText = decryptText(cipherText, secKey); // working correctly
    byte[] secKey2 = secKey.getEncoded(); // necessary??

    try{  
        Class.forName("com.mysql.jdbc.Driver"); 
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Test?useSSL=false","****","****");  // user/pass commented out
        Statement stmt = con.createStatement();  
        ResultSet rs = stmt.executeQuery("use Test;");  
        stmt = con.createStatement(); 
        stmt.executeUpdate("INSERT INTO Accounts (username, password, key) VALUES ('test1', '"+cipherText+"', '"+secKey2+"');");
        con.close();
    } catch(Exception e){ 
        System.out.println(e);
    }  

}
</code></pre>

<p>The error I get:</p>

<pre><code>com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key) VALUES ('test1', '[B@735b478', '[B@2c9f9fb0')' at line 1
</code></pre>

<p>The schema I use in my database is called "Test." The "use Test" Query does seem to work.</p>

<p>In the example above, "test1" would be the username, cipherText would be the hashed password, and secKey2 would be the key encoded into a byte array.</p>

<p>I've tried doing this both with and without converting the Key to a byte array, since I read it's best to convert the key to a byte array and store as a blob. I've also tried this with the 'password' and 'key' fields as VARCHARs. I've not included the methods for the encrypting/decrypting since I know they work correctly, the issue is storing the information in the database.</p>

<p>Any help would be greatly appreciated!</p>