RC2加密Java
我需要一个java应用程序上的数据库密钥加密/解密算法。 我必须使用以前使用c#实现的算法,但是它使用的一些类没有java等价物 这是c代码: 在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
谢谢 这应该能帮到你
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);
}