简体   繁体   English

AES:在 Java 中使用 ECB 模式加密

[英]AES: Encryption with ECB mode in Java

I am actually Researching the difference between the ECB mode of AES encryption and CBC Mode.我其实是在研究 AES 加密的 ECB 模式和 CBC 模式的区别。 After some study I came to know that ECB mode has some flaws at it creates same ciphertext for the plaintext if encrypted with the same key and same content.经过一番研究,我知道 ECB 模式存在一些缺陷,如果使用相同的密钥和相同的内容加密,它会为明文创建相同的密文。 on the other hand, in CBC there is Initialization Vector to overcome this issue which creates randomness of bytes on run-time for each encryption.另一方面,在 CBC 中有初始化向量来克服这个问题,它会在运行时为每个加密创建字节的随机性。

Now, on few forums I came to know that if there are similar blocks between two plain texts, then it will produce the same ciphertext each time;现在,在几个论坛上我才知道,如果两个纯文本之间存在相似的块,那么每次都会产生相同的密文; which will help the hacker to identify the common pattern of cipher.这将有助于黑客识别密码的常见模式。

I tried it with some Java code and provided two 2 plain texts as below:我尝试了一些 Java 代码并提供了两个 2 纯文本,如下所示:

Jimmy Anderson.
Corrie Anderson.

Now the word Anderson is common between both but when i encrypted it and printed the output, it produced different ciphertexts.现在安德森这个词在两者之间很常见,但是当我对其进行加密并打印 output 时,它产生了不同的密文。 However, encrypting the same piece of plain text produced similar ciphertexts that's fine though.但是,加密同一段纯文本会产生类似的密文,但这很好。 But why it produced different ciphers for common last names?但是为什么它为常见的姓氏产生了不同的密码呢?

another question is that, if we are using the random key generation on each encryption cycle, then It will always produce a different cipher for even same plain text because the key is different?另一个问题是,如果我们在每个加密周期都使用随机密钥生成,那么即使是相同的纯文本,它总是会因为密钥不同而产生不同的密码?

Then whats the need of CBC if it is catered here?那么如果在这里满足CBC的需求呢?

Can someone help me out in this?有人可以帮我解决这个问题吗?

Any help will be appreciated:).任何帮助将不胜感激:)。

    public static String encryptwithecb ( byte[] plaintext, SecretKey key )  throws Exception
    {
        Cipher cipher = Cipher.getInstance( "AES/ECB/PKCS5Padding" );

        SecretKeySpec keyspec = new SecretKeySpec( key.getEncoded(), "AES" );

        cipher.init( Cipher.ENCRYPT_MODE,  keyspec );

        byte[] cipherText = cipher.doFinal( plaintext );

        return Base64.getEncoder().encodeToString( cipherText );

    }

KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);

        // Generate Key
        SecretKey key = keyGenerator.generateKey();
        System.out.println("");
        System.out.println("");
        System.out.println("***** ENCRYPT WITH ECB ****** ");
        System.out.println("");

        String name1 = "Jimmy Anderson";
        String name2 = "Corrie Anderson..";

        String ecb = encryptwithecb( name1.getBytes("UTF-8") , key);
        String ecb2 = encryptwithecb( name2.getBytes("UTF-8") , key);
        String ecb3 = encryptwithecb( name1.getBytes("UTF-8") , key);

        System.out.println(ecb);
        System.out.println(ecb2);
        System.out.println(ecb3);

        System.out.println("");
        System.out.println("***** ENCRYPT WITH ECB ****** ");

        System.out.println("");
        System.out.println("");

Output Output

***** ENCRYPT WITH ECB ****** 

gs3y1N8jA7kwzO/c/dzwEA==
yKFC43ySA1NBAnRdvp9jEHtfcJeM7bAlmcMY63Aeupc=
gs3y1N8jA7kwzO/c/dzwEA==

***** ENCRYPT WITH ECB ******

ECB mode produces the same ciphertext if any block matches.如果任何块匹配,ECB 模式会产生相同的密文。 The blocks are 16 bytes for AES.对于 AES,这些块是 16 个字节。 This is not just true for initial blocks, but for any block required to encrypt the message.这不仅适用于初始块,而且适用于加密消息所需的任何块。 In your example the first block is different because the first names are different.在您的示例中,第一个块不同,因为名字不同。 Besides that, the plaintext is shifted one character.除此之外,明文移动一个字符。 Blocks always start at a an offset that is a block boundary, also a multiple of 16 of course.块总是从块边界的偏移量开始,当然也是 16 的倍数。

Yes, it is possible to generate a different key each time.是的,每次都可以生成不同的密钥。 But that would often be quite inefficient.但这通常效率很低。 You have to securely share the key between sender and receiver for instance, while the IV can be included with the message.例如,您必须在发送者和接收者之间安全地共享密钥,而 IV 可以包含在消息中。 Maybe the key is stored in a secure hardware location that you don't want to update.也许密钥存储在您不想更新的安全硬件位置。 Performing Diffie-Hellman key agreement to establish a key is rather costly.执行 Diffie-Hellman 密钥协议以建立密钥是相当昂贵的。

For CBC the IV must be unpredictable to an adversary (which is commonly solved by making it random).对于 CBC,IV 对对手来说必须是不可预测的(通常通过使其随机来解决)。 Many other modes such as counter mode or GCM mode only require a nonce, in which case you can use a message counter, which is often required anyway to disallow replay attacks.许多其他模式(例如计数器模式或 GCM 模式)只需要一个随机数,在这种情况下,您可以使用消息计数器,无论如何通常都需要它来禁止重放攻击。

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

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