开发者

AES encryption - password , salt not resolved? [closed]

开发者 https://www.devze.com 2022-12-12 11:47 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_如何学C Closed 10 years ago.

i get the error " password , salt not resolved ". any suggestions ?

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"));


    }
}


Declare and initialize the salt and password variables.

For example, if you pass the password and salt (as a 16-digit hexadecimal number) as the first two arguments to the program when you launch it, it might look like this:

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);
}

Cryptography is extremely difficult in itself. Trying to get familiar with it and a new language at the same is only compounding the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消