【问题标题】:JNCryptor: Why does my call to decryptData not produce the correct result?JNCryptor:为什么我对 decryptData 的调用没有产生正确的结果?
【发布时间】:2017-11-26 11:40:34
【问题描述】:

我需要在项目中使用 JNCryptor 库,所以我首先尝试获取一个非常简单的加密/解密示例来工作。我的程序只是加密一个短字符串,然后解密它并显示结果。问题是我没有得到原始文本。这是运行时的输出:

C:\Java\JNCryptor_Test>java JNCryptorTest
Encrypted text: [B@2cfb4a64
Encrypted text back to plain text: [B@5474c6c

谁能告诉我我做错了什么?

这是我的 JNCryptorTest 类的源代码:

import org.cryptonode.jncryptor.JNCryptor;
import org.cryptonode.jncryptor.AES256JNCryptor;
import org.cryptonode.jncryptor.CryptorException;


public class JNCryptorTest
{
    private static String plaintext = "Hello, World!";
    private static String password = "secretsquirrel";

    public static void main(String[] args)
    {
        AllowAes256BitKeys.fixKeyLength();
        byte[] encrypted = encrypt(plaintext);
        System.out.println("Encrypted text: " + encrypted.toString());
        String decrypted = decrypt(encrypted);
        System.out.println("Encrypted text back to plain text: " + decrypted);
    }

    private static byte[] encrypt(String s)
    {
        JNCryptor cryptor = new AES256JNCryptor();
        try
        {
            return cryptor.encryptData(s.getBytes(), password.toCharArray());
        }
        catch (CryptorException e)
        {
            // Something went wrong
            e.printStackTrace();
            return null;
        }
    }


    private static String decrypt(byte[] msg)
    {
        JNCryptor cryptor = new AES256JNCryptor();
        try
        {
            return (cryptor.decryptData(msg,
                                        password.toCharArray())).toString();
        }
        catch (CryptorException e)
        {
            // Something went wrong
            e.printStackTrace();
            return null;
        }
    }
}

另外,我必须创建类 AllowAes256BitKeys 以允许 256 位密钥。建议将“无限强度管辖文件”安装到 JVM 中,但这在我们的网站上是不可接受的,所以我找到了一种不这样做的方法(参见 Java Security: Illegal key size or default parameters?)。

这是我的类 AllowAes256BitKeys 的源代码:

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


public class AllowAes256BitKeys
{
    // From https://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters

    public static void fixKeyLength()
    {
        String errorString =
            "Unable to manually override 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);
                // Warnings suppressed because CryptoPermissions uses a raw Map
                @SuppressWarnings({"unchecked"})
                Object junk =  // Only need this so we can use @SuppressWarnings
                    ((Map) f.get(allPermissions)).put("*", allPermissionCollection);
//              ((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
    }
}

谢谢

【问题讨论】:

  • 您是否尝试过不使用AllowAes256BitKeys,只使用默认的JNCryptor 来确定问题?
  • 是的,但我无法执行该操作,因为它因无效密钥异常而失败。解决方案是将无限强度权限文件安装到 JVM 中。但是,正如我所说,我们不允许在我们的站点上“破解”JVM。
  • 1.虽然我理解使用不比 128 位密钥更安全的 256 位密钥的愿望,但两者都不能被暴力破解。 256 位的两个合理原因是 128 量子位量子计算机可以以可承受的成本创建的可能性,或者仅仅是因为。 2.任何攻击都将针对密码而不是密钥或应用Rubber-hose cryptanalysis
  • 重点是通过不使用AllowAes256BitKeys 并使用128 位密钥来消除用于调试的变量。

标签: java jncryptor


【解决方案1】:

但是,在一位更有经验的 Java 开发人员的帮助下,我自己解决了这个问题。事实证明,JNCryptor 没有问题;问题是我将解密结果(字节数组)错误地转换为字符串。 toString 方法不是正确的方法;您必须创建一个新的 String 对象并将字节数组传递给它的构造函数。

所以我在解密方法中更改了这一行

return (cryptor.decryptData(msg, password.toCharArray())).toString();

到这里

return (new String(cryptor.decryptData(msg, password.toCharArray())));

然后我得到了正确的结果:

Encrypted text: [B@2cfb4a64
Encrypted text back to plain text: Hello, World!

所以实际上,JNCryptor 一直在正常工作 - 是我的代码显示的结果是问题所在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-23
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多