首页 > 代码库 > java 对称加解密

java 对称加解密

对称加密

1.特点

  加密密钥与解密密钥相同

2.分类

  2.1 DES

  2.2 3DES

  2.3 AES

  2.4 PBE 

3.各种加密示例

  3.1 DES

    

import java.security.Key;

import java.security.Security;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.binary.Hex;

import org.bouncycastle.jce.provider.BouncyCastleProvider;


public class DES

{


public static void main(String[] args) throws Exception

{

getJkdDes("测试 test");

getBCDes("测试 test");

}


public static void getJkdDes(String src) throws Exception

{

// KEY的生成器

KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");

keyGenerator.init(56); // 密钥长度

// 生成KEY

SecretKey generateKey = keyGenerator.generateKey();

byte[] encodedKey = generateKey.getEncoded();

// KEY的转换

DESKeySpec desKeySpec = new DESKeySpec(encodedKey);

SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");

Key convertSecretKey = factory.generateSecret(desKeySpec);

// 加密

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);

byte[] bytes = src.getBytes();

byte[] result = cipher.doFinal(bytes);

System.out.println("jkd des encrypt : " + Hex.encodeHexString(result));

// 解密

cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);

result = cipher.doFinal(result);

System.out.println("jkd des decrypt : " + new String(result));

}

public static void getBCDes(String src) throws Exception

{

// 增加一个BC的provider

Security.addProvider(new BouncyCastleProvider());

// KEY的生成器

KeyGenerator keyGenerator = KeyGenerator.getInstance("DES","BC");

keyGenerator.getProvider();// 获取当前的加密provider SunJCE version 1.7

keyGenerator.init(56);

// 生成KEY

SecretKey generateKey = keyGenerator.generateKey();

byte[] encodedKey = generateKey.getEncoded();

// KEY的转换

DESKeySpec desKeySpec = new DESKeySpec(encodedKey);

SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");

Key convertSecretKey = factory.generateSecret(desKeySpec);

// 加密

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);

byte[] bytes = src.getBytes();

byte[] result = cipher.doFinal(bytes);

System.out.println("bc des encrypt : " + Hex.encodeHexString(result));

// 解密

cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);

result = cipher.doFinal(result);

System.out.println("bc des decrypt : " + new String(result));

}

}


3.2 3DES


import java.security.Key;

import java.security.SecureRandom;

import java.security.Security;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESedeKeySpec;

import org.apache.commons.codec.binary.Hex;

import org.bouncycastle.jce.provider.BouncyCastleProvider;


public class Securty3DES

{


public static void main(String[] args) throws Exception

{

getJkd3Des("测试 test");

getBC3Des("测试 test");

}


public static void getJkd3Des(String src) throws Exception

{

// KEY的生成器

KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");

// keyGenerator.init(168);

keyGenerator.init(new SecureRandom()); // 根据加密算法自动初始化默认长度

// 生成KEY

SecretKey generateKey = keyGenerator.generateKey();

byte[] encodedKey = generateKey.getEncoded();

// KEY的转换

DESedeKeySpec desKeySpec = new DESedeKeySpec(encodedKey);

SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");

Key convertSecretKey = factory.generateSecret(desKeySpec);

System.out.println(Hex.encodeHexString(convertSecretKey.getEncoded())); 

// 加密

Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);

byte[] bytes = src.getBytes();

byte[] result = cipher.doFinal(bytes);

System.out.println("jkd 3des encrypt : " + Hex.encodeHexString(result));


// 解密

cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);

result = cipher.doFinal(result);

System.out.println("jkd 3des decrypt : " + new String(result));

}


public static void getBC3Des(String src) throws Exception

{

// 增加一个BC的provider

Security.addProvider(new BouncyCastleProvider());

// KEY的生成器

KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede","BC");

// keyGenerator.init(168);

keyGenerator.init(new SecureRandom());

// 生成KEY

SecretKey generateKey = keyGenerator.generateKey();

byte[] encodedKey = generateKey.getEncoded();


// KEY的转换

DESedeKeySpec desKeySpec = new DESedeKeySpec(encodedKey);

SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");

Key convertSecretKey = factory.generateSecret(desKeySpec);


// 加密

Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);

byte[] bytes = src.getBytes();

byte[] result = cipher.doFinal(bytes);

System.out.println("jkd 3des encrypt : " + Hex.encodeHexString(result));


// 解密

cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);

result = cipher.doFinal(result);

System.out.println("jkd 3des decrypt : " + new String(result));

}

}


3.3 AES

import java.security.Key;

import java.security.SecureRandom;

import java.security.Security;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import org.bouncycastle.util.encoders.Base64;


public class AES

{


public static void main(String[] args) throws Exception

{

getJdkAes("测试 test");

getBCAes("测试 test");


}


public static void getJdkAes(String src) throws Exception

{

// 生成key的生成器

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

keyGenerator.init(new SecureRandom());

SecretKey generateKey = keyGenerator.generateKey();

byte[] keyBytes = generateKey.getEncoded();

// key的转换

Key key = new SecretKeySpec(keyBytes, "AES");

// 加密

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] result = cipher.doFinal(src.getBytes());

System.out.println("jdk aec encrypt : " + Base64.toBase64String(result));

// 解密

cipher.init(Cipher.DECRYPT_MODE, key);

result = cipher.doFinal(result);

System.out.println("jdk aec decrypt : " + new String(result));

}

public static void getBCAes(String src) throws Exception

{

Security.addProvider(new BouncyCastleProvider());

// 生成key的生成器

KeyGenerator keyGenerator = KeyGenerator.getInstance("AES","BC");

keyGenerator.init(128);

SecretKey generateKey = keyGenerator.generateKey();

byte[] keyBytes = generateKey.getEncoded();

// key的转换

Key key = new SecretKeySpec(keyBytes, "AES");

// 加密

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] result = cipher.doFinal(src.getBytes());

System.out.println("jdk aec encrypt : " + Base64.toBase64String(result));

// 解密

cipher.init(Cipher.DECRYPT_MODE, key);

result = cipher.doFinal(result);

System.out.println("jdk aec decrypt : " + new String(result));

}

}


3.4 PBE

import java.security.Key;

import java.security.SecureRandom;

import java.security.Security;

import javax.crypto.Cipher;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.PBEKeySpec;

import javax.crypto.spec.PBEParameterSpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import org.bouncycastle.util.encoders.Base64;


public class PBE

{


public static void main(String[] args) throws Exception

{

getJkdPBE("测试 test");

getBCPBE("测试 test");

}


public static void getJkdPBE(String src) throws Exception

{

// 初始化盐

SecureRandom secureRandom = new SecureRandom();

byte[] salt = secureRandom.generateSeed(8);

// 生成口令密钥

String password = "yyc";

PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");

Key key = secretKeyFactory.generateSecret(pbeKeySpec);

// 加密

PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, 100);

Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");

cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);

byte[] result = cipher.doFinal(src.getBytes());

System.out.println("jkd peb encrypt : " + Base64.toBase64String(result));

// 解密

cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);

result = cipher.doFinal(result);

System.out.println("jkd peb decrypt : " + new String(result));

}

public static void getBCPBE(String src) throws Exception

{

Security.addProvider(new BouncyCastleProvider());

// 初始化盐

SecureRandom secureRandom = new SecureRandom();

byte[] salt = secureRandom.generateSeed(8);

// 生成口令密钥

String password = "yyc";

PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBEWITHMD5andDES","BC");

Key key = secretKeyFactory.generateSecret(pbeKeySpec);

// 加密

PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, 100);

Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");

cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);

byte[] result = cipher.doFinal(src.getBytes());

System.out.println("BC peb encrypt : " + Base64.toBase64String(result));

// 解密

cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);

result = cipher.doFinal(result);

System.out.println("BC peb decrypt : " + new String(result));

}


}



java 对称加解密