非对称加密

信息发送者:用私钥对信息进行签名,使用信息接收方的公钥对信息加密。

信息接收方:用信息发送者的公钥验证信息发送者的身份,使用私钥对加密信息解密。

 

 

公钥可公开发布,用于发送方加密要发送的信息,私钥用于接收方解密接收到的加密内容。公私钥对计算时间较长,主要用于加密较少的数据。常用的非对称加密算法有RSA和ECC。

非对称加密

非对称加密

非对称加密

非对称加密

非对称加密

非对称加密

公有链上交易数据全网可见,公众可以跟踪这些交易,任何人可以通过观察区块链得出关于某事的结论,不利于个人或机构的合法隐私保护。针对该类风险的应对策略是:第一,由认证机构代理用户在区块链上进行交易,用户资料和个人行为不进入区块链。第二,不采用全网广播方式,而是将交易数据的传输限制在正在进行相关交易的节点之间。第三,对用户数据的访问采用权限控制,持有**的访问者才能解密和访问数据。第四,采用例如“零知识证明”等隐私保护算法,规避隐私暴露。

[html] view plain copy

  1. <code class="language-html">  /**  
  2.    * 非对称加密和解密用到的模数(modulus)  
  3.    */  
  4.   private static String modulus = "978C0A92D2173439707498F0944AA476B1B62595877DD6FA87F" +  
  5.           "E2AC6DCB3D0BF0B82857439C99B5091192BC134889DFF60C562EC54EFBA4FF2F9D55ADBCCEA4" +  
  6.           "2FBA80CB398ED501280A007C83AF30C3D1A142D6133C63012B90AB26AC60C898FB66EDC3192C3E" +  
  7.           "C4FF66925A64003B72496099F4F09A9FB72A2CF9E4D770C4";  
  8.   
  9.   /**  
  10.    * 非对称加密用到的加***(component)  
  11.    */  
  12.   private static String exponent = "010001";  
  13.   
  14.   /**  
  15.    * rsa加密对象  
  16.    */  
  17.   private static Cipher cipher;  
  18.   
  19.   /**  
  20.   * 初始化rsa加密对象  
  21.   */  
  22.   static {  
  23.     byte[] modulusBytes = hexStringToByte(modulus);  
  24.     byte[] exponentBytes = hexStringToByte(exponent);  
  25.     BigInteger modulusInteger = new BigInteger(1, modulusBytes);  
  26.     BigInteger exponentInteger = new BigInteger(1, exponentBytes);  
  27.     RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulusInteger, exponentInteger);  
  28.     try {  
  29.       KeyFactory fact = KeyFactory.getInstance("RSA");  
  30.       PublicKey pubKey = fact.generatePublic(rsaPubKey);  
  31.       cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");  
  32.       cipher.init(Cipher.ENCRYPT_MODE, pubKey);  
  33.     } catch (NoSuchAlgorithmException e) {  
  34.       e.printStackTrace();  
  35.     } catch (InvalidKeySpecException e) {  
  36.       e.printStackTrace();  
  37.     } catch (NoSuchPaddingException e) {  
  38.       e.printStackTrace();  
  39.     } catch (InvalidKeyException e) {  
  40.       e.printStackTrace();  
  41.     }  
  42.   }  
  43.   
  44.   /**  
  45.    * 将字符串进行非对称加密。  
  46.    *  
  47.    * @param string  
  48.    *        要加密的字符串明文  
  49.    * @return 加密后的字符串  
  50.    */  
  51.   public static String encrypt(String string) {  
  52.     byte[] cipherData = null;  
  53.     try {  
  54.       cipherData = cipher.doFinal(string.getBytes("UTF-8"));  
  55.     } catch (IllegalBlockSizeException e) {  
  56.       e.printStackTrace();  
  57.     } catch (BadPaddingException e) {  
  58.       e.printStackTrace();  
  59.     } catch (UnsupportedEncodingException e) {  
  60.       e.printStackTrace();  
  61.     }  
  62.     return byteToHexString(cipherData);  
  63.   }  
  64.   
  65.   /**  
  66.    * 将字节数组转换为十六进制字符串  
  67.    *  
  68.    * @param bytes  
  69.    *        字节数组  
  70.    * @return 十六进制字符串  
  71.    */  
  72.   private static String byteToHexString(byte[] bytes) {  
  73.     String string = new BigInteger(1, bytes).toString(16);  
  74.     if (string.length() % 2 != 0) {  
  75.       string = "0" + string;  
  76.     }  
  77.     return string;  
  78.   }  
  79.   
  80.   /**  
  81.    * 将十六进制字符串转换为byte数组  
  82.    *  
  83.    * @param hexString  
  84.    *        十六进制字符串  
  85.    * @return 转换后的字节数组  
  86.    */  
  87.   private static byte[] hexStringToByte(String hexString) {  
  88.     byte[] digest = new byte[hexString.length() / 2];  
  89.     for (int i = 0; i < digest.length; i++) {  
  90.       String byteString = hexString.substring(2 * i, 2 * i + 2);  
  91.       int byteValue = Integer.parseInt(byteString, 16);  
  92.       digest[i] = (byte) byteValue;  
  93.     }  
  94.     return digest;  
  95.   }</code>  

 

 

转自  https://blog.****.net/huanleyuan/article/details/79272974