使用DESede/DES对称算法实现加密解密
DES是单钥密码体制的代表。
单钥密码体制是一种传统的加密算法,是指信息的发送方和接收方共同使用同一把密钥进行加解密。
通常,使用的加密算法 比较简便高效,密钥简短,加解密速度快,破译极其困难。但是加密的安全性依靠密钥保管的安全性,在公开的计算机网络上安全地传送和保管密钥是一个严峻的问题,并且如果在多用户的情况下密钥的保管安全性也是一个问题。
单钥密码体制的代表是美国的DES
下面是用Sun提供的安全包实现DESede/DES对称算法加密解密
/* 安全程序 DESede/DES测试 */ import java.security.*; import javax.crypto.*; public class testdes { public static void main(String[] args){ testdes my=new testdes(); my.run(); } public void run() { //添加新安全算法,如果用JCE就要把它添加进去 Security.addProvider(new com.sun.crypto.provider.SunJCE()); String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish String myinfo="要加密的信息"; try { //生成密钥 KeyGenerator keygen = KeyGenerator.getInstance(Algorithm); SecretKey deskey = keygen.generateKey(); //加密 System.out.println("加密前的二进串:"+byte2hex(myinfo.getBytes())); System.out.println("加密前的信息:"+myinfo); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE,deskey); byte[] cipherByte=c1.doFinal(myinfo.getBytes()); System.out.println("加密后的二进串:"+byte2hex(cipherByte)); //解密 c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.DECRYPT_MODE,deskey); byte[] clearByte=c1.doFinal(cipherByte); System.out.println("解密后的二进串:"+byte2hex(clearByte)); System.out.println("解密后的信息:"+(new String(clearByte))); } catch (java.security.NoSuchAlgorithmException e1) {e1.printStackTrace();} catch (javax.crypto.NoSuchPaddingException e2) {e2.printStackTrace();} catch (java.lang.Exception e3) {e3.printStackTrace();} } public String byte2hex(byte[] b) //二行制转字符串 { String hs=""; String stmp=""; for (int n=0;n<b.length;n++) { stmp=(java.lang.Integer.toHexString(b[n] & 0XFF)); if (stmp.length()==1) hs=hs+"0"+stmp; else hs=hs+stmp; if (n<b.length-1) hs=hs+":"; } return hs.toUpperCase(); } }
Java代码来自:Java上加密算法的实现用例
使用消息摘要加密用户密码
消息摘要算法的主要特征是加密过程不需要密钥,并且经过加密的数据无法被解密,只有输入相同的明文数据经过相同的消息摘要算法才能得到相同的密文。消息摘要算法不存在密钥的管理与分发问题,适合于分布式网络相同上使用。由于其加密计算的工作量相当可观,所以以前的这种算法通常只用于数据量有限的情况下的加密,例如计算机的口令就是用不可逆加密算法加密的。近年来,随着计算机相同性能的飞速改善,加密速度不再成为限制这种加密技术发展的桎梏,因而消息摘要算法应用的领域不断增加。
现在,消息摘要算法主要应用在“数字签名”领域,作为对明文的摘要算法。著名的摘要算法有RSA公司的MD5算法和SHA-1算法及其大量的变体。
一个消息摘要就是一个数据块的数字指纹。即对一个任意长度的一个数据块进行计算,产生一个唯一指印(对于SHA1是产生一个20字节的二进制数组)。
消息摘要有两个基本属性:
- 两个不同的报文难以生成相同的摘要
- 难以对指定的摘要生成一个报文,而由该报文反推算出该指定的摘要
代表:美国国家标准技术研究所的SHA1和麻省理工学院Ronald Rivest提出的MD5
下面是一个用Sun公司提供的算法包来实现消息摘要计算:
import java.security.*; public class myDigest { public static void main(String[] args) { myDigest my=new myDigest(); my.testDigest(); } public void testDigest() { try { String myinfo="我的测试信息"; //java.security.MessageDigest alg=java.security.MessageDigest.getInstance("MD5"); java.security.MessageDigest alga=java.security.MessageDigest.getInstance("SHA-1"); alga.update(myinfo.getBytes()); byte[] digesta=alga.digest(); System.out.println("本信息摘要是:"+byte2hex(digesta)); //通过某中方式传给其他人你的信息(myinfo)和摘要(digesta) 对方可以判断是否更改或传输正常 java.security.MessageDigest algb=java.security.MessageDigest.getInstance("SHA-1"); algb.update(myinfo.getBytes()); if (algb.isEqual(digesta,algb.digest())) { System.out.println("信息检查正常"); } else { System.out.println("摘要不相同"); } } catch (java.security.NoSuchAlgorithmException ex) { System.out.println("非法摘要算法"); } } public String byte2hex(byte[] b) //二行制转字符串 { String hs=""; String stmp=""; for (int n=0;n<b.length;n++) { stmp=(java.lang.Integer.toHexString(b[n] & 0XFF)); if (stmp.length()==1) hs=hs+"0"+stmp; else hs=hs+stmp; if (n<b.length-1) hs=hs+":"; } return hs.toUpperCase(); } }
使用ROME解析RSS/ATOM
ROME is an set of open source Java tools for parsing, generating and publishing RSS and Atom feeds. The core ROME library depends only on the JDOM XML parser and supports parsing, generating and converting all of the popular RSS and Atom formats including RSS 0.90, RSS 0.91 Netscape, RSS 0.91 Userland, RSS 0.92, RSS 0.93, RSS 0.94, RSS 1.0, RSS 2.0, Atom 0.3, and Atom 1.0. You can parse to an RSS object model, an Atom object model or an abstract SyndFeed model that can model either family of formats.
下面是一个使用ROME解析google博客输出RSS的java代码
import java.io.IOException;
import java.net.MalformedURLException;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.io.FeedException;
/**
*
* 使用rome rss和atom feeds工具解析google的博客rss
*
*/
public class TestRome {
public static void main(String[] args) {
// 如果程序位于防火墙后面,就需要在程序中加上一些Proxy设置。
java.util.Properties systemSettings = System.getProperties();
systemSettings.put("http.proxyHost", "wsay.net");
systemSettings.put("http.proxyPort", "8080");
System.setProperties(systemSettings);
String urlStr = "http://blogsearch.google.cn/blogsearch_feeds?hl=zh-CN&um=1&q=site:itindex.net&lr=&ie=utf-8&num=10&output=rss";
// java.net.URLConnection feedUrl = new java.net.URL("file:D:/atom.xhtml").openConnection();
java.net.URLConnection feedUrl=null;
try {
feedUrl = new java.net.URL(urlStr).openConnection();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
feedUrl.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; http://itindex.net)");
com.sun.syndication.io.SyndFeedInput input = new com.sun.syndication.io.SyndFeedInput();
com.sun.syndication.feed.synd.SyndFeed feed=null;
try {
feed = input
.build(new com.sun.syndication.io.XmlReader(feedUrl));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FeedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String feedType = feed.getFeedType();// 获取原feed属于哪种格式
System.out.println(feedType);
java.util.List list = feed.getEntries();
for (int i = 0; i < list.size(); i++) {
com.sun.syndication.feed.synd.SyndEntry entry = (com.sun.syndication.feed.synd.SyndEntry) list
.get(i);
String entryTitle = entry.getLink();
System.out.println("PublishedDate:"+entry.getPublishedDate());
System.out.println("Title:"+entry.getTitle());
System.out.println("Link:"+entry.getLink());
System.out.println("category:"+entry.getCategories());
System.out.println("Description:"+entry.getDescription());
if(entry.getContents().size()>0){
SyndContent syndContent=(SyndContent)entry.getContents().get(0);
System.out.println("Content:"+syndContent.getValue());
}
System.out.println("----------------------------:");
}
}
}