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

Rak*_*yal 31 java encryption jvm jasypt

我正在使用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 );
    }

}
Run Code Online (Sandbox Code Playgroud)

取消注释该setAlgorithm行,它将引发异常

org.jasypt.exceptions.EncryptionOperationNotPossibleException:加密引发了异常.可能的原因是您使用的是强加密算法,并且尚未在此Java虚拟机中安装Java Cryptography Ex tension(JCE)Unlimited Strength Jurisdiction Policy Files

api说:

设置用于加密的算法设置用于加密的算法,如PBEWithMD5AndDES.

JCE提供程序必须支持此算法(如果不指定,则为默认JVM提供程序),如果支持,则还可以为其指定模式和填充,如ALGORITHM/MODE/PADDING .

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

现在,当您评论'setAlgorithm'时,它将使用默认算法[我猜它是md5],它将正常工作.这意味着我的JVM支持md5.现在,如何找出我的JVM支持的其他加密算法.

谢谢,

Qwe*_*rky 39

以下将列出所有提供程序和算法支持者.您使用的是哪个版本的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());
            for (Provider.Service service : provider.getServices()) {
                System.out.println("  Algorithm: " + service.getAlgorithm());
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

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

1)生成Key使用

Key key = SecretKeyFactory.getInstance(algorithm).generateSecret(new PBEKeySpec(password.toCharArray()));
Run Code Online (Sandbox Code Playgroud)

2)创建一个Cipher使用

cipher = Cipher.getInstance(algorithm);  
Run Code Online (Sandbox Code Playgroud)

3)用密钥启动你的密码

cipher.init(Cipher.ENCRYPT_MODE, key);  
Run Code Online (Sandbox Code Playgroud)

4)进行加密

byte[] encrypted = cipher.doFinal(data)
Run Code Online (Sandbox Code Playgroud)

  • 而不是:System.out.println("Algorithm:"+ service.getAlgorithm()); 使用System.out.println(""+ service); 并且您将能够看到可用的加密级别.例如,以下表示支持128位AES加密,但不支持256位AES:SunJCE:Cipher.AES - > com.sun.crypto.provider.AESCipher ... SupportedModes = ECB | CBC | PCBC | CTR | CTS | CFB | OFB | CFB8 | CFB16 | CFB24 | CFB32 | CFB40 | CFB48 | CFB56 | CFB64 | OFB8 | OFB16 | OFB24 | OFB32 | OFB40 | OFB48 | OFB56 | OFB64 | CFB72 | CFB80 | CFB88 | CFB96 | CFB104 | CFB112 | CFB120 | CFB128 | OFB72 | OFB80 | OFB88 | OFB96 | OFB104 | OFB112 | OFB120 | OFB128} (4认同)