简体   繁体   English

使用第三方Java库重新实现AES加密,不受美国法律限制

[英]Reimplement AES encryption using third-party Java library without US law limitations

I've implemented AES encryption with certain task-specific parameters using standard Java tools and BouncyCastle provider for specific AES algorithm. 我已经使用标准Java工具和特定于AES算法的BouncyCastle提供程序使用某些特定于任务的参数实现了AES加密。

Here is the code: 这是代码:

private byte[] aesEncryptedInfo(String info) throws UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidParameterSpecException, InvalidAlgorithmParameterException, NoSuchProviderException {
    Security.addProvider(new BouncyCastleProvider());
    SecretKey secret = new SecretKeySpec(CUSTOMLONGSECRETKEY.substring(0, 32).getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
    cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(VECTOR_SECRET_KEY.getBytes()));
    return cipher.doFinal(info.getBytes("UTF-8"));
}

In some environments this code requires special policy files. 在某些环境中,此代码需要特殊的策略文件。 See related question: InvalidKeyException Illegal key size 请参阅相关问题: InvalidKeyException非法密钥大小

My goal is to reimplement it using third-party library, ideally I would use bouncy castle which is already used as provider. 我的目标是使用第三方库重新实现它,理想情况下,我将使用已经用作提供程序的充气城堡。 The library should have no restictions of standard java policy files. 该库不应限制标准Java策略文件。 In other words there should be no restrictions. 换句话说,应该没有限制。

Please suggest in your answers how to reimplement it using BouncyCastle or other third-party library which can work without restrictions mentioned. 请在您的答案中建议如何使用BouncyCastle或其他第三方库来重新实现它,这些库可以在没有提及限制的情况下工作。 Ideally I would see the code :-) 理想情况下,我会看到代码:-)

Thank you very much for reading! 非常感谢您的阅读!

After a delay I now happy to post a solution. 经过一段时间的延迟后,我现在很乐意发布解决方案。 Hope that someone can benefit from it because Bouncy Castle documentation is not filled with a lot of examples :-) 希望有人可以从中受益,因为Bouncy Castle文档中没有很多示例:-)

private byte[] aesEncryptedInfo(String info)
// Creating AES/CBC/PKCS7Padding cipher with specified Secret Key and Initial Vector
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
cipher.init(true, new ParametersWithIV(new KeyParameter(CUSTOMLONGSECRETKEY.getBytes()), VECTOR_SECRET_KEY.getBytes()));

byte[] inputData = info.getBytes("UTF-8");
int outBlockSize = cipher.getOutputSize(inputData.length);
byte[] outputData = new byte[outBlockSize];

int outLength = cipher.processBytes(inputData, 0, inputData.length, outputData, 0);
outLength += cipher.doFinal(outputData, outLength);
if (outLength != outBlockSize) {
    return Arrays.copyOf(outputData, outLength);
}
else {
    return outputData;
}    

} }

By the way I found two differences between Java API and Bouncy Castle API: 1. Bouncy Castle uses composition of objects to create needed cipher. 顺便说一下,我发现Java API和Bouncy Castle API之间有两个区别:1. Bouncy Castle使用对象的组合来创建所需的密码。 While Java API uses string to identify needed cipher. 而Java API使用字符串来标识所需的密码。 2. BC encryption code slightly bigger, while Java API code is more compact. 2. BC加密代码稍大一些,而Java API代码则更为紧凑。

The solution is full replacement for original Java API implementation - the proof is a custom unit test that I made. 该解决方案完全替代了原始Java API实现-证明是我进行的自定义单元测试。

Use the Bouncycastle lightweight crypto API directly , rather than through Java JCE interface. 直接使用Bouncycastle轻量级加密API,而不是通过Java JCE接口使用。 Bouncycastle includes its own crypto API accessible through various classes in org.bouncycastle.* packages. Bouncycastle包含自己的加密API,可通过org.bouncycastle.*包中的各种类进行访问。 It also implements the JCE provider interface to make some of its crypto implementations available through standard JCE classes like Cipher , KeyGenerator , etc. 它还实现了JCE提供者接口,以通过标准JCE类(例如CipherKeyGenerator等)使某些加密实现可用。

The cryptography policy restrictions are enforced by the JCE classes, not by bouncycastle. 加密策略限制由JCE类而不是bouncycastle强制执行。 Therefore if you do not use these classes you'll will not encounter any restrictions. 因此,如果您不使用这些类,则不会遇到任何限制。 On the downside you will sacrifice some portability. 缺点是您会牺牲一些可移植性。 To get started, take a look at the javadocs for the AESEngine class, and the rest of the javadocs for the bouncycastle . 首先,请查看AESEngine类的javadocs,以及bouncycastle的其余javadocs

Why isn't it possible to just add the necessary policy files? 为什么不能仅添加必要的策略文件?

That would be the easiest thing to do. 那将是最容易的事情。 If you live in the US and you export your software to other (maybe "unallowed") countries, you will (theoretically) get trouble either way (including policy files/doing the encryption yourself). 如果您居住在美国,并且将软件出口到其他(也许是“不允许的”)国家/地区,则(理论上)您将在两种方式上都遇到麻烦(包括策略文件/自己进行加密)。

If you live outside the US, why even bother about it, just include the policy files, no one cares. 如果您居住在美国境外,为什么还要为此而烦恼,只需提供政策文件,无人问津。

No option for buying a toolkit? 没有购买工具包的选择? RSA BSAFE RSA BSAFE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM