2009-11-19 188 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); 
} 

密码本身就非常困难。试图熟悉它一个新的语言在同一个只是复合问题。