1

我必须将与另一个数据库的连接详细信息存储在数据库表中,我必须加密这些数据库的密码,并且必须能够通过 SQL 脚本“手动”将数据插入该表......

我需要加密和解密它,因为我的应用程序必须能够使用这些数据并连接到其他数据库,所以 MD5 和类似的没有用..

我想到了 Blowfish、AES 等...但是如果我将密码作为 VARCHAR 存储在数据库中,则解密部分不起作用...所以我将其存储为 BYTE,但如果我这样做,没有人可以编写脚本在表上预加载数据..

也许我在这里遗漏了一些东西......

这是我在表中的注册表定义为 VARCHAR 时使用的代码:

package main;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;


public class Prueba {

    private static final String keyValue = "fd<[;.7e/OC0W!d|";
    private static final String ALG = "Blowfish";

    public static void main(String[] args) {
        String text = "some random text";

        try {
            SecretKeySpec key = new SecretKeySpec(keyValue.getBytes(), ALG);
            Cipher cipher = Cipher.getInstance(ALG);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encryptedBytes = cipher.doFinal(text.getBytes());
            String encrypted = new String(encryptedBytes);

            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] recoveredBytes = cipher.doFinal(encrypted.getBytes());
            String recovered = new String(recoveredBytes);

        } catch (NoSuchAlgorithmException nsa) {
            nsa.printStackTrace();
        } catch (NoSuchPaddingException nspe) {
            nspe.printStackTrace();
        } catch (InvalidKeyException ike) {
            ike.printStackTrace();
        } catch (BadPaddingException bpe) {
            bpe.printStackTrace();
        } catch (IllegalBlockSizeException ibse) {
            ibse.printStackTrace();
        } 
    }


}

我得到了例外:

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
    at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
    at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
    at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA12275)
    at javax.crypto.Cipher.doFinal(DashoA12275)
    at main.Prueba.main(Prueba.java:30)

如果不是:

byte[] recoveredBytes = cipher.doFinal(encrypted.getBytes());

我愿意

byte[] recoveredBytes = cipher.doFinal(encryptedBytes);

我也不例外,但是我必须将密码存储为 byte[] soooo 没有脚本可能......

有任何想法吗?

4

3 回答 3

2

似乎当您将密钥存储为字节时,某处的某些东西会错误地解释它。可能是字符编码问题。

如果要将密钥存储为文本,则可能需要先对其进行 base64 编码。然后,您可以 base64 将文本解码回密钥。

于 2012-10-09T15:27:15.253 回答
2

最后,正如maerics所建议的那样,我以这种方式解决了它:

import org.apache.commons.net.util.Base64;

public class MyCrypto {

    /**
     * Codifica un texto usando Base64.
     * @param texto <code>String</code> texto a codificar.
     * @return <code>String</code> texto codificado.
     */
    public static String encrypt(String texto) {
        return new String(Base64.encodeBase64(texto.getBytes()));
    }

    /**
     * Decodifica un texto usando Base64.
     * @param texto <code>String</code> texto a decodificar.
     * @return <code>String</code> texto decodificado.
     */
    public static String decrypt(String texto) {
        return new String(Base64.decodeBase64(texto.getBytes()));
    }
}
于 2013-01-04T08:59:05.800 回答
0
String encrypted = new BASE64Encoder ().encodeBuffer ( encryptedBytes );            
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] recoveredBytes = cipher.doFinal( new BASE64Decoder ().decodeBuffer (  encrypted ) );
于 2013-01-03T06:05:22.683 回答