使用Bouncy Castle提供程序进行AES加密/解密

编程入门 行业动态 更新时间:2024-10-25 14:28:25
本文介绍了使用Bouncy Castle提供程序进行AES加密/解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我使用JDK 5的本机库开发的AES 256加密和解密的实现:

Here is my implementation of a AES 256 encrypt and decrypt, developed with the native library of JDK 5:

public static String encrypt(String key, String toEncrypt) throws Exception { Key skeySpec = generateKeySpec(key); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(toEncrypt.getBytes()); byte[] encryptedValue = Base64.encodeBase64(encrypted); return new String(encryptedValue); } public static String decrypt(String key, String encrypted) throws Exception { Key skeySpec = generateKeySpec(key); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decodedBytes = Base64.decodeBase64(encrypted.getBytes()); byte[] original = cipher.doFinal(decodedBytes); return new String(original); }

我想用Boucy Castle API(Java)实现相同的方法:我搜索了很多东西,测试了很多东西,没有结果……有人可以帮助我吗?

I want to implement the same methods with the Boucy Castle API (Java): I've searched a lot, tested a lot, without results ... can someone help me?

谢谢

推荐答案

您可以使用

Security.addProvider(new BouncyCastleProvider()); Cipher cipher = Cipher.getInstance("AES", "BC");

否则

Cipher cipher = Cipher.getInstance("AES", new BouncyCastleProvider());

也就是说, Cipher.getInstance( AES)使用不安全的电子密码本。您需要加密块链接( Cipher.getInstance( AES / CBC / PKCS5Padding))或计数器( Cipher.getInstance( AES / CTR / NoPadding))模式;它们都是安全的,主要区别在于CBC需要填充,而CTR则不需要。

That said, Cipher.getInstance("AES") uses Electronic Codebook, which is insecure. You either want Cipher Block Chaining (Cipher.getInstance("AES/CBC/PKCS5Padding")) or Counter (Cipher.getInstance("AES/CTR/NoPadding")) modes; they are both secure, the primary difference being that CBC requires padding while CTR does not.

更多推荐

使用Bouncy Castle提供程序进行AES加密/解密

本文发布于:2023-11-17 17:15:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1610425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   Bouncy   Castle   AES

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!