简体   繁体   English

这是否使用256位AES加密?

[英]Does this use 256-bit AES encryption?

I think it's hashing a 256 bit key, not sure if this is producing 256 bit cipher text though. 我认为它正在散列256位密钥,但是不确定这是否会产生256位密文。 Does using a 256-bit key mean the cipher will produce a 256-bit cipher text? 使用256位密钥是否意味着密码将生成256位密文? The resultant cipher text is base 64 encoded. 生成的密文以base 64编码。

Thanks! 谢谢!

import java.security.spec.InvalidKeySpecException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import com.ibm.broker.javacompute.Base64;

public class Security {
    private static final String AES_PASS = "43qyu3qwjaw8ga5azbro00ig"; // Hashed into an AES key later
    private SecretKeySpec keyObj;
    private Cipher cipher;
    private IvParameterSpec ivObj;

    public Security() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException {
        // A constant IV, since CBC requires an IV but we don't really need one
        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        this.ivObj = new IvParameterSpec(iv);

        // Create an SHA-256 256-bit hash of the key
        byte[] key = AES_PASS.getBytes();
        MessageDigest sha = MessageDigest.getInstance("SHA-256");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 32); // Use only first 256 bit
        this.keyObj = new SecretKeySpec(key, "AES");

        // Create a Cipher by specifying the following parameters
        //  a. Algorithm name - here it is AES 
        //  b. Mode - here it is CBC mode 
        //  c. Padding - e.g. PKCS7 or PKCS5
        this.cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
    }

    public String encrypt(String strDataToEncrypt) throws InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException {
        String strCipherText = new String();

        this.cipher.init(Cipher.ENCRYPT_MODE, this.keyObj, this.ivObj);

        // Encrypt the Data 
        //  a. Declare / Initialize the Data. Here the data is of type String 
        //  b. Convert the Input Text to Bytes 
        //  c. Encrypt the bytes using doFinal method
        byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();

        byte[] byteCipherText = this.cipher.doFinal(byteDataToEncrypt);

        // b64 is done differently on Android
        strCipherText = Base64.encode(byteCipherText);

        return strCipherText;
    }

    public String decrypt(String strCipherText) throws InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException {
        String strDecryptedText = new String();

        // Initialize the Cipher for Encryption
        this.cipher.init(Cipher.DECRYPT_MODE, this.keyObj, this.ivObj);

        // Decode the Base64 text
        byte[] cipherBytes = Base64.decode(strCipherText);

        // Decrypt the Data
        //  a. Initialize a new instance of Cipher for Decryption (normally don't reuse the same object)
        //     Be sure to obtain the same IV bytes for CBC mode.
        //  b. Decrypt the cipher bytes using doFinal method
        byte[] byteDecryptedText = this.cipher.doFinal(cipherBytes);
        strDecryptedText = new String(byteDecryptedText);

        return strDecryptedText;
    }
}

Your example appears to use a 32-byte key and a 256 bit version of the AES cryptosystem. 您的示例似乎使用了32字节密钥和256位版本的AES密码系统。 So, technically yes it is 256-bit AES encryption. 因此,从技术上讲,它是256位AES加密。 The actual size of the message determines the resulting output but it should be larger then the original message. 消息的实际大小决定了结果输出,但它应大于原始消息。 Also, you should be able to decrypt it and get the original message. 同样,您应该能够解密它并获得原始消息。 Finally, using a constant iv is not recommended and may well render your system insecure in and of itself. 最后, 建议使用常量iv,它很可能使系统本身不安全。

MessageDigest sha = MessageDigest.getInstance("SHA-256");
key = sha.digest(key);

The following code creates a Hash of the input that is key. 以下代码创建作为键的输入的哈希值。 If we have some data "x" and "y" unless x=y hash of "x" will never equal to hash of "y", this can be used to determine if original data is tampered because if it is it will produce a different hash. 如果我们有一些数据“ x”和“ y”,除非x = y“ x”的哈希值永远不会等于“ y”的哈希值,则这可以用来确定原始数据是否被篡改,因为如果原始数据被篡改,则会产生一个不同的哈希。

key = Arrays.copyOf(key, 32); // Use only first 256 bit
this.keyObj = new SecretKeySpec(key, "AES");

In this case you are getting 32 bytes of the digest you have created and forming a secret key that is of size 256 bit as 8x32=256 you are then using the cipher along with this key for encryption and decryption. 在这种情况下,您将获得创建的摘要的32个字节,并形成一个256位大小为8x32 = 256的密钥,然后将密码与该密钥一起用于加密和解密。

most of the Cipher's operate in blocks (this one does). 大多数密码算法都是按块操作(此操作)。 They partition the text to be encrypted into fixed block size which is equal to key size and then apply XOR etc operation on the block to get the encrypted block . 他们将要加密的文本划分为与密钥大小相等的固定块大小,然后对块进行XOR等操作得到加密块。 If the text size does not align with cipher block size then extra padding is appended to the text to align it to the fixed block size. 如果文本大小与密码块大小不匹配,则会在文本上附加额外的填充以使其与固定块大小对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM