如何找出我的JVM支持什么算法[加密]? [英] How to find out what algorithm [ encryption ] are supported by my JVM?

查看:204
本文介绍了如何找出我的JVM支持什么算法[加密]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jasypt进行加密。这是我的代码:

  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);
}

}

取消注释 setAlgorithm 行,它将抛出异常


org.jasypt.exceptions.EncryptionOperationNotPossibleException
加密提升了一个例外。 A
可能的原因是您正在使用强大的
加密算法,而您尚未安装
安装Java Cryptography Ex
张力(JCE)无限力量
此处的管辖权策略文件Java
虚拟机


api说:


设置要用于
加密的算法将算法设置为
用于加密,如
PBEWithMD5AndDES。



该算法必须由
您的JCE提供程序(如果您指定一个
或默认JVM提供程序
不支持)支持,并且如果支持,则$
还可以指定
的模式和填充,如ALGORITHM / MODE / PADDING。


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



现在,当您注释setAlgorithm时,它将使用默认算法[我想这是md5],它可以正常工作。这意味着我的JVM支持md5。现在,如何找出我的JVM支持的其他加密算法。



谢谢,

解决方案

以下将列出所有提供者和算法支持者。你使用什么版本的Java?除非您使用旧版本,否则应包含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()); (Provider.Service服务:provider.getServices())的
{
System.out.println(Algorithm:+ service.getAlgorithm());
}
}

}
}

编辑:
您不使用javax.crypto包中的标准内容的任何原因?



1)生成 Key 使用

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

2)创建一个密码 p>

  cipher = Cipher.getInstance(algorithm); 

3)用钥匙初始化你的密码

  cipher.init(Cipher.ENCRYPT_MODE,key); 

4)使用



<$ p进行加密$ p> byte [] encrypted = cipher.doFinal(data)


I am using Jasypt for encryption. 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

org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an excep tion. 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

api says:

Sets the algorithm to be used for encryption Sets the algorithm to be used for encryption, like 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.

refer: 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. That means md5 is supported by my JVM. Now, how to find out what other encryption algorithms are supported by my JVM.

Thanks,

解决方案

The following will list all the providers and the algorithms supporter. What version of Java are you using? Unless you're on an old version JCE should be included as standard.

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?

1) Generate a Key using

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

2) Create a Cipher using

cipher = Cipher.getInstance(algorithm);  

3) Init your cipher with the key

cipher.init(Cipher.ENCRYPT_MODE, key);  

4) Do the encrypting with

byte[] encrypted = cipher.doFinal(data)

这篇关于如何找出我的JVM支持什么算法[加密]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆