import java.io.*;
import javax.crypto.*; import javax.crypto.spec.*; import java.security.*; public class desede { public static void main(String args[]) throws Exception { if (args[0].compareTo("+") == 0)// 控制是加密还是减密 { KeyGenerator kg;// 生产密锁的工厂 FileInputStream fin;// 明文所在的文件 FileOutputStream fout;// 密文所在的文件 kg = KeyGenerator.getInstance("DESede");// 产生DESede算的密锁 fin = new FileInputStream(args[1]);// 打开文件 fout = new FileOutputStream(args[1] + ".DESede");// 写密文文件 kg.init(168);// 初始化密锁 SecretKey k = kg.generateKey();// 得到密锁 Cipher cp = Cipher.getInstance("DESede");// 生成加密工厂对象,DESede为加密算法 cp.init(Cipher.ENCRYPT_MODE, k);// 初始化加密器,Cipher.ENCRYPT_MODE指定加密,k为密锁 File f = new File(args[1]);// 得到明文大小 int num = (int) f.length(); byte[] rb = new byte[num]; fin.read(rb);// 读取明文 byte[] cb = cp.doFinal(rb);// 加密,将加密结果放在cb字节数组中 fout.write(cb);// 写入文件 fin.close(); fout.close(); fout = new FileOutputStream("key.DESede");// 保存密锁 fout.write(k.getEncoded()); fout.close(); System.out.print("\r\n\r\n"); System.out.print("加密文件" + args[1] + "完成\r\n"); System.out.print("密锁文件key.DESede\r\n"); return; } if (args[0].compareTo("-") == 0)// 减密时 { FileInputStream fin;// 用于打开密锁文件和密明文件 FileOutputStream fout;// 写明文文件 fin = new FileInputStream("key.DESede"); File f = new File("key.DESede"); int num = (int) f.length(); byte[] kb = new byte[num]; fin.read(kb);// 读到密锁 fin.close(); SecretKeySpec k = new SecretKeySpec(kb, "DESede");// 将密锁存在SecretKey对象中 Cipher cp = Cipher.getInstance("DESede");// 生成解密工厂,DESede为算法 cp.init(Cipher.DECRYPT_MODE, k);// 初始化解密,Cipher.DECRYPT_MODE为解密方式,k为密锁 fout = new FileOutputStream(args[1]); fin = new FileInputStream(args[1] + ".DESede"); f = new File(args[1] + ".DESede"); num = (int) f.length(); kb = new byte[num]; fin.read(kb);// 读取密文 byte[] m = cp.doFinal(kb);// 解密,将结果存在m字节数组中 fout.write(m);// 写入文件 fin.close(); fout.close(); return; } else// 如果不是有效参数,则出现帮助信息 { System.out.print("\r\n\r\n"); System.out.print("DESede算法加密解密:\r\n"); System.out.print(" '-' :减号解密;\r\n"); System.out.print(" filename:解密文件名;\r\n"); System.out.print(" '+' :加号加密;\r\n"); System.out.print(" filename:加密文件名;\r\n"); } }//main }//class
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import net.peoplesolution.wss.conf.SystemConfProp; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class DESede { public DESede() { } public static byte[] getKey(String keystr) { int l = keystr.length() / 2; byte w[] = new byte[l]; for(int i = 0; i < l; i++) w[i] = (byte)(Integer.parseInt(keystr.substring(i * 2, i * 2 + 2), 16) & 0xff); return w; } private static byte[] EncryptionByteData(byte SourceData[], String sKey, String sIV) throws Exception { byte retByte[] = (byte[])null; byte EncryptionByte[] = getKey(sKey); javax.crypto.SecretKey securekey = new SecretKeySpec(EncryptionByte, "DESede"); IvParameterSpec spec = new IvParameterSpec(getKey(sIV)); Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); cipher.init(1, securekey, spec); retByte = cipher.doFinal(SourceData); return retByte; } private static byte[] DecryptionByteData(byte SourceData[], String sKey, String sIV) throws Exception { byte retByte[] = (byte[])null; byte EncryptionByte[] = getKey(sKey); javax.crypto.SecretKey securekey = new SecretKeySpec(EncryptionByte, "DESede"); IvParameterSpec spec = new IvParameterSpec(getKey(sIV)); Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); cipher.init(2, securekey, spec); retByte = cipher.doFinal(SourceData); return retByte; } public static String EncryptCode(String strTobeEnCrypted, String sKey, String sIV) throws Exception { String retStr = null; byte retByte[] = (byte[])null; byte sorData[] = strTobeEnCrypted.getBytes("UTF-8"); retByte = EncryptionByteData(sorData, sKey, sIV); BASE64Encoder be = new BASE64Encoder(); retStr = be.encode(retByte); return retStr; } public static String DecryptCode(String strTobeDeCrypted, String sKey, String sIV) throws Exception { if(strTobeDeCrypted.lastIndexOf(" ") > 0) strTobeDeCrypted = strTobeDeCrypted.replaceAll(" ", "+"); String retStr = null; byte retByte[] = (byte[])null; BASE64Decoder bd = new BASE64Decoder(); byte sorData[] = bd.decodeBuffer(strTobeDeCrypted); retByte = DecryptionByteData(sorData, sKey, sIV); retStr = new String(retByte, "UTF-8"); return retStr; } public static String DecryptCode(String strTobeDeCrypted) throws Exception { return DecryptCode(strTobeDeCrypted, getKey(), getIV()); } public static String EncryptCode(String strTobeEnCrypted) throws Exception { return EncryptCode(strTobeEnCrypted, getKey(), getIV()); } private static String getKey() { if(Key == null || Key.equals("")) Key = SystemConfProp.getInstance().getProperty("key"); return Key; } private static String getIV() { if(IV == null || IV.equals("")) IV = SystemConfProp.getInstance().getProperty("iv"); return IV; } private static final String DESede = "DESede/CBC/PKCS5Padding"; private static String Key; private static String IV; } |