java绕过加密密码_Java密码加密与解密

importjava.io.UnsupportedEncodingException;importjava.net.URLDecoder;importjava.security.InvalidKeyException;importjava.security.MessageDigest;importjava.security.NoSuchAlgorithmException;importjavax.crypto.Cipher;importjavax.crypto.SecretKey;importjavax.crypto.SecretKeyFactory;importjavax.crypto.spec.DESKeySpec;

@SuppressWarnings("unused")public classPasswordUtils {//加密

@SuppressWarnings({})public staticString encrypt(String plainText) {

String result= "";try{

DESKeySpec keySpec= new DESKeySpec("CampusHelper".getBytes("UTF8"));

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

SecretKey key=keyFactory.generateSecret(keySpec);

sun.misc.BASE64Encoder base64encoder= newsun.misc.BASE64Encoder();

sun.misc.BASE64Decoder base64decoder= newsun.misc.BASE64Decoder();//ENCODE plainTextPassword String

byte[] cleartext = plainText.getBytes("UTF8");

Cipher cipher= Cipher.getInstance("DES"); //cipher is not thread//safe

cipher.init(Cipher.ENCRYPT_MODE, key);

result=base64encoder.encode(cipher.doFinal(cleartext));

}catch(Exception e) {

e.printStackTrace();

}returnresult;

}//解密

@SuppressWarnings({})public staticString decrypt(String encryptedPwd)throwsInvalidKeyException, UnsupportedEncodingException {

String result= "";try{

DESKeySpec keySpec= new DESKeySpec("CampusHelper".getBytes("UTF8"));

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

SecretKey key=keyFactory.generateSecret(keySpec);

sun.misc.BASE64Encoder base64encoder= newsun.misc.BASE64Encoder();

sun.misc.BASE64Decoder base64decoder= newsun.misc.BASE64Decoder();//DECODE encryptedPwd String

byte[] encrypedPwdBytes =base64decoder.decodeBuffer(encryptedPwd);

Cipher cipher= Cipher.getInstance("DES");//cipher is not thread//safe

cipher.init(Cipher.DECRYPT_MODE, key);byte[] plainTextPwdBytes =(cipher.doFinal(encrypedPwdBytes));

result= newString(plainTextPwdBytes);

}catch(Exception e) {

System.out.println("解密失败:"+encryptedPwd);

result=againDecrypt(encryptedPwd);

}returnresult;

}public staticString againDecrypt(String encryptedPwd)throwsInvalidKeyException, UnsupportedEncodingException {

String result= "";try{

encryptedPwd=URLDecoder.decode(encryptedPwd,"utf-8");

DESKeySpec keySpec= new DESKeySpec("CampusHelper".getBytes("UTF8"));

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

SecretKey key=keyFactory.generateSecret(keySpec);

sun.misc.BASE64Encoder base64encoder= newsun.misc.BASE64Encoder();

sun.misc.BASE64Decoder base64decoder= newsun.misc.BASE64Decoder();//DECODE encryptedPwd String

byte[] encrypedPwdBytes =base64decoder.decodeBuffer(encryptedPwd);

Cipher cipher= Cipher.getInstance("DES");//cipher is not thread//safe

cipher.init(Cipher.DECRYPT_MODE, key);byte[] file=cipher.doFinal(encrypedPwdBytes);byte[] plainTextPwdBytes =(file);

result= newString(plainTextPwdBytes);

}catch(Exception e) {//e.printStackTrace();

System.out.println("重新解密失败:"+encryptedPwd);

}returnresult;

}public staticString generateMD5(String plainText) {

String result= "";try{

MessageDigest md= MessageDigest.getInstance("MD5");

md.update(plainText.getBytes());byte[] digest =md.digest();

StringBuffer sb= newStringBuffer();for (byteb : digest) {

sb.append(Integer.toHexString(b& 0xff));

}

result=sb.toString();

}catch(NoSuchAlgorithmException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returnresult;

}public static voidmain(String[] args) {try{//System.out.println(decrypt(URLDecoder.decode("BrhT6ggt4%252BI%253D","utf-8")));//System.out.println(decrypt(URLDecoder.decode(URLDecoder.decode("BrhT6ggt4%252BI%253D","utf-8"))));//System.out.println(URLEncoder.encode(encrypt("41"), "utf-8"));//System.out.println(encrypt("41"));//long now = System.currentTimeMillis();//System.out.println(now);//now = now - now / 10000000000L * 10000000000L;//System.out.println(now);

System.out.println(decrypt("DAD0dZU0S/w/pJcrfPW4Fw=="));//System.out.println(encrypt("321088199511214887"));

} catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值