对称加密算法 之 PBE 篇
算法 |
密钥长度 |
默认 |
工作模式 |
填充方式 |
实现 |
PBEWithMD5AndDES |
56 |
56 |
CBC |
PKCS5Padding |
JDK |
PBEWithMD5AndTripleDES |
112、168 |
168 |
|||
PBEWithSHA1AndDESede |
112、168 |
168 |
|||
PBEWithSHA1AndRC2_40 |
40~1024(8倍数) |
128 |
算法 |
密钥长度 |
默认 |
工作模式 |
填充方式 |
实现 |
PBEWithMD5AndDES |
64 |
64 |
CBC |
PKCS5Padding PKCS7Padding ISO10126Paddiing ZeroBytePadding |
BC |
PBEWithMD5AndRC2 |
112 |
128 |
|||
PBEWithSHA1AndDES |
64 |
64 |
|||
PBEWithSHA1AndRC2 |
128 |
128 |
|||
PBEWithSHAAndIDEA-CBC |
128 |
128 |
|||
PBEWithSHAAnd2-KeyTripleDES-CBC |
128 |
128 |
|||
PBEWithSHAAnd3-KeyTripleDES-CBC |
192 |
192 |
|||
PBEWithSHAAnd128BitRC2-CBC |
128 |
128 |
|||
PBEWithSHAAnd40BitRC2-CBC |
40 |
40 |
|||
PBEWithSHAAnd128BitRC4 |
128 |
128 |
|||
PBEWithSHAAnd40BitRC4 |
40 |
40 |
|||
PBEWithSHAAndTwofish-CBC |
256 |
256 |
package com.baikeyang.EncrptionProject;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.apache.commons.codec.binary.Base64;
public class PBEEncrption {
public static void main(String[] args) {
jdkPBE();
}
static String str = "采用JDK实现对称加密PBE算法";
public static void jdkPBE(){
try {
//初始化盐
SecureRandom random = new SecureRandom();
byte[] salt =random.generateSeed(8);
// 口令与密钥
String password = "baikeyang.com";
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
Key key = factory.generateSecret(pbeKeySpec);
// 加密
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, 100);
Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
byte[] result = cipher.doFinal(str.getBytes());
System.out.println(Base64.encodeBase64String(result));
// 解密
cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
result = cipher.doFinal(result);
System.out.println(new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
后记:
本次总共给大家分享了4中对称加密算法:DES 3DES AES PBE
Same failure, also 19″