java rc4 算法,我应该使用哪种Java加密算法?

本文探讨了在Java中使用Jasypt库进行加密时遇到的问题,包括可用的加密算法列表及其差异。作者通过测试发现,即使在引入BouncyCastle JCE提供者后,也面临选择最安全算法的挑战。AES被认为是最安全的选择,特别是PBEWITHSHA256AND256BITAES-CBC-BC。然而,考虑到不依赖无限强度加密文件的用户需求,文章建议在不使用这些文件的情况下寻找最强加密选项。
摘要由CSDN通过智能技术生成

I'm working on a program that needs to store binary information encrypted at rest. Unfortunately, I can't seem to find a resource that explains which encryption schemes are best for different applications.

Since encryption is complicated and I'm not an expert, I've decided to use a library called Jasypt that wraps Java's built-in encryption functions. To figure out what algorithms are available to me, I wrote a few unit tests.

The first test calls Jasypt's AlgorithmRegistry.getAllPBEAlgorithms() function and lists out all available encryption algorithms:

PBEWITHHMACSHA1ANDAES_128

PBEWITHHMACSHA1ANDAES_256

PBEWITHHMACSHA224ANDAES_128

PBEWITHHMACSHA224ANDAES_256

PBEWITHHMACSHA256ANDAES_128

PBEWITHHMACSHA256ANDAES_256

PBEWITHHMACSHA384ANDAES_128

PBEWITHHMACSHA384ANDAES_256

PBEWITHHMACSHA512ANDAES_128

PBEWITHHMACSHA512ANDAES_256

PBEWITHMD5ANDDES

PBEWITHMD5ANDTRIPLEDES

PBEWITHSHA1ANDDESEDE

PBEWITHSHA1ANDRC2_128

PBEWITHSHA1ANDRC2_40

PBEWITHSHA1ANDRC4_128

PBEWITHSHA1ANDRC4_40

At runtime, Jasypt will throw an EncryptionOperationNotPossibleException if you try to use an algorithm that for some reason isn't supported or violates Java's encryption rules. Interestingly, if I attempt to use each of the 'available' algorithms to encrypt and then decrypt some arbitrary data, and only print out the ones that don't throw that exception, I get this slimmed down list:

PBEWITHMD5ANDDES

PBEWITHSHA1ANDDESEDE

PBEWITHSHA1ANDRC2_128

PBEWITHSHA1ANDRC2_40

PBEWITHSHA1ANDRC4_128

PBEWITHSHA1ANDRC4_40

The list of available algorithms can be expanded by pulling in the BouncyCastle JCE and registering it by executing Security.addProvider(new BouncyCastleProvider()). If I repeat the previous test after doing this, I get a much bigger list of algorithms to choose from:

PBEWITHMD2ANDDES

PBEWITHMD5AND128BITAES-CBC-OPENSSL

PBEWITHMD5AND192BITAES-CBC-OPENSSL

PBEWITHMD5AND256BITAES-CBC-OPENSSL

PBEWITHMD5ANDDES

PBEWITHMD5ANDRC2

PBEWITHSHA1ANDDES

PBEWITHSHA1ANDDESEDE

PBEWITHSHA1ANDRC2

PBEWITHSHA1ANDRC2_128

PBEWITHSHA1ANDRC2_40

PBEWITHSHA1ANDRC4_128

PBEWITHSHA1ANDRC4_40

PBEWITHSHA256AND128BITAES-CBC-BC

PBEWITHSHA256AND192BITAES-CBC-BC

PBEWITHSHA256AND256BITAES-CBC-BC

PBEWITHSHAAND128BITAES-CBC-BC

PBEWITHSHAAND128BITRC2-CBC

PBEWITHSHAAND128BITRC4

PBEWITHSHAAND192BITAES-CBC-BC

PBEWITHSHAAND2-KEYTRIPLEDES-CBC

PBEWITHSHAAND256BITAES-CBC-BC

PBEWITHSHAAND3-KEYTRIPLEDES-CBC

PBEWITHSHAAND40BITRC2-CBC

PBEWITHSHAAND40BITRC4

PBEWITHSHAANDIDEA-CBC

PBEWITHSHAANDTWOFISH-CBC

Unfortunately, now I have no idea which of these many algorithms is most appropriate for my application. I have an inkling that AES is the right way to go, and it looks like PBEWITHSHA256AND256BITAES-CBC-BC is the AES implementation with the longest key length, but I don't know where to go to confirm that suspicion.

Which of these schemes would provide the highest security levels and which have obvious security issues?

EDIT: I want to be able to distribute my code without requiring the end user to install the unlimited cryptography files, as that will almost certainly be beyond the capabilities of not so tech savvy users. What I really want is the strongest encryption that I can get without using the unlimited strength jurisdiction files.

解决方案

First of all you should install the unlimited cryptography files from Oracle. After doing so you should have fewer EncryptionOperationNotPossibleExceptions and higher security levels should become available.

Furthermore, for the highest level encryption possible I would not use JaSypt at all, because it doesn't seem to provide any integrity checking or authentication of ciphertext. For just confidentiality that doesn't seem to matter. But in practice you'd need to check that against your threat model.

If you'd decide to use JaSypt (which I personally dislike) you should probably go for PBEWITHHMACSHA512ANDAES_256 as highest level possibility. Make sure you understand PBKDF2 and work factors (iteration count, e.g. setKeyObtentionIterations.

You don't need Bouncy Castle. AES is considered secure; all Bouncy Castle does - for your example that uses JaSypt - is adding obscure ciphers. In the above list, everything other than AES is less secure than AES.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值