简体   繁体   English

如何找出我的 JVM 支持什么算法 [加密]?

[英]How to find out what algorithm [ encryption ] are supported by my JVM?

I am using Jasypt for encryption.我正在使用 Jasypt 进行加密。 This is my code:这是我的代码:

public class Encryptor {    
    private final static StandardPBEStringEncryptor pbeEncryptor = new StandardPBEStringEncryptor();
    private final static String PASSWORD = "FBL";
    private final static String ALGORITHM = "PBEWithMD5AndTripleDES";

    static{
        pbeEncryptor.setPassword( PASSWORD );
        //pbeEncryptor.setAlgorithm( ALGORITHM );       
    }

    public static String getEncryptedValue( String text ){
        return pbeEncryptor.encrypt( text );
    }

    public static String getDecryptedValue( String text ){
        return pbeEncryptor.decrypt( text );
    }

}

Uncomment the setAlgorithm line and it will throw an exception取消注释setAlgorithm行,它将抛出异常

org.jasypt.exceptions.EncryptionOperationNotPossibleException : Encryption raised an excep tion. org.jasypt.exceptions.EncryptionOperationNotPossibleException :加密引发异常。 A possible cause is you are using strong encryption algorithms and you have not installed the Java Cryptography Ex tension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine一个可能的原因是您正在使用强加密算法,并且您尚未在此 Java 虚拟机中安装 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files

api says: api 说:

Sets the algorithm to be used for encryption Sets the algorithm to be used for encryption, like PBEWithMD5AndDES.设置用于加密的算法 设置用于加密的算法,如 PBEWithMD5AndDES。

This algorithm has to be supported by your JCE provider (if you specify one, or the default JVM provider if you don't) and, if it is supported, you can also specify mode and padding for it, like ALGORITHM/MODE/PADDING.您的 JCE 提供程序必须支持此算法(如果您指定一个,或者默认的 JVM 提供程序,如果您不支持),如果支持,您还可以为其指定模式和填充,如 ALGORITHM/MODE/PADDING .

refer: http://www.jasypt.org/api/jasypt/apidocs/org/jasypt/encryption/pbe/StandardPBEStringEncryptor.html#setAlgorithm%28java.lang.String%29参考: http : //www.jasypt.org/api/jasypt/apidocs/org/jasypt/encryption/pbe/StandardPBEStringEncryptor.html#setAlgorithm%28java.lang.String%29

Now, when you comment 'setAlgorithm' it will use the default Algorithm [ i guess it is md5 ], and it will work fine.现在,当您评论“setAlgorithm”时,它将使用默认算法 [我猜它是 md5],它会正常工作。 That means md5 is supported by my JVM.这意味着我的 JVM 支持 md5。 Now, how to find out what other encryption algorithms are supported by my JVM.现在,如何找出我的 JVM 支持哪些其他加密算法。

Thanks,谢谢,

The following will list all the providers and the algorithms supporter.下面将列出所有提供者和算法支持者。 What version of Java are you using?您使用的是什么版本的 Java? Unless you're on an old version JCE should be included as standard.除非您使用的是旧版本,否则 JCE 应该作为标准包含在内。

import java.security.Provider;
import java.security.Security;

public class SecurityListings {
    public static void main(String[] args) {
        for (Provider provider : Security.getProviders()) {
            System.out.println("Provider: " + provider.getName());
            for (Provider.Service service : provider.getServices()) {
                System.out.println("  Algorithm: " + service.getAlgorithm());
            }
        }

    }
}

Edit: Any reason why you don't use the standard stuff from the javax.crypto package?编辑:为什么不使用 javax.crypto 包中的标准内容?

1) Generate a Key using 1) 使用生成Key

Key key = SecretKeyFactory.getInstance(algorithm).generateSecret(new PBEKeySpec(password.toCharArray()));

2) Create a Cipher using 2)使用创建Cipher

cipher = Cipher.getInstance(algorithm);  

3) Init your cipher with the key 3)用密钥初始化你的密码

cipher.init(Cipher.ENCRYPT_MODE, key);  

4) Do the encrypting with 4)做加密

byte[] encrypted = cipher.doFinal(data)

The Jasypt command line tool now comes with a script for doing this called listAlgorithms.bat for windows and listAlgorithms.sh for Linux.该Jasypt命令行工具现在带有这样做称为脚本listAlgorithms.bat用于Windows和listAlgorithms.sh的Linux版本。

You can find instructions on how to download and use it here: http://www.jasypt.org/cli.html#Listing_algorithms您可以在此处找到有关如何下载和使用它的说明: http : //www.jasypt.org/cli.html#Listing_algorithms

If you don't have it installed already, then you need to install the JCE (Java Cryptography Extension) which provides support for the algorithms.如果您还没有安装它,那么您需要安装 JCE(Java 加密扩展),它为算法提供支持。

You can see how to install here:您可以在此处查看如何安装:

http://download.oracle.com/javase/1.4.2/docs/guide/security/CryptoSpec.html#ProviderInstalling http://download.oracle.com/javase/1.4.2/docs/guide/security/CryptoSpec.html#ProviderInstalling

The library can be found here: http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136007.html该库可以在这里找到: http : //www.oracle.com/technetwork/java/javase/tech/index-jsp-136007.html

I tried the code posted by @Qwerky, but it's not very helpful.我尝试了@Qwerky 发布的代码,但不是很有帮助。 I had added the latest BouncyCastle provider, and the results I got were very confusing.我添加了最新的 BouncyCastle 提供程序,但得到的结果非常令人困惑。 This shows in better detail who's the provider, version, and the algorithm type and name.这更详细地显示了谁是提供者、版本以及算法类型和名称。

for (Provider provider : Security.getProviders()) {
    System.out.println("Provider: " + provider.getName() + " version: " + provider.getVersion());
    for (Provider.Service service : provider.getServices()) {
        System.out.printf("  Type : %-30s  Algorithm: %-30s\n", service.getType(), service.getAlgorithm());
    }
}

There is still a 'pending' question asked by Qwerky: why using Jasypt instead of using javax.crypto? Qwerky 仍然提出了一个“未决”的问题:为什么使用 Jasypt 而不是使用 javax.crypto?

Well, I would recommend using Jasypt as it is a simple way to crypto for beginners and highly configurable for experienced users.好吧,我建议使用 Jasypt,因为它是初学者的一种简单加密方法,并且对于有经验的用户来说是高度可配置的。

With Jasypt, you can start taking benefit of javax.crypto quickly with a little knowledge of JCE and the cryptography.使用 Jasypt,您可以通过对 JCE 和密码学的一点了解,快速开始利用 javax.crypto。 Whether you want to manage user passwords or encrypt/decrypt data, the framework provides a simple abstraction to the question.无论您是要管理用户密码还是加密/解密数据,该框架都为问题提供了一个简单的抽象。

In the same time, the framework exposes all the possibilities of the JCE specification to allow experienced users to be in full control.同时,该框架公开了 JCE 规范的所有可能性,允许有经验的用户完全控制。

In addition to this, Jasypt provides many more features out-of-the-box for well known questions (dealing with sensitive data stored in the database, ...)除此之外,Jasypt 还为众所周知的问题(处理存储在数据库中的敏感数据,...)提供了更多开箱即用的功能

Using Java 8 and above,使用 Java 8 及更高版本,

Stream.of(Security.getProviders()).flatMap(mapper -> Stream.of(mapper.getServices())).flatMap(Set::stream)
                .map(Provider.Service::getAlgorithm).distinct().sorted().forEach(System.out::println);

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

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