Skip to content

对称密码算法

DES算法

DES密码算法(Data Encryption Standard,数据加密标准)是一种对称密钥算法,加密和解密使用相同的密钥。由于DES的密钥长度仅为56位,其密钥空间大小为2^56,这在现代计算条件下存在被穷举搜索攻击的风险。由于安全性问题,DES已经不再被广泛使用,取而代之的是更安全的高级加密标准(AES)。DES密码算法是一种经典的对称密钥算法,但在现代加密领域已逐渐被更安全、更高效的算法所取代。

详细用法请参考 链接

DES算法测试例子:

java
/**
* 测试DES密码算法的加密和解密
*
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
@Test
public void testDESEncryptDecrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    String plainText = RandomStringUtils.random(256);

    // DES密码算法
    String algorithm = "DES";
    // 明文的密钥
    String plainSecretKey = RandomStringUtils.random(1024);

    // 生成秘钥
    KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
    keyGenerator.init(new SecureRandom(plainSecretKey.getBytes()));
    SecretKey secretKey = keyGenerator.generateKey();
    byte[] secretKeyBytes = secretKey.getEncoded();

    // 加密
    secretKey = new SecretKeySpec(secretKeyBytes, algorithm);
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] encryptBytes = cipher.doFinal(plainText.getBytes());

    // 解密
    secretKey = new SecretKeySpec(secretKeyBytes, algorithm);
    cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] plainBytes = cipher.doFinal(encryptBytes);

    // 原来的明文能够匹配被加密和解密后的明文
    String plainTextDecrypt = new String(plainBytes);
    Assert.assertEquals(plainText, plainTextDecrypt);
}

DES3算法

DES3密码算法,也称为Triple Data Encryption Algorithm(TDEA)或Triple DES,是一种对称密钥加密块密码。它是对原始Data Encryption Standard(DES)算法的一种增强,通过三重加密过程来提高安全性。

DES3密码算法是一种通过三重加密来提高安全性的对称密钥加密算法,它使用三个密钥进行加密和解密操作,并提供多种工作模式。然而,由于其较慢的处理速度和逐渐降低的安全边际,现代系统已开始转向更先进的加密算法,如AES。

详细用法请参考 链接

DES3算法测试例子:

java
 /**
 * 测试DES3密码算法的加密和解密
 *
 * @throws NoSuchAlgorithmException
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
@Test
public void testDES3EncryptDecrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    String plainText = RandomStringUtils.random(256);

    String algorithm = "DESede";
    String plainSecretKey = RandomStringUtils.random(1024);

    // 生成秘钥
    KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
    keyGenerator.init(new SecureRandom(plainSecretKey.getBytes()));
    SecretKey secretKey = keyGenerator.generateKey();
    byte[] secretKeyBytes = secretKey.getEncoded();

    // 加密
    secretKey = new SecretKeySpec(secretKeyBytes, algorithm);
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] encryptBytes = cipher.doFinal(plainText.getBytes());

    // 解密
    secretKey = new SecretKeySpec(secretKeyBytes, algorithm);
    cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] plainBytes = cipher.doFinal(encryptBytes);

    // 原来的明文能够匹配被加密和解密后的明文
    String plainTextDecrypt = new String(plainBytes);
    Assert.assertEquals(plainText, plainTextDecrypt);
}

AES算法

AES(Advanced Encryption Standard)算法是一种广泛使用的对称密钥加密算法,由美国政府于2001年发布,并作为联邦信息处理标准(FIPS)的一部分被采用。AES的设计目标是替代其前身DES(Data Encryption Standard)算法,因为DES的密钥长度(56位有效密钥)被认为不足以提供足够的安全性。

详细用法请参考 链接

AES算法测试例子:

java
/**
* 测试AES密码算法的加密和解密
*
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
@Test
public void testAESEncryptDecrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    String plainText = RandomStringUtils.random(256);

    String algorithm = "AES";
    String plainSecretKey = RandomStringUtils.random(1024);

    // 生成秘钥
    KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
    keyGenerator.init(new SecureRandom(plainSecretKey.getBytes()));
    SecretKey secretKey = keyGenerator.generateKey();
    byte[] secretKeyBytes = secretKey.getEncoded();

    // 加密
    secretKey = new SecretKeySpec(secretKeyBytes, algorithm);
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] encryptBytes = cipher.doFinal(plainText.getBytes());

    // 解密
    secretKey = new SecretKeySpec(secretKeyBytes, algorithm);
    cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] plainBytes = cipher.doFinal(encryptBytes);

    // 原来的明文能够匹配被加密和解密后的明文
    String plainTextDecrypt = new String(plainBytes);
    Assert.assertEquals(plainText, plainTextDecrypt);
}