简体   繁体   English

AES_ENCRYPT与MySQL Server 5.5

[英]AES_ENCRYPT with mysql server 5.5

I am trying to insert data in to MySQL in encrypted format using prepared statement. 我试图使用准备好的语句以加密格式将数据插入MySQL。 But I am getting this error. 但我收到此错误。 I think my query is correct. 我认为我的查询是正确的。

    Connection con=null;
    PreparedStatement psmt1=null;

    String tit=request.getParameter("tit");
    String min=request.getParameter("minvalue");
    String max=request.getParameter("maxvalue");
   String disc=request.getParameter("disc");
    System.out.println(tit);
    String [] title=tit.split(":");

   try{
   con=databasecon.getconnection();
  psmt1=con.prepareStatement("insert into medication(tid,titname,minvalue,maxvalue,disc) values(AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'))");

psmt1.setString(1,title[0]);
psmt1.setString(2,title[1]);
psmt1.setString(3,min);
psmt1.setString(4,max);
psmt1.setString(5,disc);
psmt1.executeUpdate();
}
catch(Exception ex)
{
out.println("Error in connection : "+ex);
}

Below is my connection class 下面是我的连接类

package databaseconnection;
import java.sql.*;

public class databasecon
{
    static Connection con;
    public static Connection getconnection()
    {


        try
        {
            Class.forName("com.mysql.jdbc.Driver"); 
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cam","root","root");
        }
        catch(Exception e)
        {
            System.out.println("class error");
        }
        return con;
    }

}

And finally error is 最后的错误是

Error in connection : java.sql.SQLException: 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 'maxvalue,disc) values(AES_ENCRYPT('6', 'key'),AES_ENCRYPT('systolic bp', 'key'),' at line 1

As requested by my fellow developer i am posting my table sturuture 根据我的开发人员的同等要求,我正在发布我的餐桌摆设 下面是我的表def:

I don't know why this is the case, but you need to pass in a byte array as the second parameter to MySQL's AES_ENCRYPT : 我不知道为什么会这样,但是您需要将字节数组作为第二个参数传递给MySQL的AES_ENCRYPT

psmt1 = con.prepareStatement("insert into medication(tid,titname,minvalue,maxvalue,disc) values(AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'))");

byte[] keyBytes = "key".getBytes();
psmt1.setString(1, title[0]);
psmt1.setBytes(2, keyBytes);
psmt1.setString(3, title[1]);
psmt1.setBytes(4, keyBytes);
psmt1.setString(5, min);
psmt1.setBytes(6, keyBytes);
psmt1.setString(7, max);
psmt1.setBytes(8, keyBytes);
psmt1.setString(9, disc);
psmt1.setBytes(10, keyBytes);
psmt1.executeUpdate();

I say that I don't have an explanation because the MySQL documentation implies that both parameters to AES_ENCRYPT are strings. 我说我没有解释,因为MySQL文档暗示AES_ENCRYPT两个参数都是字符串。 I would have made this mistake myself. 我本人会犯这个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM