如何从 Cassandra 中的加密 CDC 中读取

How to read from encrypted CDC in Cassandra

我们已经为 Cassandra DSE 中的所有表实施了 TDE。我们使用 AES/ECB/PKCS5Padding / 128 作为密码算法生成了系统密钥。

我们还为几个需要 cdc 捕获的表启用了 cdc。由于为表启用了 TDE,因此 cdc 日志也被加密。

我们需要将 cdc 捕获推送到 kafka 主题。我们尝试使用 system_key 文件中自动生成的 system_key 来解密文件。

AES/ECB/PKCS5Padding:128:(键)

但我们得到 java.security.InvalidKeyException: 非法密钥大小或默认参数

能否告知此密钥是否可用于解密 cdc 日志或提出任何解决方案。

下面是我们用来解密的片段。

public class EncryptDecrypt {

public static String encrypt(String input, String key) {
    byte[] crypted = null;
    try {

        SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");

        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skey);
        crypted = cipher.doFinal(input.getBytes());
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();

    return new String(encoder.encodeToString(crypted));
}

public static String decrypt(String input, String key) {
    byte[] output = null;
    try {
        java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
        SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");          
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skey);
        output = cipher.doFinal(decoder.decode(input));
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    return new String(output);
}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    String key = "qhk9gDtvTUlLW9dnh/UMaw==";
    String data = "ABC";

    System.out.println(EncryptDecrypt.encrypt(data, key));
    System.out.println(EncryptDecrypt.decrypt(EncryptDecrypt.encrypt(data, key), key));
}
}

system_key 文件不用于直接加密数据,而是用于加密存储在 dse_system.encrypted_keys 中的实际加密密钥。这些键是为 algorithm/strength 的每个组合生成的。有关详细信息,请参阅 documentation