Java加密和數字簽名本文主要談一下密碼學中的加密和數字簽名
一
這是一種與消息認證碼結合使用以確保消息完整性的技術
/**
*MessageDigestExample
*Copyright
*/
import java
/**
*單一的消息摘要算法
*/
public class MessageDigestExample{
public static void main(String[] args) throws Exception{
if(args
System
System
}
byte[] plainText=args[
//使用getInstance(
MessageDigest messageDigest=MessageDigest
System
//開始使用算法
messageDigest
System
//輸出算法運算結果
System
}
}
還可以通過消息認證碼來進行加密實現
這裡補充另一個運用消息摘要的方式加密的例子:
public class TestEncrypt {
public TestEncrypt() {
}
/**
* @param strSrc :strSrc is a string will be encrypted
* @param encName : encName is the algorithm name will be used
* encName dafault to
* @return String
*/
public String Encrypt(String strSrc
MessageDigest md = null;
String strDes = null;
byte[] bt = strSrc
try {
if (encName == null || encName
encName =
}
md = MessageDigest
md
strDes = bytes
}
catch (NoSuchAlgorithmException e) {
System
return null;
}
return strDes;
}
public String bytes
String des =
String tmp = null;
for (int i =
tmp = (Integer
if (tmp
des +=
}
des += tmp;
}
return des;
}
public static void main(String[]args) {
TestEncrypt te = new TestEncrypt();
String strSrc =
System
System
System
System
System
System
}
}
另外
public static String generateGUID(HttpServletRequest request) {
String out =
try {
// Construct a string that is comprised of:
// Remote IP Address + Host IP Address + Date (yyyyMMdd) +
// Time (hhmmssSSa) + Requested Path + Session ID +
// HashCode Of ParameterMap
StringBuffer sb = new StringBuffer(
sb
InetAddress ia = InetAddress
sb
sb
String path = request
String pathInfo = request
if (pathInfo != null) {
path += pathInfo;
}
sb
sb
sb
String str = sb
// Now encode the string using an MD
MessageDigest md = MessageDigest
md
byte[] digest = md
StringBuffer hexStr = new StringBuffer(
for (int i =
str = Integer
if (str
str =
}
hexStr
}
out = hexStr
} catch (NoSuchAlgorithmException nsae) {
log
} catch (UnknownHostException uhe) {
log
}
// Return the encrypted string
// components that comprise the plain text string
//
return out;
} // End generateGUID()
消息摘要只能檢查消息的完整性
這種最好理解
使用私鑰加密的話
/**
*PrivateExmaple
*Copyright
*/
import javax
import javax
import java
/**
*私鈅加密
*/
public class PrivateExample{
public static void main(String[] args) throws Exception{
if(args
System
System
}
byte[] plainText=args[
//通過KeyGenerator形成一個key
System
KeyGenerator keyGen=KeyGenerator
keyGen
Key key=keyGen
System
//獲得一個私鈅加密類Cipher
Cipher cipher=Cipher
System
//使用私鈅加密
System
cipher
byte[] cipherText=cipher
System
System
System
cipher
byte[] newPlainText=cipher
System
System
}
}
上面提到
/**
*PublicExample
*Copyright
*/
import java
import javax
import java
import java
/**
*一個簡單的公鈅加密例子
*/
public class PublicExample{
public static void main(String[] args) throws Exception{
if(args
System
System
}
byte[] plainText=args[
//構成一個RSA密鑰
System
KeyPairGenerator keyGen=KeyPairGenerator
keyGen
KeyPair key=keyGen
System
//獲得一個RSA的Cipher類
Cipher cipher=Cipher
System
System
cipher
byte[] cipherText=cipher
System
System
//使用私鈅解密
System
cipher
byte[] newPlainText=cipher
System
System
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26898.html