0

我正在开发一些 Java 软件,它需要使用两个单独的软件在网络连接的两端加密和解密信息。为了缓解这个问题,我有一个类 Cryptographer 来处理数据的加密。截至目前,Controller(连接的一侧)和 Agent(另一侧)都使用此类根据两个程序之间共享的密码生成 SecretKey。

密钥是在 Cryptographer 类的这个函数中生成的:

public SecretKey generateKey(String key) {
    this._paramSpec = new PBEParameterSpec(this.SALT, this.ITERATION_COUNT);
    PBEKeySpec spec = new PBEKeySpec(key.toCharArray());
    SecretKeyFactory fac = null;
    try {
        fac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
        System.err.println("[ERR] Cryptographer could not create a SecretKeyFactory due to an unsupported algorithm.");
    }
    try {
        if (fac == null)
            return null;
        return fac.generateSecret(spec);
    } catch (InvalidKeySpecException ex) {
        System.err.println("[ERR] Cryptographer could not generate a SecretKey due to an invalid Key Specification.");
        ex.printStackTrace();
        return null;
    }
}

加密本身发生在 encrypt 函数中:

public byte[] encrypt(byte[] message) {
    try {
        this._cipher.init(Cipher.ENCRYPT_MODE, this._key, this._paramSpec);
    } catch (InvalidKeyException ex) {
        System.err.println("[ERR] Cryptographer could not encrypt a message because the provided key is invalid.");
        ex.printStackTrace();
        return new byte[0];
    } catch (InvalidAlgorithmParameterException ex) {
        System.err.println("[ERR] Cryptographer could not encrypt a message because the parameters are invalid.");
        ex.printStackTrace();
        return new byte[0];
    }
    try {
        return this._cipher.doFinal(message);
    } catch (IllegalBlockSizeException ex) {
        System.err.println("[ERR] Cryptographer could not encrypt a message due to an illegal block size.");
        ex.printStackTrace();
        return new byte[0];
    } catch (BadPaddingException ex) {
        System.err.println("[ERR] Cryptographer could not encrypt a message due to bad padding.");
        ex.printStackTrace();
        return new byte[0];
    }
}

它被解密函数解密:

public byte[] decrypt(byte[] message) {
    try {
        this._cipher.init(Cipher.DECRYPT_MODE, this._key, this._paramSpec);
    } catch (InvalidKeyException ex) {
        System.err.println("[ERR] Cryptographer could not decrypt a message because the provided key is invalid.");
        return new byte[0];
    } catch (InvalidAlgorithmParameterException ex) {
        System.err.println("[ERR] Cryptographer could not decrypt a message because the parameters are invalid.");
    }
    try {
        return this._cipher.doFinal(message);
    } catch (IllegalBlockSizeException ex) {
        System.err.println("[ERR] Cryptographer could not decrypt a message due to an illegal block size.");
        return new byte[0];
    } catch (BadPaddingException ex) {
        System.err.println("[ERR] Cryptographer could not decrypt a message due to bad padding.");
        return new byte[0];
    }
}

加密似乎工作正常,但是当我尝试在接收端解密序列化对象时,会抛出 InvalidKeyException。比较在控制器和代理上独立生成的密钥表明,尽管它们来自相同的密码,但它们不会生成相同的密钥。

现在,我是 Java 加密的新手,所以我完全有可能在这里做错了什么。似乎我缺少一个随机元素。目标是让连接的每一端从相同的密码生成相同的密钥。那么我在做什么显然是错误的?如果您需要查看更多代码,请告诉我。我很乐意发布它。

4

1 回答 1

0

InvalidKeyException抛出向我表明我将查看密钥在接收端是如何使用的。您是否将其存储在数据库或文件中?您确定它与用于加密数据的编码相同吗?

于 2011-11-11T21:46:06.100 回答