Java:从Applet为AES256修补客户端安全策略 [英] Java: Patching client side security policy from applet for AES256

查看:315
本文介绍了Java:从Applet为AES256修补客户端安全策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在商业Web应用程序中需要AES256加密/解密。
目前一切都很好,密钥大小为128.这不是令人满意的加密方式,所以我的问题是如何最好地解决这个问题,而不需要用户手动安装任何东西。

I require AES256 encryption/decryption in a commercial web application. Currently everything is good with a key size of 128. This is not satisfactory cryptographically so my problem is how best to get round this issue without requiring the user to install anything manually.

我有来自Oracle的无限权限的jar文件,但我不知道如果在用户的JRE / lib / security目录中替换这些文件将与旧版本兼容。显然我不想破坏用户的JRE。此外,我有写我的JRE安全目录的权限,但我认为某些用户将不具有这些权限。

I have the unlimited jurisdiction jar files from Oracle but I have no idea if replacing these in the user's JRE/lib/security directory will be compatible with older versions. Obviously I don't want to corrupt the user's JRE. Also I have write permission to my JRE security directory but I assume some user's will not have these privileges.

有没有一个简单的方法来解决这个问题,或者我坚持使用弱加密或潜在的问题的步骤?

Is there a simple way around this issue, or am I stuck with either weak encryption or a potentially problematic step for users?

无限制更新 javax.crypto.JceSecurity

@ntoskml你是对的。 getMaxAllowedKeyLength 仍然返回有限的密钥大小,但加密成功,密钥大小为== 256 :)。我将更新我的测试方法并设置密钥大小,如果有强大的加密可用。谢谢

@ntoskml You are correct. getMaxAllowedKeyLength still returns the limited key size but the encryption succeeds with key size == 256 :). I will update my test method and set the key size if strong encryption is available. Thanks

>>> from javax.crypto import Cipher
>>> Cipher.getMaxAllowedKeyLength("AES")
128
>>> from java.lang import Class
>>> c = Class.forName("javax.crypto.JceSecurity")
>>> isRestricted = c.getDeclaredField("isRestricted")
>>> isRestricted.setAccessible(True)
>>> isRestricted.set(None, False)
>>> isRestricted.get(None)
False
>>> Cipher.getMaxAllowedKeyLength("AES")
128
>>> from javax.crypto import KeyGenerator
>>> kge = KeyGenerator.getInstance("AES")
>>> kge.init(256)
>>> aesKey = kgen.generateKey()
>>> c2 = Cipher.getInstance("AES")
>>> c2.init(Cipher.ENCRYPT_MODE, aesKey)
>>> c2.doFinal("test")
array('b', [-81, 99, -61, -51, 93, -42, -68, -28, 107, 59, -109, -98, -25, 127, 37, 23])

重新启动Jython控制台后的测试用例



And the test case after restarting Jython console

>>> # Reflection as above
>>> isRestricted.get(None)
True
>>> kge.init(256)
>>> aesKey = kge.generateKey()
>>> c2.init(Cipher.ENCRYPT_MODE, aesKey)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
        at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1011)
        at javax.crypto.Cipher.implInit(Cipher.java:786)
        at javax.crypto.Cipher.chooseProvider(Cipher.java:849)
        at javax.crypto.Cipher.init(Cipher.java:1213)
        at javax.crypto.Cipher.init(Cipher.java:1153)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)

java.security.InvalidKeyException: java.security.InvalidKeyException: Illegal key size or default parameters

宾果:)感谢分享@ntoskml

Bingo :) Thanks for sharing @ntoskml

推荐答案

strong>编辑:这是一个更新的这个问题的答案:如何避免安装无限力量部署应用程序时的JCE策略文件?

Here's an updated answer to this question: How to avoid installing "Unlimited Strength" JCE policy files when deploying an application?

可以禁用密钥大小只是通过使用几行反射来限制。我们在我们的程序中使用这种方法,需要访问256位加密技术以实现互操作性。

It is possible to disable the key size restrictions simply by using a few lines of reflection. We use this method in our program which needs access to 256-bit cryptography for interoperability purposes.

private static void removeCryptographyRestrictions() {
    if (!isRestrictedCryptography()) {
        return;
    }
    try {
        java.lang.reflect.Field isRestricted;
        try {
            final Class<?> c = Class.forName("javax.crypto.JceSecurity");
            isRestricted = c.getDeclaredField("isRestricted");
        } catch (final ClassNotFoundException e) {
            try {
                // Java 6 has obfuscated JCE classes
                final Class<?> c = Class.forName("javax.crypto.SunJCE_b");
                isRestricted = c.getDeclaredField("g");
            } catch (final ClassNotFoundException e2) {
                throw e;
            }
        }
        isRestricted.setAccessible(true);
        isRestricted.set(null, false);
    } catch (final Throwable e) {
        logger.log(Level.WARNING,
                "Failed to remove cryptography restrictions", e);
    }
}

private static boolean isRestrictedCryptography() {
    return "Java(TM) SE Runtime Environment"
            .equals(System.getProperty("java.runtime.name"));
}

但是,我们的程序不是一个小程序,我不知道applet可以访问反思API。

However, our program is not an applet, and I am not sure whether applets have access to the reflection API.

关于合法性的问题仍然存在。有这个限制的原因。如果您担心,请咨询律师。

The question about legality also remains. There is a reason for that limit. Consult a lawyer if you are concerned.

如果可能,请尝试将其保留到128位密钥。即使考虑到摩尔定律,打破128位AES将需要数十亿年的时间。更长的密钥在现实世界中没有任何好处 - 特别是当密钥来源于密码时,无论何处都有近256位的熵。

If possible, try to keep it to 128-bit keys. Even when taking Moore's law into consideration, breaking 128-bit AES would take billions upon billions of years. Longer keys offer no benefit in the real world – particularly when the keys are derived from passwords, which don't have anywhere near 256 bits of entropy anyway.

这篇关于Java:从Applet为AES256修补客户端安全策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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