java - KeyGenerator 中 AES 与 DES 的 key 大小

标签 java encryption

我正在使用 javax.crypto.KeyGenerator 生成 key 。

下面是我的 AES 代码:

Key key = null;
        SecureRandom rand = new SecureRandom();
        KeyGenerator generator;
        try {                       
            generator = KeyGenerator.getInstance("AES");
            generator.init(rand);
            generator.init(128);
            key = generator.generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        System.out.println("AES key is : ");
        System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));

DES 代码:

Key key = null;
        SecureRandom rand = new SecureRandom();
        KeyGenerator generator;
        try {                       
            generator = KeyGenerator.getInstance("DES");
            generator.init(rand);
            generator.init(56);
            key = generator.generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        System.out.println("DES key is : ");
        System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));

正如您所见,当我使用 DES 时,我必须将 56 传递给 init() 方法,而不是 AES 中的 128。

有人可以解释一下为什么我不能使用 128 位以及这两种类型中哪种加密更可取?

最佳答案

AES 是 DES 的高级版本,具有更大的 key 大小。因此,如果您需要更安全的加密,那么使用 AES 应该是您的选择。

Data Encryption Standard (DES) : DES is an implementation of a Feistel Cipher. It uses 16 round Feistel structure. The block size is 64-bit. Though, key length is 64-bit, DES has an effective key length of 56 bits, since 8 of the 64 bits of the key are not used by the encryption algorithm (function as check bits only).

Advanced Encryption Standard(AES) :The more popular and widely adopted symmetric encryption algorithm likely to be encountered nowadays is the Advanced Encryption Standard (AES). It is found at least six time faster than triple DES. A replacement for DES was needed as its key size was too small. With increasing computing power, it was considered vulnerable against exhaustive key search attack. Triple DES was designed to overcome this drawback but it was found slow

关于java - KeyGenerator 中 AES 与 DES 的 key 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47647338/

相关文章:

java - 如何剪辑 Path2D?

encryption - GPG - 以非交互方式更改密码

c# - Python 中 C# 的 GetBytes() 等价于什么?

java - 将 RestAPI Slim 框架连接到 Android 应用程序

java - java中的简单加密

java - 如何为java中的Zip文件夹提供密码保护?

c - 为什么我的 vigenere.c 不工作?

java - 枚举 - 静态和实例 block

java - Java 中的锁定效果

java - float 操作按钮加号未显示