1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.security.Key; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Hex;
public class Encrypt {
private static byte keyiv[] = "12345678".getBytes();
public static String generateKeyByMd5(String str) { String result = ""; try { MessageDigest messageDigest = MessageDigest.getInstance("md5"); byte[] md5Bytes = messageDigest.digest(str.getBytes()); result = Hex.encodeHexString(md5Bytes); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return result; }
public static String encrypt3Des(String str, String key) { String returnStr = ""; try { DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(key.getBytes()); SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede"); Key convertSecretKey = factory.generateSecret(deSedeKeySpec); Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(keyiv); cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey,ips); byte[] result = cipher.doFinal(str.getBytes()); returnStr = Base64.encodeBase64String(result); } catch (Exception e) { } return returnStr; }
public static String decrypt(String str, String key) { String returnStr = ""; try { DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(key.getBytes()); SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede"); Key convertSecretKey = factory.generateSecret(deSedeKeySpec); Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(keyiv); cipher.init(Cipher.DECRYPT_MODE, convertSecretKey,ips); byte[] result = cipher.doFinal(Base64.decodeBase64(str.getBytes())); System.out.println("decrypt : " + new String(result)); returnStr = new String(result, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return returnStr; }
public static String readFile(String path) throws Exception { File f = new File(path); InputStream in = new FileInputStream(f); byte[] b = new byte[(int) f.length()]; for (int i = 0; i < b.length; i++) { b[i] = (byte) in.read(); } in.close(); System.out.println(new String(b)); return new String(b); }
public static void writeFile(String str,String path){ File f=new File(path); try{ f.createNewFile(); OutputStream out =new FileOutputStream(f); byte[] b=str.getBytes(); for (int i = 0; i < b.length; i++) { out.write(b[i]); } out.close(); }catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) { String keyStr = "hello world"; String key = generateKeyByMd5(keyStr); System.out.println("key :" + key);
try { String str = readFile("/home/jay/test"); str = encrypt3Des(str, key); System.out.println("加密后内容:" + str); writeFile(str,"/home/jay/output"); } catch (Exception e) { e.printStackTrace(); } try { String str = readFile("/home/jay/output"); str = decrypt(str, key); System.out.println("解密后内容:" + str); writeFile(str,"/home/jay/output2"); } catch (Exception e) { e.printStackTrace(); } } }
|