相关疑难解决方法(0)

Java安全:非法密钥大小或默认参数?

我之前曾问过一个关于这个问题的问题,但它没有得到正确的答案而且无处可去.

所以我已经澄清了关于这个问题的一些细节,我真的很想听听你如何解决这个问题或者我应该尝试什么的想法.

我在我的Linux服务器上安装了Java 1.6.0.12,下面的代码运行得非常完美.

String key = "av45k1pfb024xa3bl359vsb4esortvks74sksr5oy4s5serondry84jsrryuhsr5ys49y5seri5shrdliheuirdygliurguiy5ru";
try {
    Cipher c = Cipher.getInstance("ARCFOUR");

    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "ARCFOUR");
    c.init(Cipher.DECRYPT_MODE, secretKeySpec);

    return new String(c.doFinal(Hex.decodeHex(data.toCharArray())), "UTF-8");

} catch (InvalidKeyException e) {
    throw new CryptoException(e);
}
Run Code Online (Sandbox Code Playgroud)

今天我在我的服务器用户上安装了Java 1.6.0.26,当我尝试运行我的应用程序时,我得到以下异常.我的猜测是它与Java安装配置有关,因为它在第一个工作,但在更高版本中不起作用.

Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
    at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
    at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
    at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
    at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
    at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
    at my.package.Something.decode(RC4Decoder.java:25) ~[my.package.jar:na]
    ... 5 common frames omitted
Run Code Online (Sandbox Code Playgroud)

第25行是: c.init(Cipher.DECRYPT_MODE, secretKeySpec);

注意:
*服务器的1.6.0.12 …

java

394
推荐指数
10
解决办法
39万
查看次数

如何在Java中散列密码?

我需要哈希密码以存储在数据库中.我怎么能用Java做到这一点?

我希望获取纯文本密码,添加随机盐,然后将salt和散列密码存储在数据库中.

然后,当用户想要登录时,我可以获取他们提交的密码,从他们的帐户信息中添加随机盐,哈希并查看它是否等于存储的哈希密码及其帐户信息.

java passwords cryptographic-hash-function

170
推荐指数
10
解决办法
14万
查看次数

AES加密Java无效密钥长度

我正在尝试创建AES加密方法,但出于某种原因,我一直在努力

java.security.InvalidKeyException: Key length not 128/192/256 bits

这是代码:

public static SecretKey getSecretKey(char[] password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException{
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    // NOTE: last argument is the key length, and it is 256
    KeySpec spec = new PBEKeySpec(password, salt, 1024, 256);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
    return(secret);
}


public static byte[] encrypt(char[] password, byte[] salt, String text) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
    SecretKey secret = getSecretKey(password, salt);

    Cipher cipher = …
Run Code Online (Sandbox Code Playgroud)

java encryption aes

9
推荐指数
2
解决办法
4万
查看次数

如何使用 Android 的身份验证标签进行 GCM 加密

我想制作一个通过 GCM 模式与 Android 身份验证标签加密数据的函数。这是我的源代码:

public static byte[] GCMEncrypt(String hexKey, String hexIV, byte[] aad) throws Exception {
        byte[] aKey = hexStringToByteArray(hexKey);
        byte[] aIV = hexStringToByteArray(hexIV);
        Key key = new SecretKeySpec(aKey, "AES");
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(16 * Byte.SIZE, aIV));
        cipher.updateAAD(aad);
        byte[] encrypted = cipher.doFinal(aKey);
        return encrypted;
    }
Run Code Online (Sandbox Code Playgroud)

如何将身份验证标签插入此代码中?

encryption android aes-gcm

2
推荐指数
1
解决办法
4507
查看次数

在Java加密和解密中处理IV和salt

所以我想在一个方法来解密消息,但它不工作,因为我需要做cipher.init(Cipher.ENCRYPT_MODE, secret)之前,我尝试添加new IvParameterSpec(iv)cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));.否则,它只返回一个NullPointerException我想知道是否可以在方法中执行此操作而不是一直写入它.我真的不能想到一个解决方案,这就是为什么我在这里.加密工作正常但不解密.

项目运行: JRE 7

加密代码:

public static String encrypt(String str) {
    try {
        SecureRandom random = new SecureRandom();
        byte[] salt = new byte[16];
        random.nextBytes(salt);

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(str.toCharArray(), salt, 65536, 256);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret); //<--- Need to do this before writing IvPerameterSpec,
        // But I think that it's not possible if …
Run Code Online (Sandbox Code Playgroud)

java encryption nullpointerexception

2
推荐指数
1
解决办法
6021
查看次数

AES加密,InvalidKeyException:不支持的密钥大小:6个字节?

我试图加密字符串如下

public class AES256Cipher {
static byte[] ivBytes = new byte[]{0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76};
static String EncryptionKey = "abc123";

public static byte[] encrypt(String plainText)
        throws java.io.UnsupportedEncodingException,
        NoSuchAlgorithmException,
        NoSuchPaddingException,
        InvalidKeyException,
        InvalidAlgorithmParameterException,
        IllegalBlockSizeException,
        BadPaddingException {
    byte[] keyBytes = EncryptionKey.getBytes("UTF-8");

    AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = null;
    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
    byte[] cipherData = cipher.doFinal(plainText.getBytes("UTF-8"));
    Log.e("cipher", Base64.encodeToString(cipherData, Base64.DEFAULT));
    return cipher.doFinal(plainText.getBytes("UTF-8"));
}
}
Run Code Online (Sandbox Code Playgroud)

我得到了这个例外

java.security.InvalidKeyException: …
Run Code Online (Sandbox Code Playgroud)

java encryption aes

1
推荐指数
2
解决办法
6827
查看次数