簡體   English   中英

ECB和CBC AES輸出在Java中相等

[英]ECB and CBC AES output is equal in Java

我玩過Java AES En / Decryption,並為此使用了不同的Cyper模式。 即我使用CBC和ECB。 由於歐洲央行被認為很薄弱,所以我想加入加拿大廣播公司。

我假設加密文本ob cbc和ecb的輸出是不同的,但是它們是相等的。 這怎么可能?

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

import javax.crypto.Cipher;
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.binary.Hex;

import com.instana.backend.common.exception.InstanaException;

public class AESTest {

  private static String pwd = "etjrgp9user9fu3984h1&(/&%$§";

  public static void main(String[] args) throws Exception {
    System.out.println("UNSECURE WITH ECB:");
    String ecbEncrypt = encrypt("YOLO", cypher(Cipher.ENCRYPT_MODE, "AES"));
    System.out.println("Encrypted: " + ecbEncrypt);

    String ebcDecrypt = decrypt(ecbEncrypt, cypher(Cipher.DECRYPT_MODE, "AES"));
    System.out.println("Decrypted: " + ebcDecrypt);

    System.out.println("=====================================");
    System.out.println("SECURE WITH CBC");

    String cbcEncrypt = encrypt("YOLO", cypher(Cipher.ENCRYPT_MODE, "AES/CBC/PKCS5Padding"));
    System.out.println("Encrypted: " + cbcEncrypt);

    String cbcDecrypt = decrypt(cbcEncrypt, cypher(Cipher.DECRYPT_MODE, "AES/CBC/PKCS5Padding"));
    System.out.println("Decrypted: " + cbcDecrypt);

    System.out.println("=====================================");
    System.out.println("Decrypting CBC with ECB");


  }

  public static String encrypt(String superDuperSecret, Cipher cipher) throws IOException {
    try {
      byte[] encrypted = cipher.doFinal(superDuperSecret.getBytes("UTF-8"));

      return new String(new Hex().encode(encrypted));

    } catch (Exception e) {
      throw new InstanaException("Encryption of token failed.", e);
    }
  }

  public static String decrypt(String superDuperSecret, Cipher cipher) {
    try {
      byte[] encrypted1 = new Hex().decode(superDuperSecret.getBytes("UTF-8"));

      return new String(cipher.doFinal(encrypted1));

    } catch (Exception e) {
      throw new InstanaException("Encrypted text could not be decrypted.", e);
    }
  }

  private static Cipher cypher(int mode, String method)
      throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException,
      InvalidAlgorithmParameterException {
    SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec spec = new PBEKeySpec(pwd.toCharArray(), pwd.getBytes(), 128, 128);
    SecretKey tmp = skf.generateSecret(spec);
    SecretKey key = new SecretKeySpec(tmp.getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance(method);

    if(method.contains("CBC")) {
      byte[] ivByte = new byte[cipher.getBlockSize()];
      IvParameterSpec ivParamsSpec = new IvParameterSpec(ivByte);

      cipher.init(mode, key, ivParamsSpec);
    }else{
      cipher.init(mode, key);
    }

    return cipher;
  }
}

由於您傳遞的是空IV(您從未在ivByte放入任何內容),因此無論使用哪種模式,對第一個塊執行的操作都是相同的。 加密更長的有效負載會導致在CBC情況下將第二個塊鏈接到第一個塊,而隨后的塊在ECB / CBC之間將有所不同。

在使用CBC模式時,您應該傳遞一個非空的IV,因此第一個塊將與IV混合,從而導致從第一個塊開始的加密值不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM