時(shí)間:2015-06-28 00:00:00 來(lái)源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評(píng)論(0)
?? 1. MD5加密,常用于加密用戶名密碼,當(dāng)用戶驗(yàn)證時(shí)。
?protected byte[] encrypt(byte[] obj) ...{
try ...{
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(obj);
return md5.digest();
} catch (NoSuchAlgorithmException e) ...{
e.printStackTrace();
}
}?
??? 2. SHA加密,與MD5相似的用法,只是兩者的算法不同。
?protected byte[] encrypt(byte[] obj) ...{
try ...{
MessageDigest sha = MessageDigest.getInstance("SHA");
sha.update(obj);
return sha.digest();
} catch (NoSuchAlgorithmException e) ...{
e.printStackTrace();
}
}?
??? 3. RSA加密,RAS加密允許解密。常用于文本內(nèi)容的加密。
?import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher; /** *//**
* RSAEncrypt
*
* @author maqujun
* @see
*/
public class RSAEncrypt ...{
/** *//**
* Main method for RSAEncrypt.
* @param args
*/
public static void main(String[] args) ...{
try ...{
RSAEncrypt encrypt = new RSAEncrypt();
String encryptText = "encryptText";
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
// Generate keys
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes());
byte[] de = encrypt.decrypt(privateKey,e);
System.out.println(encrypt.bytesToString(e));
System.out.println(encrypt.bytesToString(de));
} catch (Exception e) ...{
e.printStackTrace();
}
}
/** *//**
* Change byte array to String.
* @return byte[]
*/
protected String bytesToString(byte[] encrytpByte) ...{
String result = "";
for (Byte bytes : encrytpByte) ...{
result += (char) bytes.intValue();
}
return result;
}
/** *//**
* Encrypt String.
* @return byte[]
*/
protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj) ...{
if (publicKey != null) ...{
try ...{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(obj);
} catch (Exception e) ...{
e.printStackTrace();
}
}
return null;
}
/** *//**
* Basic decrypt method
* @return byte[]
*/
protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) ...{
if (privateKey != null) ...{
try ...{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(obj);
} catch (Exception e) ...{
e.printStackTrace();
}
}
return null;
}
}
關(guān)鍵詞標(biāo)簽:Java加密算法
相關(guān)閱讀
熱門(mén)文章 eclipse中如何設(shè)置字體大小_eclipse字體大小設(shè)置方法 jsp 實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì) Eclipse優(yōu)化設(shè)置教程_Eclipse卡頓優(yōu)化設(shè)置技巧 JS截取字符串常用方法詳細(xì)整理
人氣排行 JS驗(yàn)證日期格式是否正確 Java中3DES加密解密調(diào)用示例 Java技術(shù)-J2EE開(kāi)發(fā)日記-MyEclipse快捷鍵與插件大全 eclipse中如何設(shè)置字體大小_eclipse字體大小設(shè)置方法 Eclipse優(yōu)化設(shè)置教程_Eclipse卡頓優(yōu)化設(shè)置技巧 JavaScript基本語(yǔ)法-常量和變量 用Java刪除文件夾里的所有文件 100多個(gè)很有用的JavaScript函數(shù)以及基礎(chǔ)寫(xiě)法匯總