对称加密中DES的加密与解密算法
2017-12-20 16:42
507 查看
package Encoder.symmetry;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* 对称加密算法 可逆加密算法
* DES/3DES/AES的不同加密算法
* @author CUICHUNCHI
*
*DES:通过keygenerator获取密钥生成器实例后,设置des的算法密钥为56,便可生成密钥
*并且每次生成的密钥都不相同,为了存储,将其密钥通过base64编码转为字符串,将密钥字符串解码后实例化SecretKey
*,只需要实例化SecretKeySpec即可,返回一个SecretKey
*加密与解密都需要Cipher对象,初始化时,需要传入加密与解密对应的模式,(Cipher.ENCRYPT_MODE/Cipher.DECRYPT_MODE)
*以及传入对应的SecretKey密钥即可加密与解密
*/
public class DESUtil {
private static final String DES = "DES";
/**
* 生成 DES 的密钥
* @throws NoSuchAlgorithmException
*/
public static byte[] getKeyDES() throws NoSuchAlgorithmException{
KeyGenerator key = KeyGenerator.getInstance(DES);
key.init(56);
SecretKey generateKey = key.generateKey();
byte[] encoded = generateKey.getEncoded();
// StringBuffer hexValue = new StringBuffer();
/* for (int i = 0; i < encoded.length; i++) {
int val = ((int) encoded[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}*/
return encoded;
}
//加载生成一个SecrectKeySpec的实例,返回一个密钥SecretKey
private static SecretKey loadKeyDES(byte [] desStr){
SecretKey key = new SecretKeySpec(desStr,"DES");
return key;
}
/**
* 对原始字符进行DES加密
* @param str
* @param key
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] encryptDES(String str,SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(str.getBytes());
return doFinal;
}
/**
* 解密
* @param b
* @return
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static String decryptDES(byte [] b,SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(b);
return new String(doFinal);
}
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
String str ="崔春驰";
//生成密钥
byte[] keyStr = DESUtil.getKeyDES();
SecretKey key = loadKeyDES(keyStr);
System.out.println("生成的密钥:"+key);
//调用加密
byte[] encryptDES = encryptDES(str, key);
System.out.println("加密后:"+encryptDES);
String decryptDES = decryptDES(encryptDES, key);
System.out.println("解密:"+decryptDES);
}
}
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* 对称加密算法 可逆加密算法
* DES/3DES/AES的不同加密算法
* @author CUICHUNCHI
*
*DES:通过keygenerator获取密钥生成器实例后,设置des的算法密钥为56,便可生成密钥
*并且每次生成的密钥都不相同,为了存储,将其密钥通过base64编码转为字符串,将密钥字符串解码后实例化SecretKey
*,只需要实例化SecretKeySpec即可,返回一个SecretKey
*加密与解密都需要Cipher对象,初始化时,需要传入加密与解密对应的模式,(Cipher.ENCRYPT_MODE/Cipher.DECRYPT_MODE)
*以及传入对应的SecretKey密钥即可加密与解密
*/
public class DESUtil {
private static final String DES = "DES";
/**
* 生成 DES 的密钥
* @throws NoSuchAlgorithmException
*/
public static byte[] getKeyDES() throws NoSuchAlgorithmException{
KeyGenerator key = KeyGenerator.getInstance(DES);
key.init(56);
SecretKey generateKey = key.generateKey();
byte[] encoded = generateKey.getEncoded();
// StringBuffer hexValue = new StringBuffer();
/* for (int i = 0; i < encoded.length; i++) {
int val = ((int) encoded[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}*/
return encoded;
}
//加载生成一个SecrectKeySpec的实例,返回一个密钥SecretKey
private static SecretKey loadKeyDES(byte [] desStr){
SecretKey key = new SecretKeySpec(desStr,"DES");
return key;
}
/**
* 对原始字符进行DES加密
* @param str
* @param key
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] encryptDES(String str,SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(str.getBytes());
return doFinal;
}
/**
* 解密
* @param b
* @return
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static String decryptDES(byte [] b,SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(b);
return new String(doFinal);
}
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
String str ="崔春驰";
//生成密钥
byte[] keyStr = DESUtil.getKeyDES();
SecretKey key = loadKeyDES(keyStr);
System.out.println("生成的密钥:"+key);
//调用加密
byte[] encryptDES = encryptDES(str, key);
System.out.println("加密后:"+encryptDES);
String decryptDES = decryptDES(encryptDES, key);
System.out.println("解密:"+decryptDES);
}
}
相关文章推荐
- Java加密解密快速入门下篇【包括MD5、BASE64、DES、RSA等算法】
- Java加密解密快速入门下篇【包括MD5、BASE64、DES、RSA等算法】
- Java加密/解密算法快速入门下篇[包括MD5、BASE64、DES、RSA等]
- 使用对称加密与解密算法对文件加密与解密
- Java加密解密快速入门上篇【包括MD5、BASE64、DES、RSA等算法】
- .NET中的加密解密:私钥加密(对称加密):AES、DES、RC2、Rijindael、TripleDES
- DES 加密解密算法的C++实现
- c# DESEncrypt 加密、解密算法
- .NET中的加密解密:私钥加密(对称加密):DES
- java实现对称加密AES和DES的加密、解密
- 常用加密解密算法【RSA、AES、DES、MD5】介绍和使用
- .NET中的加密解密:私钥加密(对称加密):AES、DES、RC2、Rijindael、TripleDES
- 加密解密算法【RSA、AES、DES、MD5】介绍和使用
- Java DES 加密和解密算法的使用
- DES 加密 解密算法
- 加密解密算法java实现(2)—DES
- 加密解密算法java实现(2)—DES
- 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解
- Java加密解密快速入门【包括MD5、BASE64、DES、RSA等算法】
- Java加密解密快速入门上篇【包括MD5、BASE64、DES、RSA等算法】