java 字符串转mac_java字符串加解密和取得本机mac地址

该博客详细介绍了使用Java实现DES对称加密和解密的方法,以及SHA-1散列算法的运用。通过`Security`类提供的静态方法,可以对字符串进行加密和解密操作,同时提供了获取MAC地址的辅助功能。博客内容涵盖了关键的加密解密步骤,包括秘钥生成、Cipher初始化和加密解密过程。
摘要由CSDN通过智能技术生成

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值