2014-12-01 110 views
1

我认为这是一个256位密钥散列,不知道这是否产生256位密文。使用256位密钥是否意味着密码会生成256位密文?由此产生的密文是基于64编码的。这是否使用256位AES加密?

谢谢!

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

回答

2

您的示例似乎使用32位密钥和256位版本的AES密码系统。所以,从技术上讲,它是256位AES加密。消息的实际大小决定了结果输出,但它应该大于原始消息。此外,你应该能够解密它并获得原始信息。最后,建议使用常量iv的是而不是,并且可能使您的系统本身不安全。

+0

它看起来像基地64编码的字符串只有160位。但由于密钥是256位,这仍被认为是256位AES加密? – Wes 2014-12-01 20:01:51

+0

@Wes这是一条短消息,但是如果密钥更长,那么可能的消息是。这是确定加密类型的关键和算法(**不是**消息)。 – 2014-12-01 20:04:18

+0

至于常数IV,我们使用的是永不改变的全局加密密钥(应用程序不会使用会话或登录),因此IV对于此处的应用程序似乎没有意义。由于该算法需要一个,而不是不提供IV我只是提供一个恒定的。 – Wes 2014-12-01 20:08:31

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

以下代码创建一个输入的哈希键,它是关键字。如果我们有一些数据“x”和“y”,除非x = y“x”的散列永远不会等于“y”的散列,这可以用来确定原始数据是否被篡改,因为如果它会产生一个不同的散列。

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

在这种情况下你得到32个字节的摘要的创建并形成秘密密钥是大小为256位为8X32 = 256 您然后使用该密码与此键进行加密和沿解密。

大部分密码都是以块的形式运行的(这个是这样做的)。他们将要加密的文本划分为与密钥大小相同的固定块大小,然后对该块应用XOR等操作以获得加密块。如果文本大小与密码块大小不一致,则会在文本上附加额外的填充以将其与固定块大小对齐。

+0

不,每次AES块大小为128位,但密钥大小可能在128位,192位和256位之间变化。您可能意指的是Rijndael,它也支持256位块大小。 – 2014-12-01 20:27:55

+0

你是正确的AES/CBC/PKCS5Padding(128)“https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html”。但我认为答案已经清除了许多关于这个问题的疑问。 – Manish 2014-12-01 21:40:00

+0

在上面的代码中,您使用“SHA-256”创建加密密钥,但稍后再将该密钥与“AES”加密算法相关联。这是不正确的,与该密钥关联的算法应该是“SHA-256”而不是“AES”。 – user2213684 2016-10-28 02:04:04