/** 得到wifi的mac地址
public class DeviceCertification {
public DeviceCertification() {
}
public String getWifiMac(Context mContext) {
WifiManager wifi = (WifiManager) mContext
.getSystemService(mContext.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String mac = info.getMacAddress();
return mac;
}
}
// md5 加密
private String MD5Encrypt(String mac) {
MessageDigest md = null;
String out = null;
try {
md = MessageDigest.getInstance("MD5"); // 可以选择其他算法如SHA
byte[] digest = md.digest(mac.getBytes());
out = byteToString(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return out;
}
// 将byte数组转化为String
private String byteToString(byte[] digest) {
StringBuffer out = new StringBuffer();
String tmp = null;
for (int i = 0; i < digest.length; i++) {
tmp = Integer.toHexString(digest[i] & 0xFF);
if (tmp.length() == 1) {
out.append("0");
}
out.append(tmp);
}
return out.toString();
}
RC4加密
//DES、IDEA、RC2、RC4、SKIPJACK、RC5、AES
public class RC4Tool {
// KeyGenerator 提供对称密钥生成器的功能,支持各种算法
// private KeyGenerator keygen;
// SecretKey 负责保存对称密钥
private SecretKey deskey;
// Cipher负责完成加密或解密工作
private Cipher c;
// 该字节数组负责保存加密的结果
private byte[] cipherByte;
private static final String KEY = "xxxx";
public RC4Tool() throws NoSuchAlgorithmException, NoSuchPaddingException {
// Security.addProvider(new com.sun.crypto.provider.SunJCE());
// keygen = KeyGenerator.getInstance("RC4");
// 生成密钥
// deskey = keygen.generateKey();
deskey = new SecretKeySpec(KEY.getBytes(), "RC4");
c = Cipher.getInstance("RC4");
}
/**
* 对字符串加密
*
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
private byte[] Encrytor(String sn) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, deskey);
String tmp = sn + generate();
byte[] src = tmp.getBytes();
// 加密,结果保存进cipherByte
cipherByte = c.doFinal(src);
return cipherByte;
}
/**
* 对字符串解密
*
* @param buff
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
c.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = c.doFinal(buff);
return cipherByte;
}
private String generate() {
long time = System.currentTimeMillis();
int len = String.valueOf(time).length();
return String.valueOf(time).substring(0, len);
}
}