Java Cipher说无效密钥长度

时间:2017-01-14 23:36:16

标签: java encryption cryptography

嘿,伙计们在尝试了大约一个小时的东西后,我被卡住了。我只是想在java中尝试加密,并且遇到了许多我不明白的错误消息。查看in the javadocs关键需要多长时间才能获得AES,它说56,我自然认为这意味着我必须有56位,所以一个7长字节数组。但它似乎没有用。这是堆栈跟踪:

java.security.InvalidKeyException: Invalid key length: 7 bytes null
    at com.sun.crypto.provider.DESCipher.engineGetKeySize(DESCipher.java:373)
    at javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1067)
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1038)
    at javax.crypto.Cipher.implInit(Cipher.java:805)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
    at javax.crypto.Cipher.init(Cipher.java:1396)
    at javax.crypto.Cipher.init(Cipher.java:1327)
    at com.skrelpoid.secure.Crypt.encrypt(Crypt.java:34)
    at com.skrelpoid.secure.Crypt.main(Crypt.java:95)
Exception in thread "main" java.lang.NullPointerException
    at com.skrelpoid.secure.Crypt.decrypt(Crypt.java:47)
    at com.skrelpoid.secure.Crypt.main(Crypt.java:97)`

这是我的代码:

import java.security.NoSuchAlgorithmException;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

// adapted from http://stackoverflow.com/a/1205272/
public class Crypt {
    private static byte[] ivBytes;
    private static Cipher cipher;
    private static SecretKeySpec keySpec;
    private static IvParameterSpec ivSpec;
    private static Random random;
    private static long seed;

    private Crypt() {
    }

    private static byte[] ivBytes() {
        random.setSeed(seed);
        byte[] bytes = new byte[7];
        random.nextBytes(bytes);
        return bytes;
    }

    public static String encrypt(String key, String input) {
        prepare(key);
        byte[] inputBytes = input.getBytes();
        try {
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
            byte[] encrypted = new byte[cipher.getOutputSize(inputBytes.length)];
            int enc_len = cipher.update(inputBytes, 0, inputBytes.length, encrypted, 0);
            enc_len += cipher.doFinal(encrypted, enc_len);
            return new String(encrypted);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static String decrypt(String key, String input) {
        prepare(key);
        byte[] inputBytes = input.getBytes();
        try {
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            byte[] decrypted = new byte[cipher.getOutputSize(inputBytes.length)];
            int dec_len = cipher.update(inputBytes, 0, inputBytes.length, decrypted, 0);
            dec_len += cipher.doFinal(decrypted, dec_len);
            return new String(decrypted);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    private static void prepare(String key) {
        byte[] keyBytes = new byte[7];
        byte[] bytesFromKey = key.getBytes();
        int length = keyBytes.length < bytesFromKey.length ? keyBytes.length : bytesFromKey.length;
        System.arraycopy(bytesFromKey, 0, keyBytes, 0, length);
        // if the length is smaller then keyBytes length, there are trailing
        // zeros in the keyBytes. Fill them with the use of the seed;
        if (length < keyBytes.length) {
            random.setSeed(seed);
            byte[] newBytes = new byte[keyBytes.length - length];
            random.nextBytes(newBytes);
            System.arraycopy(newBytes, 0, keyBytes, length, newBytes.length);
        }

        keySpec = new SecretKeySpec(keyBytes, "DES");
        ivSpec = new IvParameterSpec(ivBytes);
        try {
            cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }

    public static void init(String id) {
        seed = id.hashCode();
        random = new Random(seed);
        ivBytes = ivBytes();
    }

    public static final void main(String[] args) {
        init("123456");
        String str = "ILikeTurtles";
        String key = "KEY123";
        String encrypted = encrypt(key, str);
        System.out.println(encrypted);
        System.out.println(decrypt(key, encrypted));
    }
}

如果有人可以帮助我让代码工作,我真的很感激。 提前致谢

1 个答案:

答案 0 :(得分:3)

DES需要8个字节的密钥和8个字节的IV。