DES 加密

package com.huateng.ebank.framework.security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


/**
* DES encryption algorithm, providing the encryption and decryption
* algorithm for byte array and string
*/

public class CryptionData {
// The length of Encryptionstring should be 8 bytes and not be
// a weak key
private String EncryptionString;

// The initialization vector should be 8 bytes
private final byte[] EncryptionIV = "abcdefgh".getBytes();
private final static String DES = "DES/CBC/PKCS5Padding";

/**
* Saving key for encryption and decryption
* @param EncryptionString String
*/
public CryptionData(String EncryptionString) {
this.EncryptionString = EncryptionString;
}

/**
* Encrypt a byte array
* @param SourceData byte[]
* @throws Exception
* @return byte[]
*/
public byte[] EncryptionByteData(byte[] SourceData) throws Exception {
byte[] retByte = null;

// Create SecretKey object

byte[] EncryptionByte = EncryptionString.getBytes();
DESKeySpec dks = new DESKeySpec(EncryptionByte);

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);

// Create IvParameterSpec object with initialization vector
IvParameterSpec spec=new IvParameterSpec(EncryptionIV);

// Create Cipter object
Cipher cipher = Cipher.getInstance(DES);

// Initialize Cipher object
cipher.init(Cipher.ENCRYPT_MODE, securekey, spec);

// Encrypting data
retByte = cipher.doFinal(SourceData);
return retByte;
}

/**
* Decrypt a byte array
* @param SourceData byte[]
* @throws Exception
* @return byte[]
*/
public byte[] DecryptionByteData(byte[] SourceData) throws Exception {
byte[] retByte = null;

// Create SecretKey object
byte[] EncryptionByte = EncryptionString.getBytes();
DESKeySpec dks = new DESKeySpec(EncryptionByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);

// Create IvParameterSpec object with initialization vector
IvParameterSpec spec=new IvParameterSpec(EncryptionIV);

// Create Cipter object
Cipher cipher = Cipher.getInstance(DES);

// Initialize Cipher object
cipher.init(Cipher.DECRYPT_MODE, securekey, spec);

// Decrypting data
retByte = cipher.doFinal(SourceData);

return retByte;
}

/**
* Encrypt a string
* @param SourceData String
* @throws Exception
* @return String
*/
public String EncryptionStringData(String SourceData) throws Exception {
String retStr = null;
byte[] retByte = null;

// Transform SourceData to byte array
byte[] sorData = SourceData.getBytes();

// Encrypte data
retByte = EncryptionByteData(sorData);

// Encode encryption data
BASE64Encoder be = new BASE64Encoder();
retStr = be.encode(retByte);

return retStr;
}

/**
* Decrypt a string
* @param SourceData String
* @throws Exception
* @return String
*/
public String DecryptionStringData(String SourceData) throws Exception {
String retStr = null;
byte[] retByte = null;

// Decode encryption data
BASE64Decoder bd = new BASE64Decoder();
byte[] sorData = bd.decodeBuffer(SourceData);

// Decrypting data
retByte = DecryptionByteData(sorData);
retStr = new String(retByte);

return retStr;
}

public static void main(String[] args){
if(args.length < 1){
System.err.print("Error Using : args should contain the cmd[encryption || decryption || help || ?) 、password and key");
System.exit(-1);
}
String cmd = args[0];
if( "encryption".equalsIgnoreCase(cmd)){
if(args.length != 3){
System.err.print("Error Using : the method of encryption need the password and key");
System.exit(-1);
}
System.out.println("-------------welcome using CryPtionData----------------");
System.out.println(" encryption strat ");
System.out.println("password = " + args[1]);
System.out.println("key = " + args[2]);
CryptionData cryptionData = new CryptionData(args[2]);
try{
String envPwd = cryptionData.EncryptionStringData(args[1]);
System.out.println("encry_password = " + envPwd);
System.out.println("-------------------Success , bye!----------------------");
System.exit(0);
}catch(Exception ex){
ex.printStackTrace();
System.err.println("------------------fail , try it again!-----------------");
System.exit(-1);
}
}else if( "decryption".equalsIgnoreCase(cmd)){
if(args.length != 3){
System.err.print("Error Using : the method of decryption need the encry_password and key");
System.exit(-1);
}
System.out.println("-------------welcome using CryPtionData----------------");
System.out.println(" decryption strat ");
System.out.println("encry_password = " + args[1]);
System.out.println("key = " + args[2]);
CryptionData cryptionData = new CryptionData(args[2]);
try{
String pwd = cryptionData.DecryptionStringData(args[1]);
System.out.println("password = " + pwd);
System.out.println("----------------------Success , bye!--------------------");
System.exit(0);
}catch(Exception ex){
ex.printStackTrace();
System.err.println("----------------fail , try it again!-------------------");
System.exit(-1);
}
}else if( "help".equalsIgnoreCase(cmd) || "?".equalsIgnoreCase(cmd)){
System.out.println("-------------welcome using CryPtionData help----------------");
System.out.println("function: encryption or decryption");
System.out.println("eq. 'java CryptionData encryption password key' , you can get encry password!");
System.out.println("eq. 'java CryptionData decryption encry_password key' , you can get password!");
System.out.println("------------------------Bye Bye!----------------------------");
}else{
System.err.print("Error Using : no support function, you can input 'java CryptionData help'");
System.exit(-1);
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值