package demo.encrypt;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
* 摘要加密。檢驗(yàn)信息完整性 目前廣泛使用的算法有MD4、MD5、SHA-1
* @author peidw 2008-03-02
*
*/
public class MessageDigestExample {
/**
* 信息摘要完整性加密
*
*/
/**
* 單一摘要算法,不使用密碼
* @param args
* @throws UnsupportedEncodingException
* @throws NoSuchAlgorithmException
*/
public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException {
String str="www.17lotto.com"; //要加密的字符串
byte[] bstr=str.getBytes("utf-8");
MessageDigest messageDigest=MessageDigest.getInstance("SHA-1"); //獲取算法
System.out.println("\n"+messageDigest.getProvider().getInfo());
System.out.println("加密前:\n "+new String(bstr));
messageDigest.update(bstr);
System.out.println("\n加密后結(jié)果:");
System.out.println(new String(messageDigest.digest(),"utf-8"));
}
}
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
* 摘要加密。檢驗(yàn)信息完整性 目前廣泛使用的算法有MD4、MD5、SHA-1
* @author peidw 2008-03-02
*
*/
public class MessageDigestExample {
/**
* 信息摘要完整性加密
*
*/
/**
* 單一摘要算法,不使用密碼
* @param args
* @throws UnsupportedEncodingException
* @throws NoSuchAlgorithmException
*/
public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException {
String str="www.17lotto.com"; //要加密的字符串
byte[] bstr=str.getBytes("utf-8");
MessageDigest messageDigest=MessageDigest.getInstance("SHA-1"); //獲取算法
System.out.println("\n"+messageDigest.getProvider().getInfo());
System.out.println("加密前:\n "+new String(bstr));
messageDigest.update(bstr);
System.out.println("\n加密后結(jié)果:");
System.out.println(new String(messageDigest.digest(),"utf-8"));
}
}
package demo.encrypt;
import java.io.*;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
/**
* 私鑰加密,也稱對(duì)稱性密碼,加/解密雙方共享同一密鑰
*
* @author peidw
*
*/
public class PrivateExample {
/**
* 加必解密例子
* @throws Exception
*/
public void deendemo()throws Exception{
String str="www.17lotto.com"; //要加密的字符串
byte[] bstr=str.getBytes("utf-8");
//產(chǎn)生密鑰
KeyGenerator keyGen=KeyGenerator.getInstance("AES");
keyGen.init(128);
Key key=keyGen.generateKey();
//密鑰保存
File fkey=new File("f:\\key.obj");
OutputStream os=new FileOutputStream(fkey);
os.write(key.getEncoded());
os.flush();
os.close();
//密鑰保存問題
//獲得一個(gè)私鈅加密類Cipher,ECB是加密方式,PKCS5Padding是填充方法
Cipher cipher=Cipher.getInstance("AES/ECB/PKCS5Padding");
System.out.println("\n"+cipher.getProvider().getInfo());
//使用私鈅加密
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] cipherText=cipher.doFinal(bstr);
//密文保存
File cryptograph=new File("f:\\cryptograph.obj");
OutputStream cos=new FileOutputStream(cryptograph);
cos.write(cipherText);
cos.flush();
cos.close();
System.out.println("Finish encryption:");
System.out.println(new String(cipherText,"utf-8"));
System.out.println("\nStart decryption:");
cipher.init(Cipher.DECRYPT_MODE,key);
byte[] newPlainText=cipher.doFinal(cipherText);
System.out.println("Finish decryption:");
System.out.println(new String(newPlainText,"utf-8"));
}
/**
* 從文件加載密鑰和密文進(jìn)行解密例子(新jdk不懂怎么加載)
* @throws Exception
*/
public void decryptionFromFile()throws Exception{
KeyGenerator keyGen=KeyGenerator.getInstance("AES");
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
PrivateExample pe=new PrivateExample();
pe.deendemo();
}
}
import java.io.*;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
/**
* 私鑰加密,也稱對(duì)稱性密碼,加/解密雙方共享同一密鑰
*
* @author peidw
*
*/
public class PrivateExample {
/**
* 加必解密例子
* @throws Exception
*/
public void deendemo()throws Exception{
String str="www.17lotto.com"; //要加密的字符串
byte[] bstr=str.getBytes("utf-8");
//產(chǎn)生密鑰
KeyGenerator keyGen=KeyGenerator.getInstance("AES");
keyGen.init(128);
Key key=keyGen.generateKey();
//密鑰保存
File fkey=new File("f:\\key.obj");
OutputStream os=new FileOutputStream(fkey);
os.write(key.getEncoded());
os.flush();
os.close();
//密鑰保存問題
//獲得一個(gè)私鈅加密類Cipher,ECB是加密方式,PKCS5Padding是填充方法
Cipher cipher=Cipher.getInstance("AES/ECB/PKCS5Padding");
System.out.println("\n"+cipher.getProvider().getInfo());
//使用私鈅加密
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] cipherText=cipher.doFinal(bstr);
//密文保存
File cryptograph=new File("f:\\cryptograph.obj");
OutputStream cos=new FileOutputStream(cryptograph);
cos.write(cipherText);
cos.flush();
cos.close();
System.out.println("Finish encryption:");
System.out.println(new String(cipherText,"utf-8"));
System.out.println("\nStart decryption:");
cipher.init(Cipher.DECRYPT_MODE,key);
byte[] newPlainText=cipher.doFinal(cipherText);
System.out.println("Finish decryption:");
System.out.println(new String(newPlainText,"utf-8"));
}
/**
* 從文件加載密鑰和密文進(jìn)行解密例子(新jdk不懂怎么加載)
* @throws Exception
*/
public void decryptionFromFile()throws Exception{
KeyGenerator keyGen=KeyGenerator.getInstance("AES");
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
PrivateExample pe=new PrivateExample();
pe.deendemo();
}
}
package demo.encrypt;
import java.io.*;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
/**
* 非對(duì)稱性加密,也叫公鑰加密 產(chǎn)開兩個(gè)密鑰(私鑰,公鑰)私鑰加密只有公鑰才能解樣,同時(shí)公鑰加密只有私鑰能解開.
* 目前JDK5提供的RSA算法
* @author peidw
*
*/
public class PublicExample {
/**
* 加密解密例子
* @throws Exception
*/
public void deenDemo()throws Exception{
String str="www.17lotto.com";
byte bstr[]=str.getBytes("utf-8");
//構(gòu)成一個(gè)RSA密鑰
System.out.println("\nStart generating RSA key");
KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key=keyGen.generateKeyPair();
//保存公/私密鑰
File pubfile=new File("f:\\public.dat");
File prifile=new File("f:\\private.dat");
OutputStream pubos=new FileOutputStream(pubfile);
OutputStream prios=new FileOutputStream(prifile);
pubos.write(key.getPublic().getEncoded());
prios.write(key.getPrivate().getEncoded());
pubos.flush();
prios.flush();
pubos.close();
prios.close();
System.out.println("Finish generating RSA key");
//獲得一個(gè)RSA的Cipher類,使用公鈅加密
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println("\n"+cipher.getProvider().getInfo());
System.out.println("\nStart encryption");
cipher.init(Cipher.ENCRYPT_MODE,key.getPublic());
byte[] cipherText=cipher.doFinal(bstr);
File pub_cryptograph=new File("f:\\pub_cryptograph.dat");
OutputStream os=new FileOutputStream(pub_cryptograph);
os.write(cipherText);
os.flush();
os.close();
System.out.println("Finish encryption:");
System.out.println(new String(cipherText,"UTF8"));
//使用私鈅解密
System.out.println("\nStart decryption");
cipher.init(Cipher.DECRYPT_MODE,key.getPrivate());
byte[] newPlainText=cipher.doFinal(cipherText);
System.out.println("Finish decryption:");
System.out.println(new String(newPlainText,"UTF8"));
}
/**
* 加裁私鑰,解密公鑰加密的文的文件
* @throws Exception
*/
public void fromfielEnDeDemo()throws Exception{
File prifile=new File("f:\\private.dat");
FileInputStream fsprivateKey = new FileInputStream(prifile);
BufferedInputStream bfsprivateKey = new BufferedInputStream(fsprivateKey);
byte[] byteprivateKey = new byte[bfsprivateKey.available()];
bfsprivateKey.read(byteprivateKey);
bfsprivateKey.close();
//X509EncodedKeySpec priKeySpec = new X509EncodedKeySpec(byteprivateKey); 公鑰加載法
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(byteprivateKey); //私鑰加載
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey priKey = keyFactory.generatePrivate(priKeySpec);
System.out.println(priKey.getFormat());
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE,priKey);
File pubcryptographfile=new File("f:\\pub_cryptograph.dat");
FileInputStream pubcis = new FileInputStream(pubcryptographfile);
byte cstr[]=new byte[pubcis.available()];
pubcis.read(cstr);
pubcis.close();
byte[] newPlainText=cipher.doFinal(cstr);
System.out.println("Finish decryption:");
System.out.println(new String(newPlainText,"UTF8"));
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
PublicExample pe=new PublicExample();
pe.fromfielEnDeDemo();
}
}
import java.io.*;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
/**
* 非對(duì)稱性加密,也叫公鑰加密 產(chǎn)開兩個(gè)密鑰(私鑰,公鑰)私鑰加密只有公鑰才能解樣,同時(shí)公鑰加密只有私鑰能解開.
* 目前JDK5提供的RSA算法
* @author peidw
*
*/
public class PublicExample {
/**
* 加密解密例子
* @throws Exception
*/
public void deenDemo()throws Exception{
String str="www.17lotto.com";
byte bstr[]=str.getBytes("utf-8");
//構(gòu)成一個(gè)RSA密鑰
System.out.println("\nStart generating RSA key");
KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key=keyGen.generateKeyPair();
//保存公/私密鑰
File pubfile=new File("f:\\public.dat");
File prifile=new File("f:\\private.dat");
OutputStream pubos=new FileOutputStream(pubfile);
OutputStream prios=new FileOutputStream(prifile);
pubos.write(key.getPublic().getEncoded());
prios.write(key.getPrivate().getEncoded());
pubos.flush();
prios.flush();
pubos.close();
prios.close();
System.out.println("Finish generating RSA key");
//獲得一個(gè)RSA的Cipher類,使用公鈅加密
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println("\n"+cipher.getProvider().getInfo());
System.out.println("\nStart encryption");
cipher.init(Cipher.ENCRYPT_MODE,key.getPublic());
byte[] cipherText=cipher.doFinal(bstr);
File pub_cryptograph=new File("f:\\pub_cryptograph.dat");
OutputStream os=new FileOutputStream(pub_cryptograph);
os.write(cipherText);
os.flush();
os.close();
System.out.println("Finish encryption:");
System.out.println(new String(cipherText,"UTF8"));
//使用私鈅解密
System.out.println("\nStart decryption");
cipher.init(Cipher.DECRYPT_MODE,key.getPrivate());
byte[] newPlainText=cipher.doFinal(cipherText);
System.out.println("Finish decryption:");
System.out.println(new String(newPlainText,"UTF8"));
}
/**
* 加裁私鑰,解密公鑰加密的文的文件
* @throws Exception
*/
public void fromfielEnDeDemo()throws Exception{
File prifile=new File("f:\\private.dat");
FileInputStream fsprivateKey = new FileInputStream(prifile);
BufferedInputStream bfsprivateKey = new BufferedInputStream(fsprivateKey);
byte[] byteprivateKey = new byte[bfsprivateKey.available()];
bfsprivateKey.read(byteprivateKey);
bfsprivateKey.close();
//X509EncodedKeySpec priKeySpec = new X509EncodedKeySpec(byteprivateKey); 公鑰加載法
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(byteprivateKey); //私鑰加載
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey priKey = keyFactory.generatePrivate(priKeySpec);
System.out.println(priKey.getFormat());
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE,priKey);
File pubcryptographfile=new File("f:\\pub_cryptograph.dat");
FileInputStream pubcis = new FileInputStream(pubcryptographfile);
byte cstr[]=new byte[pubcis.available()];
pubcis.read(cstr);
pubcis.close();
byte[] newPlainText=cipher.doFinal(cstr);
System.out.println("Finish decryption:");
System.out.println(new String(newPlainText,"UTF8"));
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
PublicExample pe=new PublicExample();
pe.fromfielEnDeDemo();
}
}
package demo.encrypt;
import java.security.*;
/**
* <p>數(shù)字簽名</p>
* <pre>
* 使用RSA私鑰對(duì)信息摘要簽名,然后用公鑰進(jìn)行解密
* </pre>
* @author peidw
*
*/
public class DigitalSignature2Example {
public void test () throws Exception {
String str="www.17lotto.com";
byte[] bstr=str.getBytes("utf-8");
//形成RSA公私鑰對(duì)
System.out.println("\nStart generating RSA key");
KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key=keyGen.generateKeyPair();
Signature sig=Signature.getInstance("SHA1WithRSA");
sig.initSign(key.getPrivate());
sig.update(bstr);
byte[] signature=sig.sign();
System.out.println(sig.getProvider().getInfo());
System.out.println("\nSignature:");
System.out.println(new String(signature,"utf-8"));
//使用公鈅驗(yàn)證
System.out.println("\nStart signature verification");
sig.initVerify(key.getPublic());
sig.update(bstr);
try{
if(sig.verify(signature)){
System.out.println("Signature verified");
}else System.out.println("Signature failed");
}catch(SignatureException e){
System.out.println("Signature failed");
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
import java.security.*;
/**
* <p>數(shù)字簽名</p>
* <pre>
* 使用RSA私鑰對(duì)信息摘要簽名,然后用公鑰進(jìn)行解密
* </pre>
* @author peidw
*
*/
public class DigitalSignature2Example {
public void test () throws Exception {
String str="www.17lotto.com";
byte[] bstr=str.getBytes("utf-8");
//形成RSA公私鑰對(duì)
System.out.println("\nStart generating RSA key");
KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key=keyGen.generateKeyPair();
Signature sig=Signature.getInstance("SHA1WithRSA");
sig.initSign(key.getPrivate());
sig.update(bstr);
byte[] signature=sig.sign();
System.out.println(sig.getProvider().getInfo());
System.out.println("\nSignature:");
System.out.println(new String(signature,"utf-8"));
//使用公鈅驗(yàn)證
System.out.println("\nStart signature verification");
sig.initVerify(key.getPublic());
sig.update(bstr);
try{
if(sig.verify(signature)){
System.out.println("Signature verified");
}else System.out.println("Signature failed");
}catch(SignatureException e){
System.out.println("Signature failed");
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}