我需要在项目中使用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
}
}谢谢
发布于 2017-06-23 23:49:45
但是,在一位更有经验的Java开发人员的帮助下,我自己解决了这个问题。事实证明,JNCryptor没有问题;问题是我错误地将解密结果(字节数组)转换为字符串。toString方法不是正确的方法;您必须创建一个新的String对象并将字节数组传递给它的构造函数。
所以我在我的decrypt方法中更改了这一行
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一直工作正常-是我的代码显示了结果,这就是问题所在。
https://stackoverflow.com/questions/44712162
复制相似问题