尝试使用Java将加密的AES数据存储到mySQL数据库中 [英] Trying to store encrypted AES data into mySQL database using Java

查看:67
本文介绍了尝试使用Java将加密的AES数据存储到mySQL数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字节数组存储到包含加密的密码"的mySQL中.我已经尝试过同时使用Blob和varbinary数据类型,但是当我提取加密的数据时,由于存储的字节数组与我开始使用的字节数组不同,因此似乎无法正确解密.

I'm trying to store a byte array into mySQL which contains encrypted 'password'. I've tried both using both Blob and varbinary datatype, but when I extract the encrypted data, it doesn't seem to decrypted correctly as the stored byte array is not the same as the one I started with.

下面用于加密/解密的代码

The code below for the encrypt/decryption

   public byte[] encrypt(String password){
    byte[] encrypted = null;
    try {
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);
        encrypted = cipher.doFinal(password.getBytes());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return encrypted;
}

public String decrypt(byte[] encrypted){
    String decrypted = null;
    try {
        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        decrypted = new String(cipher.doFinal(encrypted));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return decrypted;
}

我已经打印出字节数组以查看是否存在差异,正如您所看到的,在过渡到数据库的过程中它确实发生了变化,我不确定如何克服这个问题

I've printed out the byte array to see if there was the difference, as you can see during the transition into the database it does change and I'm not sure how to overcome this problem

字节数组的输出(顶部是原始字节数组,底部是来自数据库)

Output of byte array (Top is original byte array, bottom is from the database)

84-48-4282-15-60-21-38-41944477106182
916664495599545657505332

请澄清一下,如果我尝试使用数据库字节数组解密,则会收到此错误:

Just to clarify, if I try to decrypt using database byte array I recieve this error:

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decryption with padded cipher

也如评论部分所述,我尝试将其转换为字符串并将其存储在DB中,然后将其解密,但是遇到相同的错误.

Also as mentioned in the comments section, I've tried converted it into a string and and storing it in DB then decrypt it but I get the same error.

我什至尝试将哈希与SHA-256结合使用,这与我检索的字节数组与原始字节数组完全不同

I've even tried using Hashing with SHA-256 and it's the same thing the byte array that I'm retrieving is completely different to what it was originally

我正在使用的变量

byte[] pa = p.hashPass("Hello World");
byte[] dbp = null;

这是我用来存储/获取数据的语句

This is the statement I'm using to store/get the data

Statement stmt = conn.createStatement();
stmt.execute("INSERT INTO staffaccounts(`ID`, `UserName`, `Password`, `Salt`) VALUES (NULL, 'admin', '"+pa+"', '')");
ResultSet rs = stmt.executeQuery("SELECT * FROM staffaccounts");
rs.next();
dbp = rs.getBytes("password");

推荐答案

此处的问题是您要插入 byte [].toString()返回的值,而不是字节数组的内容.

The problem here is that you are inserting the value returned by byte[].toString(), which is not the content of the byte array.

您应该通过 PreparedStatement中的位置参数来执行此操作. 从不将值连接到SQL语句中.

You should be doing this via positional parameters in a PreparedStatement. Never concatenate values into an SQL statement.

这篇关于尝试使用Java将加密的AES数据存储到mySQL数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆