Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
openssl_在java中使用哈希加密aes 256_Java_Php_Encryption_Openssl_Php Openssl - Fatal编程技术网

openssl_在java中使用哈希加密aes 256

openssl_在java中使用哈希加密aes 256,java,php,encryption,openssl,php-openssl,Java,Php,Encryption,Openssl,Php Openssl,从下面的php加密函数: $data = "1212312121447"; $cipher = "aes-256-ofb"; $secretKey = "aNdRgUjXn2r5u8x/A?D(G+KbPeShVmYp"; $ivLength = openssl_cipher_iv_length($cipher); $keyOfb = substr(hash('sha256', $secretKey, true), 0, 32); $i

从下面的php加密函数:

$data = "1212312121447";
$cipher = "aes-256-ofb";
$secretKey = "aNdRgUjXn2r5u8x/A?D(G+KbPeShVmYp";
$ivLength = openssl_cipher_iv_length($cipher);
$keyOfb = substr(hash('sha256', $secretKey, true), 0, 32);
$ivOfb = substr($keyOfb, 0, $ivLength);
$encryptedOfb = openssl_encrypt($data, $cipher, $keyOfb, OPENSSL_RAW_DATA, $ivOfb);
echo "ofb-encrypted: " . base64_encode($ivOfb . $encryptedOfb);
加密的结果是
MyFTCJx8RPzOx7h8QNxEtQgeiNIRwnrJ+uc0V70=

我试着用Java编写这个函数,如下所示:

public static SecretKeySpec hashKey(String key){
        String keyPass = key;
        SecretKeySpec result = null;
        try{
                MessageDigest md = MessageDigest.getInstance("SHA-256");
                md.update(keyPass.getBytes());
                byte[] AesKeyData = Arrays.copyOfRange(md.digest(), 0, 32);
                SecretKeySpec keySpec = new SecretKeySpec(AesKeyData, "AES");
                result = keySpec;
                
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return result;
    }

public static String encryptedOFB(String inp){
        String result = "";
        String key = "aNdRgUjXn2r5u8x/A?D(G+KbPeShVmYp";
        SecretKeySpec keyHashed = hashKey(key);
        try{
            byte[] initVectorSize = Arrays.copyOfRange(keyHashed.toString().getBytes(), 0, 16);
            Cipher cipher = Cipher.getInstance("AES/OFB/NoPadding");
            
            IvParameterSpec iv = new IvParameterSpec(initVectorSize, 0, cipher.getBlockSize());
            cipher.init(Cipher.ENCRYPT_MODE, keyHashed, iv);
            
            byte[] encrypted = cipher.doFinal(inp.getBytes());
            
            ByteArrayOutputStream conc = new ByteArrayOutputStream();
            conc.write(initVectorSize);
            conc.write(encrypted);
            byte[] concEnc = conc.toByteArray();

            result = new String(Base64.getEncoder().encode(concEnc));
            
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return result;
    }
结果是
amf2yxguy3j5chrvlnwzyumrjnv8ycvlua0o9g=

为什么我的java函数返回与php不同的结果

如何修复java函数以获得与php相同的结果


谢谢。

静脉输液的测定有误。您必须使用
keyHashed.getEncoded()
而不是
keyHashed.toString().getBytes()
。然后得到PHP代码的结果

除此之外,您的密钥派生是不安全的:因为IV是密钥的前16个字节,所以相同的密码也意味着相同的密钥/IV对,这是不安全的。对于密码,最好将可靠的密钥派生函数与随机生成的salt结合使用。IV可以与密钥一起推断,也可以独立随机生成。Salt(或IV)不是秘密的,可以通过密文进行解密,通常是串联的

编码时(例如
inp.getBytes()
),应始终指定编码(例如
StandardCharsets.UTF_8
)。同样地,解码(
新字符串(…,StandardCharsets.UTF_8)
)。否则将使用默认编码,这可能会导致跨平台问题