ChatGPT解决这个技术问题 Extra ChatGPT

Java 256 位 AES 基于密码的加密

我需要实现 256 位 AES 加密,但我在网上找到的所有示例都使用“KeyGenerator”来生成 256 位密钥,但我想使用自己的密码。如何创建自己的密钥?我尝试将其填充为 256 位,但随后我收到一条错误消息,提示密钥太长。我确实安装了无限权限补丁,所以这不是问题:)

IE。 KeyGenerator 看起来像这样......

// Get the KeyGenerator
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256 bits may not be available

// Generate the secret key specs.
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();

Code taken from here

编辑

我实际上是将密码填充到 256 个字节,而不是位,这太长了。以下是我现在正在使用的一些代码,因为我对此有了更多的经验。

byte[] key = null; // TODO
byte[] input = null; // TODO
byte[] output = null;
SecretKeySpec keySpec = null;
keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
output = cipher.doFinal(input)

您需要自己做的“TODO”位:-)

您能否澄清一下:调用 kgen.init(256) 是否有效?
是的,但这会自动生成一个密钥……但由于我想在两个地方之间加密数据,我需要事先知道密钥,所以我需要指定一个而不是“生成”一个。我可以指定一个适用于 128 位加密的 16 位加密。我尝试了一个 32 位的 256 位加密,但它没有按预期工作。
如果我理解正确,您正在尝试使用预先安排的 256 位密钥,例如指定为字节数组。如果是这样,DarkSquid 使用 SecretKeySpec 的方法应该可以工作。也可以从密码中导出 AES 密钥;如果这就是你所追求的,请告诉我,我会告诉你正确的方法;简单地散列密码并不是最佳做法。
填充数字时要小心,您可能会降低 AES 的安全性。
@erickson:这正是我需要做的(从密码中获取 AES 密钥)。

e
erickson

与接收者共享 password(一个 char[])和 salt(一个 byte[] - 由 SecureRandom 选择的 8 个字节是一个很好的盐 - 不需要保密) -乐队。然后从这些信息中导出一个好的密钥:

/* Derive the key, given password and salt. */
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(password, salt, 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

幻数(可以在某处定义为常量)65536 和 256 分别是密钥派生迭代次数和密钥大小。

密钥派生函数被迭代以需要大量的计算工作,这可以防止攻击者快速尝试许多不同的密码。可以根据可用的计算资源更改迭代计数。

密钥大小可以减少到 128 位,这仍然被认为是“强”加密,但如果发现削弱 AES 的攻击,它不会提供太多安全余量。

与适当的块链接模式一起使用,相同的派生密钥可用于加密许多消息。在 Cipher Block Chaining (CBC) 中,为每条消息生成一个随机初始化向量 (IV),即使明文相同,也会产生不同的密文。 CBC 可能不是您可以使用的最安全的模式(请参阅下面的 AEAD);还有许多其他具有不同安全属性的模式,但它们都使用类似的随机输入。在任何情况下,每个加密操作的输出都是密文初始化向量:

/* Encrypt the message. */
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(StandardCharsets.UTF_8));

存储 ciphertextiv。解密时,SecretKey 以完全相同的方式重新生成,使用具有相同盐和迭代参数的密码。用这个密钥初始化密码与消息一起存储的初始化向量:

/* Decrypt the message, given derived key and initialization vector. */
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
String plaintext = new String(cipher.doFinal(ciphertext), StandardCharsets.UTF_8);
System.out.println(plaintext);

Java 7 包含 API support for AEAD cipher modes,OpenJDK 和 Oracle 发行版中包含的“SunJCE”提供程序从 Java 8 开始实现这些。强烈建议使用其中一种模式代替 CBC;它将保护数据的完整性及其隐私。

带有“非法密钥大小或默认参数”消息的 java.security.InvalidKeyException 表示加密强度受到限制;无限强度管辖权政策文件不在正确的位置。在 JDK 中,它们应该放在 ${jdk}/jre/lib/security

根据问题描述,听起来策略文件没有正确安装。系统可以很容易地拥有多个 Java 运行时;仔细检查以确保使用了正确的位置。


@Nick:阅读 PKCS #5。 PBKDF2 需要盐,这就是为什么基于密码的加密 API 需要它们作为密钥派生的输入。如果没有盐,可以使用字典攻击,从而启用最可能的对称加密密钥的预先计算列表。密码 IV 和密钥派生盐有不同的用途。 IV 允许一个人为多条消息重用相同的密钥。盐可以防止字典对密钥的攻击。
在这种情况下,我会将密文存储在一个字段中,IV 存储在另一个字段中,并将“盐”和“迭代”一起存储在第三个字段中。我会在客户端应用程序中提示用户输入密码,并使用存储的盐和迭代派生密钥。然后用派生密钥和存储的 IV 初始化密码,并解密内容。
我已经将@erickson 的答案实现为一个类:github.com/mrclay/jSecureEdit/tree/master/src/org/mrclay/crypto(PBE 完成工作,PBEStorage 是一个用于将 IV/密文存储在一起的值对象。)
@AndyNuss 此示例用于可逆加密,通常不应用于密码。您可以使用 PBKDF2 密钥派生来安全地“散列”密码。这意味着在上面的示例中,您会将 tmp.getEncoded() 的结果存储为哈希。您还应该存储 salt 和迭代(在此示例中为 65536),以便在有人尝试进行身份验证时重新计算哈希。在这种情况下,每次更改密码时使用加密随机数生成器生成盐。
要运行此代码,请确保您的 JRE 中有正确的 Unlimited Strength Jurisdiction Policy 文件,如 ngs.ac.uk/tools/jcepolicyfiles 中所述
C
Community

考虑使用 Spring Security Crypto 模块

Spring Security Crypto 模块提供对对称加密、密钥生成和密码编码的支持。该代码作为核心模块的一部分分发,但不依赖于任何其他 Spring Security(或 Spring)代码。

它为加密提供了一个简单的抽象,并且似乎符合这里的要求,

“标准”加密方法是使用 PKCS #5 的 PBKDF2(基于密码的密钥派生函数 #2)的 256 位 AES。此方法需要 Java 6。用于生成 SecretKey 的密码应保存在安全的地方,不得共享。如果您的加密数据被泄露,盐用于防止对密钥的字典攻击。还应用了一个 16 字节的随机初始化向量,因此每条加密消息都是唯一的。

查看 internals 会发现类似于 erickson's answer 的结构。

如问题中所述,这还需要 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy(否则您会遇到 InvalidKeyException: Illegal Key Size)。它可用于 Java 6Java 7Java 8

示例用法

import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.crypto.keygen.KeyGenerators;

public class CryptoExample {
    public static void main(String[] args) {
        final String password = "I AM SHERLOCKED";  
        final String salt = KeyGenerators.string().generateKey();
        
        TextEncryptor encryptor = Encryptors.text(password, salt);      
        System.out.println("Salt: \"" + salt + "\"");
        
        String textToEncrypt = "*royal secrets*";
        System.out.println("Original text: \"" + textToEncrypt + "\"");
        
        String encryptedText = encryptor.encrypt(textToEncrypt);
        System.out.println("Encrypted text: \"" + encryptedText + "\"");
        
        // Could reuse encryptor but wanted to show reconstructing TextEncryptor
        TextEncryptor decryptor = Encryptors.text(password, salt);
        String decryptedText = decryptor.decrypt(encryptedText);
        System.out.println("Decrypted text: \"" + decryptedText + "\"");
        
        if(textToEncrypt.equals(decryptedText)) {
            System.out.println("Success: decrypted text matches");
        } else {
            System.out.println("Failed: decrypted text does not match");
        }       
    }
}

和样本输出,

Salt: "feacbc02a3a697b0"
Original text: "*royal secrets*"
Encrypted text: "7c73c5a83fa580b5d6f8208768adc931ef3123291ac8bc335a1277a39d256d9a" 
Decrypted text: "*royal secrets*"
Success: decrypted text matches

您可以在不加载所有 Spring 的情况下使用该模块吗?他们似乎没有提供可供下载的 jar 文件。
@theglauber 是的,您可以在没有 Spring Security 或 Spring 框架的情况下使用该模块。通过查看 pom,唯一的运行时依赖项是 apache commons-logging 1.1.1。您可以使用 pull in the jar with mavendownload it directly from the official binary repo(有关 Spring 二进制文件的更多信息,请参阅 Spring 4 binaries download)。
是否可以将密钥长度设置为 128 位?修改每台 PC 中的安全文件夹对我来说不是一个选项。
@IvanRF 抱歉,看起来不像。 256 硬编码在 source
Spring 实用程序使用的 NULL_IV_GENERATOR 不安全。如果应用程序没有提供 IV,让提供者选择它,并在初始化后查询它。
w
wufoo

在阅读了 erickson 的建议,并从其他几篇帖子和这个示例 here 中收集了我可以收集到的内容后,我尝试使用建议的更改来更新 Doug 的代码。随意编辑以使其更好。

初始化向量不再固定

加密密钥是使用来自 erickson 的代码派生的

使用 SecureRandom() 在 setupEncrypt() 中生成 8 字节盐

解密密钥由加密盐和密码生成

解密密码由解密密钥和初始化向量生成

删除了 hex twiddling 代替 org.apache.commons 编解码器 Hex 例程

一些注意事项:这使用 128 位加密密钥 - java 显然不会开箱即用地进行 256 位加密。实现 256 需要在 java 安装目录中安装一些额外的文件。

另外,我不是加密货币的人。谨慎。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.KeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class Crypto
{
    String mPassword = null;
    public final static int SALT_LEN = 8;
    byte [] mInitVec = null;
    byte [] mSalt = null;
    Cipher mEcipher = null;
    Cipher mDecipher = null;
    private final int KEYLEN_BITS = 128; // see notes below where this is used.
    private final int ITERATIONS = 65536;
    private final int MAX_FILE_BUF = 1024;

    /**
     * create an object with just the passphrase from the user. Don't do anything else yet 
     * @param password
     */
    public Crypto (String password)
    {
        mPassword = password;
    }

    /**
     * return the generated salt for this object
     * @return
     */
    public byte [] getSalt ()
    {
        return (mSalt);
    }

    /**
     * return the initialization vector created from setupEncryption
     * @return
     */
    public byte [] getInitVec ()
    {
        return (mInitVec);
    }

    /**
     * debug/print messages
     * @param msg
     */
    private void Db (String msg)
    {
        System.out.println ("** Crypt ** " + msg);
    }

    /**
     * this must be called after creating the initial Crypto object. It creates a salt of SALT_LEN bytes
     * and generates the salt bytes using secureRandom().  The encryption secret key is created 
     * along with the initialization vectory. The member variable mEcipher is created to be used
     * by the class later on when either creating a CipherOutputStream, or encrypting a buffer
     * to be written to disk.
     *  
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     * @throws NoSuchPaddingException
     * @throws InvalidParameterSpecException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     * @throws InvalidKeyException
     */
    public void setupEncrypt () throws NoSuchAlgorithmException, 
                                                           InvalidKeySpecException, 
                                                           NoSuchPaddingException, 
                                                           InvalidParameterSpecException, 
                                                           IllegalBlockSizeException, 
                                                           BadPaddingException, 
                                                           UnsupportedEncodingException, 
                                                           InvalidKeyException
    {
        SecretKeyFactory factory = null;
        SecretKey tmp = null;

        // crate secureRandom salt and store  as member var for later use
         mSalt = new byte [SALT_LEN];
        SecureRandom rnd = new SecureRandom ();
        rnd.nextBytes (mSalt);
        Db ("generated salt :" + Hex.encodeHexString (mSalt));

        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

        /* Derive the key, given password and salt. 
         * 
         * in order to do 256 bit crypto, you have to muck with the files for Java's "unlimted security"
         * The end user must also install them (not compiled in) so beware. 
         * see here:  http://www.javamex.com/tutorials/cryptography/unrestricted_policy_files.shtml
         */
        KeySpec spec = new PBEKeySpec (mPassword.toCharArray (), mSalt, ITERATIONS, KEYLEN_BITS);
        tmp = factory.generateSecret (spec);
        SecretKey secret = new SecretKeySpec (tmp.getEncoded(), "AES");

        /* Create the Encryption cipher object and store as a member variable
         */
        mEcipher = Cipher.getInstance ("AES/CBC/PKCS5Padding");
        mEcipher.init (Cipher.ENCRYPT_MODE, secret);
        AlgorithmParameters params = mEcipher.getParameters ();

        // get the initialization vectory and store as member var 
        mInitVec = params.getParameterSpec (IvParameterSpec.class).getIV();

        Db ("mInitVec is :" + Hex.encodeHexString (mInitVec));
    }



    /**
     * If a file is being decrypted, we need to know the pasword, the salt and the initialization vector (iv). 
     * We have the password from initializing the class. pass the iv and salt here which is
     * obtained when encrypting the file initially.
     *   
     * @param initvec
     * @param salt
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws InvalidAlgorithmParameterException
     * @throws DecoderException
     */
    public void setupDecrypt (String initvec, String salt) throws NoSuchAlgorithmException, 
                                                                                       InvalidKeySpecException, 
                                                                                       NoSuchPaddingException, 
                                                                                       InvalidKeyException, 
                                                                                       InvalidAlgorithmParameterException, 
                                                                                       DecoderException
    {
        SecretKeyFactory factory = null;
        SecretKey tmp = null;
        SecretKey secret = null;

        // since we pass it as a string of input, convert to a actual byte buffer here
        mSalt = Hex.decodeHex (salt.toCharArray ());
       Db ("got salt " + Hex.encodeHexString (mSalt));

        // get initialization vector from passed string
        mInitVec = Hex.decodeHex (initvec.toCharArray ());
        Db ("got initvector :" + Hex.encodeHexString (mInitVec));


        /* Derive the key, given password and salt. */
        // in order to do 256 bit crypto, you have to muck with the files for Java's "unlimted security"
        // The end user must also install them (not compiled in) so beware. 
        // see here: 
      // http://www.javamex.com/tutorials/cryptography/unrestricted_policy_files.shtml
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(mPassword.toCharArray (), mSalt, ITERATIONS, KEYLEN_BITS);

        tmp = factory.generateSecret(spec);
        secret = new SecretKeySpec(tmp.getEncoded(), "AES");

        /* Decrypt the message, given derived key and initialization vector. */
        mDecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        mDecipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(mInitVec));
    }


    /**
     * This is where we write out the actual encrypted data to disk using the Cipher created in setupEncrypt().
     * Pass two file objects representing the actual input (cleartext) and output file to be encrypted.
     * 
     * there may be a way to write a cleartext header to the encrypted file containing the salt, but I ran
     * into uncertain problems with that. 
     *  
     * @param input - the cleartext file to be encrypted
     * @param output - the encrypted data file
     * @throws IOException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public void WriteEncryptedFile (File input, File output) throws 
                                                                                          IOException, 
                                                                                          IllegalBlockSizeException, 
                                                                                          BadPaddingException
    {
        FileInputStream fin;
        FileOutputStream fout;
        long totalread = 0;
        int nread = 0;
        byte [] inbuf = new byte [MAX_FILE_BUF];

        fout = new FileOutputStream (output);
        fin = new FileInputStream (input);

        while ((nread = fin.read (inbuf)) > 0 )
        {
            Db ("read " + nread + " bytes");
            totalread += nread;

            // create a buffer to write with the exact number of bytes read. Otherwise a short read fills inbuf with 0x0
            // and results in full blocks of MAX_FILE_BUF being written. 
            byte [] trimbuf = new byte [nread];
            for (int i = 0; i < nread; i++)
                trimbuf[i] = inbuf[i];

            // encrypt the buffer using the cipher obtained previosly
            byte [] tmp = mEcipher.update (trimbuf);

            // I don't think this should happen, but just in case..
            if (tmp != null)
                fout.write (tmp);
        }

        // finalize the encryption since we've done it in blocks of MAX_FILE_BUF
        byte [] finalbuf = mEcipher.doFinal ();
        if (finalbuf != null)
            fout.write (finalbuf);

        fout.flush();
        fin.close();
        fout.close();

        Db ("wrote " + totalread + " encrypted bytes");
    }


    /**
     * Read from the encrypted file (input) and turn the cipher back into cleartext. Write the cleartext buffer back out
     * to disk as (output) File.
     * 
     * I left CipherInputStream in here as a test to see if I could mix it with the update() and final() methods of encrypting
     *  and still have a correctly decrypted file in the end. Seems to work so left it in.
     *  
     * @param input - File object representing encrypted data on disk 
     * @param output - File object of cleartext data to write out after decrypting
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws IOException
     */
    public void ReadEncryptedFile (File input, File output) throws 
                                                                                                                                            IllegalBlockSizeException, 
                                                                                                                                            BadPaddingException, 
                                                                                                                                            IOException
    {
        FileInputStream fin; 
        FileOutputStream fout;
        CipherInputStream cin;
        long totalread = 0;
        int nread = 0;
        byte [] inbuf = new byte [MAX_FILE_BUF];

        fout = new FileOutputStream (output);
        fin = new FileInputStream (input);

        // creating a decoding stream from the FileInputStream above using the cipher created from setupDecrypt()
        cin = new CipherInputStream (fin, mDecipher);

        while ((nread = cin.read (inbuf)) > 0 )
        {
            Db ("read " + nread + " bytes");
            totalread += nread;

            // create a buffer to write with the exact number of bytes read. Otherwise a short read fills inbuf with 0x0
            byte [] trimbuf = new byte [nread];
            for (int i = 0; i < nread; i++)
                trimbuf[i] = inbuf[i];

            // write out the size-adjusted buffer
            fout.write (trimbuf);
        }

        fout.flush();
        cin.close();
        fin.close ();       
        fout.close();   

        Db ("wrote " + totalread + " encrypted bytes");
    }


    /**
     * adding main() for usage demonstration. With member vars, some of the locals would not be needed
     */
    public static void main(String [] args)
    {

        // create the input.txt file in the current directory before continuing
        File input = new File ("input.txt");
        File eoutput = new File ("encrypted.aes");
        File doutput = new File ("decrypted.txt");
        String iv = null;
        String salt = null;
        Crypto en = new Crypto ("mypassword");

        /*
         * setup encryption cipher using password. print out iv and salt
         */
        try
      {
          en.setupEncrypt ();
          iv = Hex.encodeHexString (en.getInitVec ()).toUpperCase ();
          salt = Hex.encodeHexString (en.getSalt ()).toUpperCase ();
      }
      catch (InvalidKeyException e)
      {
          e.printStackTrace();
      }
      catch (NoSuchAlgorithmException e)
      {
          e.printStackTrace();
      }
      catch (InvalidKeySpecException e)
      {
          e.printStackTrace();
      }
      catch (NoSuchPaddingException e)
      {
          e.printStackTrace();
      }
      catch (InvalidParameterSpecException e)
      {
          e.printStackTrace();
      }
      catch (IllegalBlockSizeException e)
      {
          e.printStackTrace();
      }
      catch (BadPaddingException e)
      {
          e.printStackTrace();
      }
      catch (UnsupportedEncodingException e)
      {
          e.printStackTrace();
      }

        /*
         * write out encrypted file
         */
        try
      {
          en.WriteEncryptedFile (input, eoutput);
          System.out.printf ("File encrypted to " + eoutput.getName () + "\niv:" + iv + "\nsalt:" + salt + "\n\n");
      }
      catch (IllegalBlockSizeException e)
      {
          e.printStackTrace();
      }
      catch (BadPaddingException e)
      {
          e.printStackTrace();
      }
      catch (IOException e)
      {
          e.printStackTrace();
      }


        /*
         * decrypt file
         */
        Crypto dc = new Crypto ("mypassword");
        try
      {
          dc.setupDecrypt (iv, salt);
      }
      catch (InvalidKeyException e)
      {
          e.printStackTrace();
      }
      catch (NoSuchAlgorithmException e)
      {
          e.printStackTrace();
      }
      catch (InvalidKeySpecException e)
      {
          e.printStackTrace();
      }
      catch (NoSuchPaddingException e)
      {
          e.printStackTrace();
      }
      catch (InvalidAlgorithmParameterException e)
      {
          e.printStackTrace();
      }
      catch (DecoderException e)
      {
          e.printStackTrace();
      }

        /*
         * write out decrypted file
         */
        try
      {
          dc.ReadEncryptedFile (eoutput, doutput);
          System.out.println ("decryption finished to " + doutput.getName ());
      }
      catch (IllegalBlockSizeException e)
      {
          e.printStackTrace();
      }
      catch (BadPaddingException e)
      {
          e.printStackTrace();
      }
      catch (IOException e)
      {
          e.printStackTrace();
      }
   }


}

这与 Erickson 的答案基本相同,被一个 - 在我看来 - 没有很好编程的 - 包装器包围。printStackTrace()
@owlstead - 这是一个很好的答案。它展示了如何通过加密字节缓冲区来加密流,而不是将所有内容都保存在内存中。埃里克森的答案不适用于不适合内存的大文件。所以+1给wufoo。 :)
@dynamokaj CipherInputStreamCipherOutputStream 的使用不是什么大问题。将表下的所有异常改组是一个问题。盐突然成为一个领域并且需要静脉注射的事实是一个问题。它不遵循 Java 编码约定的事实是一个问题。而且这仅适用于未要求的文件这一事实是一个问题。其余的代码基本上是副本也无济于事。但也许我会按照建议对其进行调整以使其变得更好......
@owlstead 我同意编码看起来会更好,我已将其减少到 1/4 左右,但我喜欢他向我介绍了 CipherInputStream 和 CipherOutputStream,因为这正是我昨天需要的! ;)
为什么两次? fout.close(); fout.close();
w
waqas

从字节数组生成自己的密钥很容易:

byte[] raw = ...; // 32 bytes in size for a 256 bit key
Key skey = new javax.crypto.spec.SecretKeySpec(raw, "AES");

但创建 256 位密钥是不够的。如果密钥生成器无法为您生成 256 位密钥,则 Cipher 类可能也不支持 AES 256 位。你说你安装了无限制权限补丁,所以应该支持 AES-256 密码(但也应该支持 256 位密钥,所以这可能是配置问题)。

Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skey);
byte[] encrypted = cipher.doFinal(plainText.getBytes());

缺少 AES-256 支持的解决方法是采用一些免费提供的 AES-256 实现,并将其用作自定义提供程序。这包括创建您自己的 Provider 子类并将其与 Cipher.getInstance(String, Provider) 一起使用。但这可能是一个复杂的过程。


您应该始终指明模式和填充算法。 Java 默认使用不安全的 ECB 模式。
您不能创建自己的提供程序,必须对提供程序进行签名(不敢相信我最初阅读了这个错误)。即使可以,密钥大小的限制也存在于 Cipher 的实现中,而不是提供者本身。您可以在 Java 8 及更低版本中使用 AES-256,但您需要使用专有 API。或者当然是对密钥大小没有限制的运行时。
OpenJDK(和 Android)的最新版本对添加您自己的安全/加密提供程序没有限制。但当然,这样做需要您自担风险。如果您忘记让您的库保持最新,您可能会面临安全风险。
@MaartenBodewes+ OpenJDK 从一开始就没有“有限的加密政策”问题,Oracle JDK 在一年前为 8u161 和 9 以上的版本删除了它(也许还有一些现在只付费的较低版本,但我还没有检查过)
D
DarkSquid

我过去所做的是通过 SHA256 之类的方法对密钥进行哈希处理,然后将哈希中的字节提取到密钥字节 [] 中。

拥有 byte[] 后,您可以简单地执行以下操作:

SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(clearText.getBytes());

对于其他人:这不是一个非常安全的方法。您应该使用 PKCS#5 中指定的 PBKDF 2。埃里克森在上面说如何做到这一点。 DarkSquid 的方法容易受到密码攻击,并且除非您的明文大小是 AES 块大小(128 位)的倍数,否则它也不起作用,因为他没有填充。它也没有指定模式;阅读 Wikipedia 的 Block Cipher Modes of Operation 以解决问题。
@DarkSquid Cipher aes256 = Cipher.getInstance("AES/OFB/NoPadding"); MessageDigest keyDigest = MessageDigest.getInstance("SHA-256"); byte[] keyHash = keyDigest.digest(secret.getBytes("UTF-8")); SecretKeySpec key = new SecretKeySpec(keyHash, "AES"); aes256.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(initializationVector));我也按照您的回答中的建议做同样的事情,但我仍然得到这个 java.security.InvalidKeyException: Illegal key size 是否必须下载 JCE 策略文件?
不要在任何类型的生产环境中使用此方法。当开始使用基于密码的加密时,许多用户会被代码墙淹没,并且不了解字典攻击和其他简单的黑客攻击是如何工作的。虽然学习可能会令人沮丧,但研究这一点是值得的投资。这是一篇很好的初学者文章:adambard.com/blog/3-wrong-ways-to-store-a-password
D
Doug

添加到@Wufoo 的编辑中,以下版本使用 InputStreams 而不是文件,以便更轻松地处理各种文件。它还将 IV 和 Salt 存储在文件的开头,因此只需要跟踪密码。由于 IV 和 Salt 不需要保密,这让生活更轻松。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.security.AlgorithmParameters;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.KeySpec;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
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 AES {
    public final static int SALT_LEN     = 8;
    static final String     HEXES        = "0123456789ABCDEF";
    String                  mPassword    = null;
    byte[]                  mInitVec     = null;
    byte[]                  mSalt        = new byte[SALT_LEN];
    Cipher                  mEcipher     = null;
    Cipher                  mDecipher    = null;
    private final int       KEYLEN_BITS  = 128;    // see notes below where this is used.
    private final int       ITERATIONS   = 65536;
    private final int       MAX_FILE_BUF = 1024;

    /**
     * create an object with just the passphrase from the user. Don't do anything else yet
     * @param password
     */
    public AES(String password) {
        mPassword = password;
    }

    public static String byteToHex(byte[] raw) {
        if (raw == null) {
            return null;
        }

        final StringBuilder hex = new StringBuilder(2 * raw.length);

        for (final byte b : raw) {
            hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
        }

        return hex.toString();
    }

    public static byte[] hexToByte(String hexString) {
        int    len = hexString.length();
        byte[] ba  = new byte[len / 2];

        for (int i = 0; i < len; i += 2) {
            ba[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                                + Character.digit(hexString.charAt(i + 1), 16));
        }

        return ba;
    }

    /**
     * debug/print messages
     * @param msg
     */
    private void Db(String msg) {
        System.out.println("** Crypt ** " + msg);
    }

    /**
     * This is where we write out the actual encrypted data to disk using the Cipher created in setupEncrypt().
     * Pass two file objects representing the actual input (cleartext) and output file to be encrypted.
     *
     * there may be a way to write a cleartext header to the encrypted file containing the salt, but I ran
     * into uncertain problems with that.
     *
     * @param input - the cleartext file to be encrypted
     * @param output - the encrypted data file
     * @throws IOException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public void WriteEncryptedFile(InputStream inputStream, OutputStream outputStream)
            throws IOException, IllegalBlockSizeException, BadPaddingException {
        try {
            long             totalread = 0;
            int              nread     = 0;
            byte[]           inbuf     = new byte[MAX_FILE_BUF];
            SecretKeyFactory factory   = null;
            SecretKey        tmp       = null;

            // crate secureRandom salt and store  as member var for later use
            mSalt = new byte[SALT_LEN];

            SecureRandom rnd = new SecureRandom();

            rnd.nextBytes(mSalt);
            Db("generated salt :" + byteToHex(mSalt));
            factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

            /*
             *  Derive the key, given password and salt.
             *
             * in order to do 256 bit crypto, you have to muck with the files for Java's "unlimted security"
             * The end user must also install them (not compiled in) so beware.
             * see here:  http://www.javamex.com/tutorials/cryptography/unrestricted_policy_files.shtml
             */
            KeySpec spec = new PBEKeySpec(mPassword.toCharArray(), mSalt, ITERATIONS, KEYLEN_BITS);

            tmp = factory.generateSecret(spec);

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

            /*
             *  Create the Encryption cipher object and store as a member variable
             */
            mEcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            mEcipher.init(Cipher.ENCRYPT_MODE, secret);

            AlgorithmParameters params = mEcipher.getParameters();

            // get the initialization vectory and store as member var
            mInitVec = params.getParameterSpec(IvParameterSpec.class).getIV();
            Db("mInitVec is :" + byteToHex(mInitVec));
            outputStream.write(mSalt);
            outputStream.write(mInitVec);

            while ((nread = inputStream.read(inbuf)) > 0) {
                Db("read " + nread + " bytes");
                totalread += nread;

                // create a buffer to write with the exact number of bytes read. Otherwise a short read fills inbuf with 0x0
                // and results in full blocks of MAX_FILE_BUF being written.
                byte[] trimbuf = new byte[nread];

                for (int i = 0; i < nread; i++) {
                    trimbuf[i] = inbuf[i];
                }

                // encrypt the buffer using the cipher obtained previosly
                byte[] tmpBuf = mEcipher.update(trimbuf);

                // I don't think this should happen, but just in case..
                if (tmpBuf != null) {
                    outputStream.write(tmpBuf);
                }
            }

            // finalize the encryption since we've done it in blocks of MAX_FILE_BUF
            byte[] finalbuf = mEcipher.doFinal();

            if (finalbuf != null) {
                outputStream.write(finalbuf);
            }

            outputStream.flush();
            inputStream.close();
            outputStream.close();
            outputStream.close();
            Db("wrote " + totalread + " encrypted bytes");
        } catch (InvalidKeyException ex) {
            Logger.getLogger(AES.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidParameterSpecException ex) {
            Logger.getLogger(AES.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(AES.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(AES.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeySpecException ex) {
            Logger.getLogger(AES.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Read from the encrypted file (input) and turn the cipher back into cleartext. Write the cleartext buffer back out
     * to disk as (output) File.
     *
     * I left CipherInputStream in here as a test to see if I could mix it with the update() and final() methods of encrypting
     *  and still have a correctly decrypted file in the end. Seems to work so left it in.
     *
     * @param input - File object representing encrypted data on disk
     * @param output - File object of cleartext data to write out after decrypting
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws IOException
     */
    public void ReadEncryptedFile(InputStream inputStream, OutputStream outputStream)
            throws IllegalBlockSizeException, BadPaddingException, IOException {
        try {
            CipherInputStream cin;
            long              totalread = 0;
            int               nread     = 0;
            byte[]            inbuf     = new byte[MAX_FILE_BUF];

            // Read the Salt
            inputStream.read(this.mSalt);
            Db("generated salt :" + byteToHex(mSalt));

            SecretKeyFactory factory = null;
            SecretKey        tmp     = null;
            SecretKey        secret  = null;

            factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

            KeySpec spec = new PBEKeySpec(mPassword.toCharArray(), mSalt, ITERATIONS, KEYLEN_BITS);

            tmp    = factory.generateSecret(spec);
            secret = new SecretKeySpec(tmp.getEncoded(), "AES");

            /* Decrypt the message, given derived key and initialization vector. */
            mDecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            // Set the appropriate size for mInitVec by Generating a New One
            AlgorithmParameters params = mDecipher.getParameters();

            mInitVec = params.getParameterSpec(IvParameterSpec.class).getIV();

            // Read the old IV from the file to mInitVec now that size is set.
            inputStream.read(this.mInitVec);
            Db("mInitVec is :" + byteToHex(mInitVec));
            mDecipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(mInitVec));

            // creating a decoding stream from the FileInputStream above using the cipher created from setupDecrypt()
            cin = new CipherInputStream(inputStream, mDecipher);

            while ((nread = cin.read(inbuf)) > 0) {
                Db("read " + nread + " bytes");
                totalread += nread;

                // create a buffer to write with the exact number of bytes read. Otherwise a short read fills inbuf with 0x0
                byte[] trimbuf = new byte[nread];

                for (int i = 0; i < nread; i++) {
                    trimbuf[i] = inbuf[i];
                }

                // write out the size-adjusted buffer
                outputStream.write(trimbuf);
            }

            outputStream.flush();
            cin.close();
            inputStream.close();
            outputStream.close();
            Db("wrote " + totalread + " encrypted bytes");
        } catch (Exception ex) {
            Logger.getLogger(AES.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * adding main() for usage demonstration. With member vars, some of the locals would not be needed
     */
    public static void main(String[] args) {

        // create the input.txt file in the current directory before continuing
        File   input   = new File("input.txt");
        File   eoutput = new File("encrypted.aes");
        File   doutput = new File("decrypted.txt");
        String iv      = null;
        String salt    = null;
        AES    en      = new AES("mypassword");

        /*
         * write out encrypted file
         */
        try {
            en.WriteEncryptedFile(new FileInputStream(input), new FileOutputStream(eoutput));
            System.out.printf("File encrypted to " + eoutput.getName() + "\niv:" + iv + "\nsalt:" + salt + "\n\n");
        } catch (IllegalBlockSizeException | BadPaddingException | IOException e) {
            e.printStackTrace();
        }

        /*
         * decrypt file
         */
        AES dc = new AES("mypassword");

        /*
         * write out decrypted file
         */
        try {
            dc.ReadEncryptedFile(new FileInputStream(eoutput), new FileOutputStream(doutput));
            System.out.println("decryption finished to " + doutput.getName());
        } catch (IllegalBlockSizeException | BadPaddingException | IOException e) {
            e.printStackTrace();
        }
    }
}

这个解决方案似乎使用了一些笨拙的缓冲区处理和绝对低于标准的异常处理,基本上是记录它们然后忘记它们。请注意,对于文件使用 CBC 是可以的,但对于传输安全性而言则不行。使用 PBKDF2 和 AES 当然可以辩护,从这个意义上说,它可能是解决方案的良好基础。
P
Praveen

(可能对其他有类似要求的人有帮助)

我有类似的要求在 Java 中使用 AES-256-CBC 加密和解密。

要实现(或指定)256 字节加密/解密,Java Cryptography Extension (JCE) 政策应设置为 "Unlimited"

可以在 $JAVA_HOME/jre/lib/security(JDK)或 $JAVA_HOME/lib/security(JRE)下的 java.security 文件中设置

crypto.policy=unlimited

或者在代码中

Security.setProperty("crypto.policy", "unlimited");

Java 9 及更高版本默认启用此功能。


我尝试使用 java 1.8.201 版本,即使没有安全属性它也可以工作
w
whitebrow

考虑使用我是作者的 Encryptor4j

首先确保您在继续之前安装了 Unlimited Strength Jurisdiction Policy 文件,以便您可以使用 256 位 AES 密钥。

然后执行以下操作:

String password = "mysupersecretpassword"; 
Key key = KeyFactory.AES.keyFromPassword(password.toCharArray());
Encryptor encryptor = new Encryptor(key, "AES/CBC/PKCS7Padding", 16);

您现在可以使用加密器来加密您的消息。如果您愿意,您还可以执行流式加密。为了您的方便,它会自动生成并预先添加一个安全的 IV。

如果它是您希望压缩的文件,请查看此答案 Encrypting a large file with AES using JAVA 以获得更简单的方法。


嗨马丁,如果你想指出,你应该总是表明你是图书馆的作者。有很多加密包装器试图让事情变得简单。这个是否有安全文件或是否收到任何评论以使其值得我们花时间?
j
jeprubio

使用此类进行加密。有用。

public class ObjectCrypter {


    public static byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] mes) 
            throws NoSuchAlgorithmException,
            NoSuchPaddingException,
            InvalidKeyException,
            InvalidAlgorithmParameterException,
            IllegalBlockSizeException,
            BadPaddingException, IOException {

        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
        return  cipher.doFinal(mes);

    }

    public static byte[] decrypt(byte[] ivBytes, byte[] keyBytes, byte[] bytes) 
            throws NoSuchAlgorithmException,
            NoSuchPaddingException,
            InvalidKeyException,
            InvalidAlgorithmParameterException,
            IllegalBlockSizeException,
            BadPaddingException, IOException, ClassNotFoundException {

        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
        return  cipher.doFinal(bytes);

    }
}

这些是 ivBytes 和随机密钥;

String key = "e8ffc7e56311679f12b6fc91aa77a5eb";

byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
keyBytes = key.getBytes("UTF-8");

“它有效”....是的,但它不符合创建加密安全解决方案的要求(在我看来,它也不符合 Java 异常处理方面的编码标准)。
IV 被初始化为零。搜索 BEAST 和 ACPA 攻击。
除了 wazoo、生成“随机”密钥的方法和零 IV 是此实现的问题,但这些问题很容易解决。 +1。

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅