无法成功将SecretKey保存为字符串

时间:2014-09-07 21:37:39

标签: java encryption secret-key

我正在尝试将密钥保存在XML文件中以便以后解密加密字符串,软件可以加密并通过“Encrypt()”方法返回我,但在尝试使用“Decrypt”时返回错误( )“方法

代码

 private static Cipher ecipher;
private static Cipher dcipher;

public static String[] encrypt(String str) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {
    String Key, res;
    SecretKey key;
    String[] Return = new String[2];

    key = KeyGenerator.getInstance("DES").generateKey();
    ecipher = Cipher.getInstance("DES");
    ecipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] utf8 = str.getBytes("UTF8");
    byte[] enc = ecipher.doFinal(utf8);

    enc = BASE64EncoderStream.encode(enc);
    res = new String(enc);

    //Returning values 0 = Encrypted String 1 = Key For Storage in XML
    Return[0] = res;
    byte[] keyBytes = key.getEncoded(); 
    Key = new String(keyBytes,"UTF8");
    Return[1] = Key;

    return Return;
}

public static String decrypt(String str, String Key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, UnsupportedEncodingException, BadPaddingException {
    SecretKey key = new SecretKeySpec(Key.getBytes("UTF8"), "DES");
    dcipher = Cipher.getInstance("DES");
    dcipher.init(Cipher.DECRYPT_MODE, key);
    byte[] dec = BASE64DecoderStream.decode(str.getBytes());
    byte[] utf8 = dcipher.doFinal(dec);
    return new String(utf8, "UTF8");
}

控制台返回:

run:
Encryped String : EB6uhzsBl08=
Key : �g�uX8p
Sep 07, 2014 6:35:17 PM Software.Software main
SEVERE: null
java.security.InvalidKeyException: Invalid key length: 12 bytes
    at com.sun.crypto.provider.DESCipher.engineGetKeySize(DESCipher.java:373)
    at javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1062)
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1020)
    at javax.crypto.Cipher.implInit(Cipher.java:796)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:859)
    at javax.crypto.Cipher.init(Cipher.java:1229)
    at javax.crypto.Cipher.init(Cipher.java:1166)
    at Software.Encryption.decrypt(Encryption.java:51)
    at Software.Software.main(main.java:30)

BUILD SUCCESSFUL (total time: 1 second)

1 个答案:

答案 0 :(得分:1)

您必须在base64中对密钥进行编码

public static String[] encrypt(String str) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException,
        UnsupportedEncodingException, IllegalBlockSizeException,
        BadPaddingException {
    String Key, res;
    SecretKey key;
    String[] Return = new String[2];

    key = KeyGenerator.getInstance("DES").generateKey();
    ecipher = Cipher.getInstance("DES");
    ecipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] utf8 = str.getBytes("UTF8");
    byte[] enc = ecipher.doFinal(utf8);

    enc = BASE64EncoderStream.encode(enc);
    res = new String(enc);

    // Returning values 0 = Encrypted String 1 = Key For Storage in XML
    Return[0] = res;
    byte[] keyBytes = key.getEncoded();
    Key = new String(BASE64EncoderStream.encode(keyBytes), "UTF8");
    Return[1] = Key;

    return Return;
}

public static String decrypt(String str, String Key)
        throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException,
        UnsupportedEncodingException, BadPaddingException {
    SecretKey key = new SecretKeySpec(BASE64DecoderStream.decode(Key.getBytes("UTF8")), "DES");
    dcipher = Cipher.getInstance("DES");
    dcipher.init(Cipher.DECRYPT_MODE, key);
    byte[] dec = BASE64DecoderStream.decode(str.getBytes());
    byte[] utf8 = dcipher.doFinal(dec);
    return new String(utf8, "UTF8");
}