0

我需要在项目中使用 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 安全性:非法密钥大小或默认参数?)。

这是我的类 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
    }
}

谢谢

4

1 回答 1

0

但是,在一位更有经验的 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 一直在正常工作——是我的代码显示了问题所在的结果。

于 2017-06-23T15:49:44.993 回答