本文主要是介绍PKI - 对称加密算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
PKI - 对称加密算法
- 1. 数据加密标准(DES - Data Encryption Standard)
- 2. 三重DES - DESede
- 3. 高级数据加密标准(AES - Advanced Encryption Standard)
- 4. 国际数据加密标准(IDEA - International Data Encryption Algorithm)
- 5. 基于口令加密(PBE - Password Based Encryption)
- 1977 DES
- 1998 AES
- 1990 IDEA
1. 数据加密标准(DES - Data Encryption Standard)
// 密钥算法
public static final String KEY_ALGORITHM = "DES";
// 加密/解密算法 / 工作模式 / 填充方式
public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";// 密钥生成(56位)
public byte[] generateDesSecretKey() throws NoSuchAlgorithmException {// generate des keyfinal KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);keyGenerator.init(56);final SecretKey secretKey = keyGenerator.generateKey();return secretKey.getEncoded();
}// 密钥生成(64位)
public byte[] generateDesSecretKey64() throws NoSuchAlgorithmException {// generate des keySecurity.addProvider(new BouncyCastleProvider());final KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);keyGenerator.init(64);final SecretKey secretKey = keyGenerator.generateKey();return secretKey.getEncoded();
}// 根据密钥生成SecretKey对象
public SecretKey generateDesSecretKey(byte[] encodedSk) throws Exception {// create SecretKey object with given secret keyfinal DESKeySpec desKeySpec = new DESKeySpec(encodedSk);final SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);return secretKeyFactory.generateSecret(desKeySpec);
}// 数据加密
public byte[] encryptData(SecretKey secretKey, byte[] rawData) throws Exception {final Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, secretKey);return cipher.doFinal(rawData);
}// 数据解密
public byte[] decryptData(SecretKey secretKey, byte[] encryptData) throws Exception {final Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, secretKey);return cipher.doFinal(encryptData);
}@Test
public void testDES() throws Exception {// 生成密钥final byte[] secretKeyBytes = generateDesSecretKey();// 创建SecretKey对象final SecretKey secretKey = generateDesSecretKey(secretKeyBytes);// 原文final String rawMessage = "我是阿汤哥";// 加密后final byte[] encryptBytes = encryptData(secretKey, rawMessage.getBytes());// 解密后final byte[] decryptedBytes = decryptData(secretKey, encryptBytes);// 输出System.out.println("原文: " + rawMessage + "\n" +"密钥: " + Base64.toBase64String(secretKeyBytes) + "\n" +"加密后:" + Base64.toBase64String(encryptBytes) + "\n" +"解密后:" + new String(decryptedBytes));
}
执行结果
原文: 我是阿汤哥
密钥: Am0+0HV5Rp4=
加密后:xoensMBnckMY0H4HAjuqUQ==
解密后:我是阿汤哥
2. 三重DES - DESede
DES算法有三点安全隐患:密钥太短、迭代偏少和半公开性。针对密钥太短和迭代偏少的问题,有人提出了多重DES的方式来克服这些缺陷。比较典型的有两重DES(2DES)、三重DES(3DES)和四重DES(4DES)等几种形式,在实际应用中一般采用三重DES,它还有两个别名 Triple DES和DESede。
三重DES的代码实现需要变更以下内容:
- KEY_ALGORITHM 调整:由 DES 改为 DESede
- CIPHER_ALGORITHM 调整:如DESede/ECB/PKCS5Padding
- 密钥长度调整: Java7支持的密钥长度为 112位 和 168位,Bouncy Castle支持的密钥长度为 128位 和 192位
3. 高级数据加密标准(AES - Advanced Encryption Standard)
1997年,NIST发起了征集DES替代算法 - AES(Advanced Encryption Standard,高级数据加密标准)算法的活动。最终在2000年10月2日,由Daemen和Rijmen两位比利时人提出的Rijndael算法,以其密钥设置快、存储要求低,在硬件实现和限制存储的条件下性能优异当选AES算法。
AES的代码实现需要变更以下内容:
- KEY_ALGORITHM 调整:由 DES 改为 AES
- CIPHER_ALGORITHM 调整:如 AES/ECB/PKCS5Padding
- 密钥长度调整: AES标准支持三种密钥长度,分别为 128位、192位、256位,默认长度 128位
4. 国际数据加密标准(IDEA - International Data Encryption Algorithm)
IDEA(International Data Encryption Algorithm,国际数据加密标准)算法是由旅居瑞士的中国青年学者来学嘉和著名密码专家JamesMassey于1990年提出的一种对称分组密码,并于1992年修改完成。
IDEA的代码实现需要变更以下内容:
- KEY_ALGORITHM 调整:由 DES 改为 IDEA
- CIPHER_ALGORITHM 调整:如 IDEA/ECB/ISO10126Padding
- 密钥长度调整: 128位
5. 基于口令加密(PBE - Password Based Encryption)
PBE算法是对称加密算法的综合性算法,常见算法如PBE With MD5 And DES,该算法使用了MD5和DES算法构建PBE算法。
盐可以是消息传递双方按一定规律约定的信息,如时间。也可以是某个不可变物理硬件的编号,如U盘自身的唯一标识。
甲乙双方可以通过约定消息传递时间,并将其作为“盐”的基本信息,根据预定的算法(如MD5)对其处理,最终获得真正的“盐”。这样一来,“盐”就无需传递,提高了安全性。
如果有一种系统,使用者需要将U盘插入计算机,同时输入口令方可登录系统,那么U盘就是“盐”信息的提供者。即使U盘丢失,加密的信息也未必能被窃取。
从另一个角度思考,“盐”和口令就像两把密不可分的钥匙。
/*** Java7支持以下任意一种算法* PBEWithMD5AndDES* PBEWithMD5AndTripleDES* PBEWithSHA1AndDESede* PBEWithSHA1AndRC2_40* PBKDF2WithHmacSHA1*/
public static final String ALGORITHM = "PBEWITHMD5andDES";
/*** 迭代次数*/
public static final int ITERATION_COUNT = 100;public byte[] initPbeSalt() {return SecureRandom.getSeed(8);
}public SecretKey generatePbeSecretKey(String password) throws Exception {// 生成SecretKeyfinal PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());final SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);return secretKeyFactory.generateSecret(pbeKeySpec);
}// 数据加密
public byte[] pbeEncryptData(String password, byte[] salt, byte[] rawData) throws Exception {// 生成SecretKeyfinal SecretKey secretKey = generatePbeSecretKey(password);// 构建PBEParameterSpecfinal PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, ITERATION_COUNT);// 数据加密Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);return cipher.doFinal(rawData);
}// 数据解密
public byte[] pbeDecryptData(String password, byte[] salt, byte[] encryptData) throws Exception {// 生成SecretKeyfinal SecretKey secretKey = generatePbeSecretKey(password);// 构建PBEParameterSpecfinal PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, ITERATION_COUNT);// 数据加密Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParameterSpec);return cipher.doFinal(encryptData);
}@Test
public void testPbe() throws Exception {// 初始化"盐"final byte[] salt = initPbeSalt();// 生成密钥String password = "password";final SecretKey secretKey = generatePbeSecretKey(password);// 原文final String rawMessage = "我是阿汤哥";// 加密后final byte[] encryptBytes = pbeEncryptData(password, salt, rawMessage.getBytes());// 解密后final byte[] decryptedBytes = pbeDecryptData(password, salt, encryptBytes);// 输出System.out.println("原文: " + rawMessage + "\n" +"密码: " + password + "\n" +"盐: " + Base64.toBase64String(salt) + "\n" +"加密后:" + Base64.toBase64String(encryptBytes) + "\n" +"解密后:" + new String(decryptedBytes));
}
输出结果
原文: 我是阿汤哥
密码: password
盐: ZDdkibobCyE=
加密后:3PIwmYdqtJ7OPtvbPfkdLQ==
解密后:我是阿汤哥
这篇关于PKI - 对称加密算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!