gpt4 book ai didi

java - AES/CFB/NOPADDING(128 位)带密码加盐

转载 作者:行者123 更新时间:2023-11-29 03:01:35 26 4
gpt4 key购买 nike

我已经尝试运行代码大约 3 天了,但我无法找出我犯的错误。

我正在使用 128 位的 AES/CFB/NOPadding 密码加盐Salt_Len = 8 字节IV_Len = 16 字节

package Firstage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.SecureRandom;
import java.security.spec.KeySpec;
import java.util.Random;
import java.util.Scanner;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class Thealgorithm1
{

static Scanner get = new Scanner(System.in);
private static final String ALGORITHM = "AES";
private static final String ALGORITHM_MODE ="AES/CFB/NoPadding";
private static String password;

public static void encrypt(File inputFile, File outputFile)
throws Exception
{
System.out.println("Enetr passprhase");
password=get.nextLine();

final Random ivspc = new SecureRandom();
byte[] ivspec = new byte[16];
ivspc.nextBytes(ivspec);
IvParameterSpec enciv = new IvParameterSpec(ivspec);

FileOutputStream outputstrm = new FileOutputStream(outputFile);
byte[] outputBytes = doCrypto(Cipher.ENCRYPT_MODE, inputFile,password,enciv);
System.arraycopy(ivspec, 0,outputBytes , 0, 16);
outputstrm.write(outputBytes);
outputstrm.close();
System.out.println("File encrypted successfully!");
}

public static void decrypt(File inputFile, File outputFile)
throws Exception
{

System.out.println("Enter password");
password=get.nextLine();
IvParameterSpec hj = null;
byte[]outpytBytes=doCrypto(Cipher.DECRYPT_MODE, inputFile,password,hj);
FileOutputStream outputstrm = new FileOutputStream(outputFile);
outputstrm.write(outpytBytes);
outputstrm.close();
System.out.println("File decrypted successfully!");
}

private static byte[] doCrypto(int cipherMode, File inputFile,String keyo ,IvParameterSpec ivespec)
throws Exception {
/* Derive the key, given password and salt. */
final Random slt = new SecureRandom();
byte[] salt = new byte[8];
slt.nextBytes(salt);


char[] passkeyo = keyo.toCharArray();
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(passkeyo, salt, 65536, 128);
SecretKey tmp = factory.generateSecret(spec);

SecretKey secret = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);

FileInputStream fylin = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int)inputFile.length()];
fylin.read(inputBytes);
fylin.close();

Cipher cipher = Cipher.getInstance(ALGORITHM_MODE);

byte[] outputBytes;
if(cipherMode==2)
{
IvParameterSpec ivdec = new IvParameterSpec(inputBytes,0,16);
cipher.init(cipherMode, secret,ivdec);
}
else
{
cipher.init(cipherMode, secret, ivespec);
}


if(cipherMode==2)
{
outputBytes = cipher.doFinal(inputBytes, 16,(inputBytes.length-16));
}
else
{
outputBytes=cipher.doFinal(inputBytes);
}


return outputBytes;

}

public static void main(String[] args)
{

File inputFile = new File("C:/temp/File.txt");
File encryptedFile = new File("C:/temp/encryaes.enc");
File decryptedFile = new File("C:/temp/mydr.txt");


try {

Thealgorithm1.encrypt(inputFile, encryptedFile);
Thealgorithm1.decrypt(encryptedFile, decryptedFile);
}
catch (Exception e)
{
e.printStackTrace();
}

}

}

代码正确地加密并解密了它,但问题是它解密的内容不正确,即代码中存在错误。

最佳答案

你有两个问题:

  • 在加密过程中,您将用 IV 覆盖密文的前 16 个字节。一个简单的解决方法是将 IV 写入流而不是使用 System.arraycopy()。变化

    System.arraycopy(ivspec, 0,outputBytes , 0, 16);

    outputstrm.write(ivspec);
  • 您在加密和解密过程中使用了不同的盐,因为您总是会生成新盐。你也应该在IV旁边的密文前面写盐,并在解密时读回它。
    您也可以只使用两者之一:生成一个 16 字节的盐并将其写在密文前面。然后你可以使用静态 IV,因为语义属性是从随机盐中获得的。

关于java - AES/CFB/NOPADDING(128 位)带密码加盐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34514103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com