Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.security.InvalidKeyException:android中的密钥大小或默认参数非法_Java_Android - Fatal编程技术网

java.security.InvalidKeyException:android中的密钥大小或默认参数非法

java.security.InvalidKeyException:android中的密钥大小或默认参数非法,java,android,Java,Android,我犯了以下错误,有点卡住了: 线程“main”中出现异常 我被卡住了,因为我找到的所有答案都在谈论Java加密扩展(JCE),它通常包含在android SDK中。所以我想我的问题不是这个 我一定忘了什么,但我找不到什么。也许我的代码错了(这是我在Java中第一次使用加密技术,我不是专家,下面的代码主要是一些教程的复制粘贴) 我使用此代码对字符串进行加密和解密: public String cryptString(String s) throws NoSuchPaddingException,

我犯了以下错误,有点卡住了: 线程“main”中出现异常

我被卡住了,因为我找到的所有答案都在谈论Java加密扩展(JCE),它通常包含在android SDK中。所以我想我的问题不是这个

我一定忘了什么,但我找不到什么。也许我的代码错了(这是我在Java中第一次使用加密技术,我不是专家,下面的代码主要是一些教程的复制粘贴)

我使用此代码对字符串进行加密和解密:

public String cryptString(String s) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
    byte[] KeyData = this.cryptKey.getBytes();
    SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
    Cipher cipher = Cipher.getInstance("Blowfish");
    cipher.init(Cipher.ENCRYPT_MODE, KS);
    String ret = new String(cipher.doFinal(s.getBytes("UTF-8")));
    return ret;
}

public String decryptString(byte[] s) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    byte[] KeyData = this.cryptKey.getBytes();
    SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
    Cipher cipher = Cipher.getInstance("Blowfish");
    cipher.init(Cipher.DECRYPT_MODE, KS);
    String ret = new String(cipher.doFinal(s));
    return ret;
}
和以下关键点:

private String cryptKey = "qkjll5@2md3gs5Q@FDFqf";

谢谢大家。

私有字符串加密密钥=”qkjll5@2md3gs5Q@FDFqf”

默认情况下,Java仅支持128位加密

128位==16字节==16个字符

因此
cryptKey
不能超过16个字符

如果你想超过16个字符,你必须


由于美国的限制,默认JDK仅支持通过128位密钥进行加密。因此,为了支持256位长密钥的加密,我们必须替换$JAVA_HOME/JAVA-8-oracle/jre/lib/security文件夹中的local_policy.jar和US_export_policy.jar,否则它将给出JAVA.security.InvalidKeyException:非法密钥大小或默认值

这是一个只使用代码的解决方案。无需下载或处理配置文件

这是一个基于反射的解决方案,在Java8上进行了测试

在程序的早期或应用程序启动时调用此方法一次

//进口

import javax.crypto.Cipher;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;
//方法

public static void fixKeyLength() {
    String errorString = "Failed manually overriding key-length permissions.";
    int newMaxKeyLength;
    try {
        if ((newMaxKeyLength = Cipher.getMaxAllowedKeyLength("AES")) < 256) {
            Class c = Class.forName("javax.crypto.CryptoAllPermissionCollection");
            Constructor con = c.getDeclaredConstructor();
            con.setAccessible(true);
            Object allPermissionCollection = con.newInstance();
            Field f = c.getDeclaredField("all_allowed");
            f.setAccessible(true);
            f.setBoolean(allPermissionCollection, true);

            c = Class.forName("javax.crypto.CryptoPermissions");
            con = c.getDeclaredConstructor();
            con.setAccessible(true);
            Object allPermissions = con.newInstance();
            f = c.getDeclaredField("perms");
            f.setAccessible(true);
            ((Map) f.get(allPermissions)).put("*", allPermissionCollection);

            c = Class.forName("javax.crypto.JceSecurityManager");
            f = c.getDeclaredField("defaultPolicy");
            f.setAccessible(true);
            Field mf = Field.class.getDeclaredField("modifiers");
            mf.setAccessible(true);
            mf.setInt(f, f.getModifiers() & ~Modifier.FINAL);
            f.set(null, allPermissions);

            newMaxKeyLength = Cipher.getMaxAllowedKeyLength("AES");
        }
    } catch (Exception e) {
        throw new RuntimeException(errorString, e);
    }
    if (newMaxKeyLength < 256)
        throw new RuntimeException(errorString); // hack failed
}
公共静态void fixKeyLength(){
String errorString=“手动覆盖密钥长度权限失败。”;
int newMaxKeyLength;
试一试{
if((newMaxKeyLength=Cipher.getMaxAllowedKeyLength(“AES”))<256){
c类=Class.forName(“javax.crypto.CryptoAllPermissionCollection”);
构造函数con=c.getDeclaredConstructor();
con.setAccessible(true);
对象allPermissionCollection=con.newInstance();
字段f=c.getDeclaredField(“允许所有”);
f、 setAccessible(true);
f、 setBoolean(allPermissionCollection,true);
c=Class.forName(“javax.crypto.CryptoPermissions”);
con=c.getDeclaredConstructor();
con.setAccessible(true);
Object allPermissions=con.newInstance();
f=c.getDeclaredField(“perms”);
f、 setAccessible(true);
((Map)f.get(allPermissions)).put(“*”,allPermissionCollection);
c=Class.forName(“javax.crypto.JceSecurityManager”);
f=c.getDeclaredField(“defaultPolicy”);
f、 setAccessible(true);
Field mf=Field.class.getDeclaredField(“修饰符”);
mf.setAccessible(true);
mf.setInt(f,f.getModifiers()&~Modifier.FINAL);
f、 设置(null,allPermissions);
newMaxKeyLength=Cipher.getMaxAllowedKeyLength(“AES”);
}
}捕获(例外e){
抛出新的运行时异常(errorString,e);
}
如果(newMaxKeyLength<256)
抛出新的RuntimeException(errorString);//hack失败
}

自Java 8/9以来,已有更新

  • Java 9中包含无限强度管辖权策略文件,默认情况下使用
  • 从Java 8 Update 161开始,Java 8默认为无限强度管辖权策略
  • 从Java8更新151开始,Java 8中包含了无限强度管辖权策略,但默认情况下不使用。要启用它,您需要在
    /jre/lib/security
    (对于JDK)或
    /lib/security
    (对于jre)中编辑java.security文件。取消注释(或包括)行

    crypto.policy=unlimited

    确保使用以管理员身份运行的编辑器编辑文件。 策略更改仅在重新启动JVM后生效

  • Java 8 Update 151之前,您必须下载JCE Unlimited Strength辖区策略文件并替换

    有关更多详细信息,请参阅


    PS:上面的链接指向我的个人博客,其中包含更多详细信息。

    您可以通过将现有JCE JAR替换为无限强度策略JAR来删除最大密钥限制

    对于JAVA 8,请从以下链接下载JCE Jar-

    /usr/libexec/java_home-v在Mac中查找java_home


    将从上述zip文件中提取的local_policy.jarUS_export_policy.jar复制到$JAVA_HOME/jre/lib/security

    您能解释一下原因吗?@CaptainNakou这些都是
    JAVA
    的一部分好的,很有效,谢谢您,先生,希望这在部署到APK后能在手机上起作用。。。“事实证明,Cipher类通常不允许密钥大小超过128位的加密。这背后的明显原因是,一些国家(尽管越来越少)对进口加密软件的许可密钥强度有限制,尽管实际数字128值得怀疑。好消息是:通过使用Sun提供的其他安全策略文件覆盖安全策略文件,您可以轻松地删除该限制。“长话短说,不管它是否是ADK的一部分,您都需要在$java/jre/lib/security中像在任何java应用程序中一样更改它。
    public static void fixKeyLength() {
        String errorString = "Failed manually overriding key-length permissions.";
        int newMaxKeyLength;
        try {
            if ((newMaxKeyLength = Cipher.getMaxAllowedKeyLength("AES")) < 256) {
                Class c = Class.forName("javax.crypto.CryptoAllPermissionCollection");
                Constructor con = c.getDeclaredConstructor();
                con.setAccessible(true);
                Object allPermissionCollection = con.newInstance();
                Field f = c.getDeclaredField("all_allowed");
                f.setAccessible(true);
                f.setBoolean(allPermissionCollection, true);
    
                c = Class.forName("javax.crypto.CryptoPermissions");
                con = c.getDeclaredConstructor();
                con.setAccessible(true);
                Object allPermissions = con.newInstance();
                f = c.getDeclaredField("perms");
                f.setAccessible(true);
                ((Map) f.get(allPermissions)).put("*", allPermissionCollection);
    
                c = Class.forName("javax.crypto.JceSecurityManager");
                f = c.getDeclaredField("defaultPolicy");
                f.setAccessible(true);
                Field mf = Field.class.getDeclaredField("modifiers");
                mf.setAccessible(true);
                mf.setInt(f, f.getModifiers() & ~Modifier.FINAL);
                f.set(null, allPermissions);
    
                newMaxKeyLength = Cipher.getMaxAllowedKeyLength("AES");
            }
        } catch (Exception e) {
            throw new RuntimeException(errorString, e);
        }
        if (newMaxKeyLength < 256)
            throw new RuntimeException(errorString); // hack failed
    }