<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    本站不再更新,歡迎光臨 java開發技術網
    隨筆-230  評論-230  文章-8  trackbacks-0
    前段時間需要用到這方面的技術,寫了幾個例子,不加文字說明,只貼代碼
    package demo.encrypt;

    import java.io.UnsupportedEncodingException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;

    /**
     * 
     * 摘要加密。檢驗信息完整性 目前廣泛使用的算法有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加密后結果:");
            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;

    /**
     * 私鑰加密,也稱對稱性密碼,加/解密雙方共享同一密鑰
     * 
     * 
    @author peidw
     *
     
    */

    public class PrivateExample {
        
    /**
         * 加必解密例子
         * 
    @throws Exception
         
    */
        
    public void deendemo()throws Exception{
            String str
    ="www.17lotto.com";  //要加密的字符串
            byte[] bstr=str.getBytes("utf-8");
            
    //產生密鑰
            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();
            
    //密鑰保存問題
            
            
    //獲得一個私鈅加密類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"));
            
        }
        
        
    /**
         * 從文件加載密鑰和密文進行解密例子(新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;

    /**
     *  非對稱性加密,也叫公鑰加密 產開兩個密鑰(私鑰,公鑰)私鑰加密只有公鑰才能解樣,同時公鑰加密只有私鑰能解開.
     *  目前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");
            
    //構成一個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");        
            
    //獲得一個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>數字簽名</p>
     * <pre>
     *   使用RSA私鑰對信息摘要簽名,然后用公鑰進行解密
     * </pre>
     * 
    @author peidw
     *
     
    */
    public class DigitalSignature2Example {
        
        
    public void test () throws Exception {
            String str
    ="www.17lotto.com";
            
    byte[] bstr=str.getBytes("utf-8");
            
    //形成RSA公私鑰對
            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"));

            
    //使用公鈅驗證
            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

        }

    }
    posted on 2008-04-15 11:44 有貓相伴的日子 閱讀(1453) 評論(0)  編輯  收藏 所屬分類: jdk
    本站不再更新,歡迎光臨 java開發技術網
    主站蜘蛛池模板: 巨胸喷奶水视频www网免费| 99精品免费视品| 91香蕉成人免费网站| 亚洲av无码不卡一区二区三区| 日本黄页网址在线看免费不卡| 18勿入网站免费永久| 亚洲电影免费在线观看| 国产猛男猛女超爽免费视频| 亚洲精品乱码久久久久久按摩| 永久免费A∨片在线观看| 亚洲国产第一站精品蜜芽| 亚洲第一福利网站| 嫩草在线视频www免费看| 亚洲AV无码国产精品麻豆天美| 久久综合九色综合97免费下载| 亚洲视频在线观看免费| 国产成人精品免费午夜app| 久久精品国产亚洲αv忘忧草| 成人男女网18免费视频| 久久精品国产亚洲av天美18 | 久久久久久久99精品免费观看 | 亚洲精品不卡视频| 成年在线观看网站免费| 亚洲AV无码专区在线厂| 亚洲午夜成人精品电影在线观看| 精品久久久久久无码免费| 亚洲av成人无码久久精品| 国产在线jyzzjyzz免费麻豆| 亚洲av永久中文无码精品综合 | 老司机亚洲精品影视www| 日韩免费人妻AV无码专区蜜桃| 亚洲av永久无码精品天堂久久 | 免费中文字幕一级毛片| 成人无码视频97免费| 日韩亚洲AV无码一区二区不卡| 岛国片在线免费观看| 国产免费黄色无码视频| 亚洲视频一区网站| 免费国产成人午夜电影| 久久成人a毛片免费观看网站| 亚洲中文无码永久免费|