Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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
RC2加密Java_Java_Encryption - Fatal编程技术网

RC2加密Java

RC2加密Java,java,encryption,Java,Encryption,我需要一个java应用程序上的数据库密钥加密/解密算法。 我必须使用以前使用c#实现的算法,但是它使用的一些类没有java等价物 这是c代码: 在java中创建等效代码有什么帮助吗?还是其他选择 谢谢 这应该能帮到你 public static String encrypt(byte[] key, byte[] iv, String unencrypted) throws NoSuchAlgorithmException, NoSuchPaddingException, Inva

我需要一个java应用程序上的数据库密钥加密/解密算法。 我必须使用以前使用c#实现的算法,但是它使用的一些类没有java等价物

这是c代码:

在java中创建等效代码有什么帮助吗?还是其他选择


谢谢

这应该能帮到你

public static String encrypt(byte[] key, byte[] iv, String unencrypted) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException{
    RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.length*8, iv);
    Cipher cipher = Cipher.getInstance("RC2/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "RC2"), ivSpec);
    byte[] encrypted = cipher.doFinal(unencrypted.getBytes());
    return DatatypeConverter.printBase64Binary(encrypted);
}

public static String decrypt(byte[] key, byte[] iv, String encrypted) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException{
    RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.length*8, iv);
    Cipher cipher = Cipher.getInstance("RC2/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "RC2"), ivSpec);
    byte[] decrypted = cipher.doFinal(DatatypeConverter.parseBase64Binary(encrypted));

    return new String(decrypted);
}
使用相同的键和IV(代码中的向量),这些方法将生成与您列出的代码兼容的输入/输出

public static String encrypt(byte[] key, byte[] iv, String unencrypted) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException{
    RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.length*8, iv);
    Cipher cipher = Cipher.getInstance("RC2/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "RC2"), ivSpec);
    byte[] encrypted = cipher.doFinal(unencrypted.getBytes());
    return DatatypeConverter.printBase64Binary(encrypted);
}

public static String decrypt(byte[] key, byte[] iv, String encrypted) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException{
    RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.length*8, iv);
    Cipher cipher = Cipher.getInstance("RC2/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "RC2"), ivSpec);
    byte[] decrypted = cipher.doFinal(DatatypeConverter.parseBase64Binary(encrypted));

    return new String(decrypted);
}