java字符串加解密和取得本機mac地址

package com.struts;

 

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.security.Key;

import java.security.MessageDigest;

import javax.crypto.Cipher;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

 

/**

 * Security 提供了一個安全算法類,其中包括對稱密碼算法和散列算法

 */

public final class Security {

    // The length of Encryptionstring should be 8 bytes and not be

    // a weak key

    private final static BASE64Encoder base64encoder = new BASE64Encoder();

    private final static BASE64Decoder base64decoder = new BASE64Decoder();

    private final static String encoding = "UTF-8";

 

    /**

     * 加密字符串

     */

    public static String Encryp(String str) {

String result = str;

if (str != null && str.length() > 0) {

   try {

byte[] encodeByte = symmetricEncrypto(str.getBytes(encoding));

result = base64encoder.encode(encodeByte);

   } catch (Exception e) {

e.printStackTrace();

   }

}

return result;

    }

 

    /**

     * 解密字符串

     */

    public static String Decryp(String str) {

String result = str;

if (str != null && str.length() > 0) {

   try {

byte[] encodeByte = base64decoder.decodeBuffer(str);

byte[] decoder = Security.symmetricDecrypto(encodeByte);

result = new String(decoder, encoding);

   } catch (Exception e) {

e.printStackTrace();

   }

}

return result;

    }

 

    /**

     * 對稱加密方法

     * 

     * @param byteSource

     *            需要加密的數據

     * @return 經過加密的數據

     * @throws Exception

     */

    public static byte[] symmetricEncrypto(byte[] byteSource) throws Exception {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

try {

   int mode = Cipher.ENCRYPT_MODE;

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

   byte[] keyData = { 1, 9, 7, 9, 0, 2, 0, 7 };

   DESKeySpec keySpec = new DESKeySpec(keyData);

   Key key = keyFactory.generateSecret(keySpec);

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

   cipher.init(mode, key);

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

   return result;

} catch (Exception e) {

   throw e;

} finally {

   baos.close();

}

    }

 

    /**

     * 對稱解密方法

     * 

     * @param byteSource

     *            需要解密的數據

     * @return 經過解密的數據

     * @throws Exception

     */

    public static byte[] symmetricDecrypto(byte[] byteSource) throws Exception {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

try {

   int mode = Cipher.DECRYPT_MODE;

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

   byte[] keyData = { 1, 9, 7, 9, 0, 2, 0, 7 };

   DESKeySpec keySpec = new DESKeySpec(keyData);

   Key key = keyFactory.generateSecret(keySpec);

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

   cipher.init(mode, key);

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

   return result;

} catch (Exception e) {

   throw e;

} finally {

   baos.close();

}

    }

 

    /**

     * 散列算法

     * 

     * @param byteSource

     *            需要散列計算的數據

     * @return 經過散列計算的數據

     * @throws Exception

     */

    public static byte[] hashMethod(byte[] byteSource) throws Exception {

try {

   MessageDigest currentAlgorithm = MessageDigest.getInstance("SHA-1");

   currentAlgorithm.reset();

   currentAlgorithm.update(byteSource);

   return currentAlgorithm.digest();

} catch (Exception e) {

   throw e;

}

    }

    public static String getMACAddress() {

String address = "";

String os = System.getProperty("os.name");

if (os != null && os.startsWith("Windows")) {

   try {

String command = "cmd.exe /c ipconfig /all";

Process p = Runtime.getRuntime().exec(command);

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;

while ((line = br.readLine()) != null) {

   if (line.indexOf("Physical Address") > 0) {

int index = line.indexOf(":");

index += 2;

address = line.substring(index);

break;

   }

}

br.close();

return address.trim();

   } catch (IOException e) {

e.printStackTrace();

   }

} else {// linux /sbin/ifconfig

   try {

String command = "/sbin/ifconfig";

Process p = Runtime.getRuntime().exec(command);

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;

while ((line = br.readLine()) != null) {

   if (line.indexOf("HWaddr") > 0) {

int index = line.indexOf("HWaddr");

index += 7;

address = line.substring(index);

break;

   }

}

br.close();

return address.trim();

   } catch (IOException e) {

e.printStackTrace();

   }

}

return address;

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章