版权归作者所有,任何形式转载请联系作者。
目录:
*生成指定位数的随机秘钥
*对随机秘钥进行MD5加密(不可逆)
*对随机秘钥进行DES加密(可逆)
*对加密秘钥进行DES解密
其中DES加密解密涉及到二进制和16进制转码,方法代码如下:
所有方法调用的时候都亲测有效。
/**
* 生成指定位数的随机秘钥
* @param KeyLength
* @return Keysb
*/
public String KeyCreate(int KeyLength){
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
Random random = new Random();
StringBuffer Keysb = new StringBuffer();
for(int i = 0; i<KeyLength; i++) //生成指定位数的随机秘钥字符串
{
int number = random.nextInt(base.length());
Keysb.append(base.charAt(number));
}
return Keysb.toString();
}
/**
* 对随机秘钥进行MD5加密
* @param Keysb
* @return SecretKey
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public String OnSecreatKey(String Keysb) throws NoSuchAlgorithmException, UnsupportedEncodingException{
MessageDigest md5 = MessageDigest.getInstance("MD5");
BASE64Encoder base64en = new B