zoukankan      html  css  js  c++  java
  • 分布式平台下的HS(HighSecurity) 非对称加密

        非对称加密,在加密和解密的过程中需要不同的密码,即必须具备同时具备两个密码信息才可能获得完整的数据。

      

    image

    图 非对称算法

    在非对称算法中,首先得有一个密钥对,这个密钥对含有两部分内容,分别称作公钥(PK)和私钥(SK),公钥通常用来加密,私钥则用来解密。在对称算法中,也讲到了可以有两个密钥(分为加密和解密密钥)。但是,对称算法中的加解密密钥可以互相转换,而在非对称算法中,则不能从公钥推算出私钥。所以,我们完全可以将公钥公开到任何地方。

    如上图所以,发送者用接收方公开出来的公钥PK进行加密。接受方在收到密文后,再用与公钥对应的私钥SK进行解密。同样,密文可以被截获,但是由于截获者只有公钥,没有私钥,他不能进行解密。

        实例: 需要对密码进行非对称加密

       产生密匙对,并将数据加密

    publicEnrypt
     1 private static void PublicEnrypt() throws Exception{
     2    
     3     Cipher cipher=Cipher.getInstance("RSA");
     4    
     5    //实例化Key
     6     KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
     7      
     8 
     9    //获取一对钥匙
    10    KeyPair keyPair =keyPairGenerator.generatorKeyPair();
    11  
    12    //获取公匙
    13    Key publicKey=keyPair.getPublic();
    14 
    15    //获取私匙
    16    Key privateKey=keyPair.getPrivate();
    17 
    18    //用公匙加密
    19    cipher.init(Cipher.ENCRYPT_MODE,publicKey);
    20    
    21       
    22    byte [] result=cipher.doFinal("helloword".getBytes("UTF-8"));
    23    
    24    //将Key写入到文件
    25   saveKey(privateKey,"zxx_private.key");
    26   
    27    //加密后的数据写入到文件
    28   saveData(result,"public_encryt.dat");  
    29 
    30 }

       使用私匙进行解密

    privateDecrypt
     1 private static void privateDecrypt() throws Exception{
     2    Cipher cipher=Cipher.getInstance("RSA");
     3     
     4    //得到Key
     5   Key privateKey=readKey("zxx_private.key");
     6  
     7   //用私匙去解密
     8   cipher.init(Cipher.DECRYPT_MODE,privateKey);
     9 
    10   
    11   //读数据源
    12   byte[] src= readData("public_encryt.dat"); 
    13   
    14  //得到解密后的结果
    15   byte[] result=cipher.doFinal(src); 
    16   
    17  //二进制数据要变成字符串需要解码
    18   System.out.println(new String(result,"UTF-8"));
    19 }

     将加密后的数据保存到文件中

    saveData
    1 private static void saveData(byte[] result, String fileName) throws Exception {  
    2     // TODO Auto-generated method stub   
    3     FileOutputStream fosData=new FileOutputStream(fileName);  
    4     fosData.write(result);  
    5     fosData.close();  
    6 }  

    将密匙保存到文件中

    saveKey
    1 public static void saveKey(Key key,String fileName)throws Exception{  
    2     FileOutputStream fosKey=new FileOutputStream(fileName);  
    3     ObjectOutputStream oosSecretKey =new ObjectOutputStream(fosKey);  
    4     oosSecretKey.writeObject(key);  
    5     oosSecretKey.close();  
    6     fosKey.close();  
    7 }  

    从文件中读取出私匙

    readKey
    1 private static Key readKey(String fileName) throws Exception {  
    2     FileInputStream fisKey=new FileInputStream(fileName);  
    3     ObjectInputStream oisKey =new ObjectInputStream(fisKey);  
    4     Key key=(Key)oisKey.readObject();  
    5     oisKey.close();  
    6     fisKey.close();  
    7     return key;  
    8 }  

    读取数据从文件中

    readData
     1 private static byte[] readData(String filename) throws Exception {  
     2     FileInputStream fisDat=new FileInputStream(filename);  
     3     byte [] src=http://www.cnblogs.com/new byte [fisDat.available()];  
     4     int len =fisDat.read(src);  
     5     int total =0;  
     6     while(total<src.length){  
     7         total +=len;  
     8         len=fisDat.read(src,total,src.length-total);  
     9     }  
    10     fisDat.close();  
    11     return src;  
    12 }  

       

  • 相关阅读:
    卡特兰数
    hdu 1023 Train Problem II
    hdu 1022 Train Problem
    hdu 1021 Fibonacci Again 找规律
    java大数模板
    gcd
    object dection资源
    Rich feature hierarchies for accurate object detection and semantic segmentation(RCNN)
    softmax sigmoid
    凸优化
  • 原文地址:https://www.cnblogs.com/jerryxing/p/2531205.html
Copyright © 2011-2022 走看看