中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java加密算法 RSA

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用

公鑰加密也稱為非對稱加密、速度慢、加密和解密的鑰匙不相同,某一個(gè)人持有私鑰,任何人都可以知道公鑰

import java.security.KeyPair;  
import java.security.KeyPairGenerator;  
import java.security.PrivateKey;  
import java.security.PublicKey;  
import java.util.Arrays;  
  
import javax.crypto.Cipher;  
  
/** 
 * RSA算法 公鑰加密 非對稱加密 
 * @author stone 
 * @date 2014-03-11 00:28:38 
 */  
public class RSA {  
    public static final String KEY_ALGORITHM = "RSA";  
    public static final String CIPHER_ALGORITHM_ECB1 = "RSA/ECB/PKCS1Padding";  
    public static final String CIPHER_ALGORITHM_ECB2 = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding"; //不能用  
    public static final String CIPHER_ALGORITHM_ECB3 = "OAEPWithSHA-256AndMGF1Padding"; //不能用  
      
    static PublicKey publicKey;  
    static PrivateKey privateKey;  
    static Cipher cipher;  
    static KeyPair keyPair;  
      
    public static void main(String[] args) throws Exception {  
        method1("斯柯達(dá)U*(Sfsad7f()*^%%$");  
        method2("斯柯達(dá)U*(Sfsad7f()*^%%$");  
        method3("斯柯達(dá)U*(Sfsad7f()*^%%$");  
          
    }  
      
    /** 
     * 公鑰加密,私鑰解密    使用默認(rèn)CIPHER_ALGORITHM_ECB1 
     * @param str 
     * @throws Exception 
     */  
    static void method1(String str) throws Exception {  
        KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);  
        KeyPair keyPair = keyGenerator.generateKeyPair();  
        publicKey = keyPair.getPublic();  
        privateKey = keyPair.getPrivate();  
        cipher = Cipher.getInstance(KEY_ALGORITHM);  
        cipher.init(Cipher.ENCRYPT_MODE, publicKey); //公鑰加密  
        byte[] encrypt = cipher.doFinal(str.getBytes());  
        System.out.println("公鑰加密后1:" + Arrays.toString(encrypt));  
          
        cipher.init(Cipher.DECRYPT_MODE, privateKey);//私鑰解密  
        byte[] decrypt = cipher.doFinal(encrypt);  
        System.out.println("私鑰解密后1:" + new String(decrypt));  
    }  
      
    /** 
     * 私鑰加密,公鑰解密    使用默認(rèn)CIPHER_ALGORITHM_ECB1 
     * @param str 
     * @throws Exception 
     */  
    static void method2(String str) throws Exception {  
        KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);  
        KeyPair keyPair = keyGenerator.generateKeyPair();  
        publicKey = keyPair.getPublic();  
        privateKey = keyPair.getPrivate();  
        cipher = Cipher.getInstance(KEY_ALGORITHM);  
        cipher.init(Cipher.ENCRYPT_MODE, privateKey); //私鑰加密  
        byte[] encrypt = cipher.doFinal(str.getBytes());  
        System.out.println("私鑰加密后2:" + Arrays.toString(encrypt));  
          
        cipher.init(Cipher.DECRYPT_MODE, publicKey);//公鑰解密  
        byte[] decrypt = cipher.doFinal(encrypt);  
        System.out.println("公鑰解密后2:" + new String(decrypt));  
    }  
      
    /** 
     * 私鑰加密,公鑰解密    使用CIPHER_ALGORITHM_ECB1 = RSA/ECB/PKCS1Padding 
     * @param str 
     * @throws Exception 
     */  
    static void method3(String str) throws Exception {  
        KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);  
        KeyPair keyPair = keyGenerator.generateKeyPair();  
        publicKey = keyPair.getPublic();  
        privateKey = keyPair.getPrivate();  
        cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB1);  
        cipher.init(Cipher.ENCRYPT_MODE, privateKey); //私鑰加密  
        byte[] encrypt = cipher.doFinal(str.getBytes());  
        System.out.println("私鑰加密后3:" + Arrays.toString(encrypt));  
          
        cipher.init(Cipher.DECRYPT_MODE, publicKey);//公鑰解密  
        byte[] decrypt = cipher.doFinal(encrypt);  
        System.out.println("公鑰解密后3:" + new String(decrypt));  
    }  
}  

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:C#將base64編碼的字符串轉(zhuǎn)換成bitmap位圖

下一篇:SwipeRefreshLayout 官方的下拉刷新組件使用示例