开发者社区 问答 正文

将Aes256 CBC JAVA加密转换为bash

是否有可能将下面的代码转换为bash。我已经编写了一个测试程序,其中正在用Java加密文本。但是我想在bash中使用相同的逻辑来加密数据。是否可以实现这一点

byte[] ivBytes;
        String password="abc123";
       password = createSHA1(password);
        SecureRandom random = new SecureRandom();
        byte bytes[] = new byte[10];
        random.nextBytes(bytes);
        byte[] saltBytes = bytes;
        SecretKeyFactory factory = null;

        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

        PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 2, 128);
        SecretKey secretKey = factory.generateSecret(spec);
        SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

        //encrypting the word
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        AlgorithmParameters params = cipher.getParameters();
        ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
        byte[] encryptedTextBytes = cipher.doFinal(attribute.getBytes("UTF-8"));
        //prepend salt and vi
        byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length];
        System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length);
        System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length);
        System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length, encryptedTextBytes.length);
        String encodedString =  new Base64().encodeToString(buffer);

展开
收起
几许相思几点泪 2019-12-10 19:25:29 681 分享 版权
阿里云 AI 助理回答

在Bash中直接实现与Java等高级语言相同的加密逻辑是具有挑战性的,因为Bash并不直接支持复杂的加密算法和类库。但是,你可以通过调用外部命令行工具(如openssl)来间接实现类似的功能。下面是一个基于你提供的Java代码逻辑,在Bash中使用openssl进行AES-256-CBC加密的示例。注意,这里使用了PBKDF2进行密钥派生,并且假设你已经有了一个方法来创建SHA-1哈希(虽然通常不推荐SHA-1用于密码处理,但为了匹配你的需求,这里仍然展示如何做)。

首先,你需要确保你的系统上安装了openssl并且版本足够新以支持必要的功能。

步骤 1: 创建SHA-1哈希 (如果需要的话)

如果你没有现成的bash函数或命令来生成SHA-1哈希,可以使用以下命令:

password="abc123"
password_sha1=$(echo -n "$password" | sha1sum | awk '{print $1}')

步骤 2: 生成随机盐

在Bash中生成随机盐比较直接:

salt=$(head /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)

步骤 3: 使用PBKDF2派生密钥

遗憾的是,Bash本身不支持PBKDF2,但你可以编写一个脚本调用其他语言编写的工具或直接使用支持此功能的系统命令(如果有的话)。不过,对于演示目的,我们假设这部分已经完成或者使用简单的替代方案。

步骤 4: 加密数据

接下来,使用openssl进行AES-256-CBC加密,包括盐和IV的处理。由于Bash不直接支持复杂的数据结构操作,我们将简化流程,直接使用openssl生成IV并进行加密。

attribute="要加密的数据"
# 注意:这里的密钥长度应为32字节,对应AES-256
key_from_password=$(echo -n "$password_sha1" | head -c 32) # 这里仅作为示例,实际应用中应该使用PBKDF2

# 使用openssl进行加密,同时获取IV
encrypted=$(echo -n "$attribute" | openssl enc -aes-256-cbc -S "$salt" -iv "$(openssl rand -hex 16)" -K "$key_from_password" -p)

# 输出包含盐、IV和加密文本的信息
echo "Salt: $salt"
echo "Encrypted with IV: $(openssl enc -aes-256-cbc -S "$salt" -iv "$(openssl rand -hex 16)" -K "$key_from_password" -p | grep 'iv=')"
echo "Encoded String: $(echo "$encrypted" | base64)"

请注意,上述示例中使用SHA-1哈希和简化的方法生成密钥仅作为概念演示,实际应用中应当使用更安全的密钥派生函数,比如通过外部程序或脚本调用支持PBKDF2的工具。此外,直接在命令行中处理敏感信息时要格外小心,避免泄露。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答