Java加密的字符串不能存储在数据库中 [英] java encrypted string cannot be stored in DB

查看:117
本文介绍了Java加密的字符串不能存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用javax.crypto库加密字符串并将其存储在数据库(Oracle)中。稍后,我将需要解密此字符串,因此需要双向算法。

Trying to use the javax.crypto library to encrypt a string and store it in the database (Oracle). I will need to decrypt this string later, so I need a two-way algorithm.

问题是数据库似乎不接受该方法创建的某些加密字符。我们介于将数据库迁移到新服务器之间。旧数据库使用US7ASCII字符集,而新数据库使用AL32UTF8。当我将加密的字符串放入数据库中时,数据库只是将它们转换为US7ASCII数据库中的问号(?)。似乎可以很好地存储在AL32UTF8数据库中。

The problem is the database doesn't seem to accept some of the encrypted characters the method creates. We are in between migrating our databases to a new server. The old databases use US7ASCII charset while the new databases use AL32UTF8. When I go to put the encrypted string in the database, the database just converts them to question marks (?) in the US7ASCII databases. It appears to store just fine in the AL32UTF8 database.

因此,我必须使它具有交叉兼容性。我尝试使用getBytes()方法向其发送不同的StandardCharsets值,但这似乎无济于事。也许我想念一些东西。我可以通过任何方式获得想要的结果吗?

So, I have to make this cross-compatible. I've tried sending it different StandardCharsets values when using the getBytes() method, but it doesn't seem to help. Maybe I'm missing something. Any way I can get the desired result doing it differently?

这是我生成密文的代码。从StackOverflow上的另一篇文章中修改

Here is my code to generate the cipher text. Modded from another post on StackOverflow

import java.io.PrintStream;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class test
{

    public static void main(String[] args)
    throws Exception
    {
        //byte[] encryptionKey = "Es6XYPkgCV75J95Y".getBytes(StandardCharsets.UTF_8);
        byte[] encryptionKey = "Es6XYPkgCV75J95Y".getBytes(StandardCharsets.ISO_8859_1);
        //byte[] plainText = args[0].getBytes(StandardCharsets.UTF_8);
        byte[] plainText = args[0].getBytes(StandardCharsets.ISO_8859_1);
        MyCrypto aes = new MyCrypto(encryptionKey);
        byte[] cipherText = aes.encrypt(plainText);
        byte[] decryptedCipherText = aes.decrypt(cipherText);

        System.out.println(new String(plainText));
        System.out.println(new String(cipherText));
        System.out.println(new String(decryptedCipherText));
    }

}

class MyCrypto
{
    private byte[] key;

    private static final String ALGORITHM = "AES";

    public MyCrypto(byte[] key)
    {
        this.key = key;
    }

    /**
     * Encrypts the given plain text
     *
     * @param plainText The plain text to encrypt
     */
    public byte[] encrypt(byte[] plainText) throws Exception
    {
        SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        return cipher.doFinal(plainText);
    }

    /**
     * Decrypts the given byte array
     *
     * @param cipherText The data to decrypt
     */
    public byte[] decrypt(byte[] cipherText) throws Exception
    {
        SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);

        return cipher.doFinal(cipherText);
    }
}


推荐答案

何时您加密数据,将其转换为二进制数据。听起来像是您试图将其存储为字符数据,这是个坏主意。

When you encrypt data you are turning it into binary data. It sounds like you are trying to store it as character data, which is a bad idea.

您确实有两个选择;


  1. 使用二进制文本格式(例如Base64)对加密的二进制数据进行编码。然后,您可以将编码后的数据存储为字符数据。当您要检索它时,可以在解密之前读取字符数据并解码(文本到二进制)。

  1. Encode the encrypted binary data using a binary-to-text scheme such as Base64. You can then store the encoded data as character data. When you want to retrieve it you read the character data and decode (text to binary) before decrypting.

将加密的二进制数据存储为二进制(例如, BLOB)。

Store the encrypted binary data as binary (for example as a BLOB).

这篇关于Java加密的字符串不能存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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