2009-11-19 74 views
0

我收到錯誤「密碼,鹽未解決」。有什麼建議麼 ?AES加密 - 密碼,salt沒有解決?

package org.temp2.cod1; 
import java.security.*; 
import java.security.spec.KeySpec; 

import javax.crypto.*; 
import javax.crypto.spec.*; 
import java.io.*; 

public class Code2 { 

public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); 
    KeySpec spec = new PBEKeySpec(password, salt, 1024, 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); 
    AlgorithmParameters params = cipher.getParameters(); 
    byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV(); 
    byte[] ciphertext = cipher.doFinal("Hello, World!".getBytes("UTF-8")); 


    } 
} 

回答

1

聲明和初始化saltpassword變量。

例如,如果你通過密碼和鹽(作爲16位十六進制數)作爲前兩個參數的程序,當您啓動它,它可能是這樣的:

char[] password = args[0].toCharArray(); 
byte[] salt = new byte[8]; 
for (int i = 0; i < 8; ++i) { 
    salt[i] = (byte) Integer.parseInt(args[1].substring(i * 2, i * 2 + 2), 16); 
} 

密碼本身就非常困難。試圖熟悉它一個新的語言在同一個只是複合問題。