zoukankan      html  css  js  c++  java
  • Java 进行 RSA 加解密时不得不考虑到的那些事儿

    1. 加密的系统不要具备解密的功能,否则 RSA 可能不太合适

    公钥加密,私钥解密。加密的系统和解密的系统分开部署,加密的系统不应该同时具备解密的功能,这样即使黑客攻破了加密系统,他拿到的也只是一堆无法破解的密文数据。否则的话,你就要考虑你的场景是否有必要用 RSA 了。

    2. 可以通过修改生成密钥的长度来调整密文长度

    生成密文的长度等于密钥长度。密钥长度越大,生成密文的长度也就越大,加密的速度也就越慢,而密文也就越难被破解掉。著名的"安全和效率总是一把双刃剑"定律,在这里展现的淋漓尽致。我们必须通过定义密钥的长度在"安全"和"加解密效率"之间做出一个平衡的选择。

    3. 生成密文的长度和明文长度无关,但明文长度不能超过密钥长度

    不管明文长度是多少,RSA 生成的密文长度总是固定的。
    但是明文长度不能超过密钥长度。比如 Java 默认的 RSA 加密实现不允许明文长度超过密钥长度减去 11(单位是字节,也就是 byte)。也就是说,如果我们定义的密钥(我们可以通过 java.security.KeyPairGenerator.initialize(int keysize) 来定义密钥长度)长度为 1024(单位是位,也就是 bit),生成的密钥长度就是 1024位 / 8位/字节 = 128字节,那么我们需要加密的明文长度不能超过 128字节 -
    11 字节 = 117字节。也就是说,我们最大能将 117 字节长度的明文进行加密,否则会出问题(抛诸如 javax.crypto.IllegalBlockSizeException: Data must not be longer than 53 bytes 的异常)。
    而 BC 提供的加密算法能够支持到的 RSA 明文长度最长为密钥长度。

    4. byte[].toString() 返回的实际上是内存地址,不是将数组的实际内容转换为 String

    警惕 toString 陷阱:Java 中数组的 toString() 方法返回的并非数组内容,它返回的实际上是数组存储元素的类型以及数组在内存的位置的一个标识。
    大部分人跌入这个误区而不自知,包括一些写了多年 Java 的老鸟。比如这篇博客《How To Convert Byte[] Array To String In Java》中的代码
    [java] view plain copy
     print?
    1. public class TestByte  
    2. {      
    3.     public static void main(String[] argv) {  
    4.    
    5.             String example = "This is an example";  
    6.             byte[] bytes = example.getBytes();  
    7.    
    8.             System.out.println("Text : " + example);  
    9.             System.out.println("Text [Byte Format] : " + bytes);  
    10.             System.out.println("Text [Byte Format] : " + bytes.toString());  
    11.    
    12.             String s = new String(bytes);  
    13.             System.out.println("Text Decryted : " + s);  
    14.    
    15.    
    16.     }  
    17. }  

    输出:
    Text : This is an example
    Text [Byte Format] : [B@187aeca
    Text [Byte Format] : [B@187aeca
    Text Decryted : This is an example

    以及这篇博客《RSA Encryption Example》中的代码
    [java] view plain copy
     print?
    1. final byte[] cipherText = encrypt(originalText, publicKey);  
    2. System.out.println("Encrypted: " +cipherText.toString());  

    输出:
    [B@4c3a8ea3
    这些输出其实都是字节数组在内存的位置的一个标识,而不是作者所认为的字节数组转换成的字符串内容。如果我们对密钥以 byte[].toString() 进行持久化存储或者和其他一些字符串打 json 传输,那么密钥的解密者得到的将只是一串毫无意义的字符,当他解码的时候很可能会遇到 "javax.crypto.BadPaddingException" 异常。

    5. 字符串用以保存文本信息,字节数组用以保存二进制数据

    java.lang.String 保存明文,byte 数组保存二进制密文,在 java.lang.String 和 byte[] 之间不应该具备互相转换。如果你确实必须得使用 java.lang.String 来持有这些二进制数据的话,最安全的方式是使用 Base64(推荐 Apache 的 commons-codec 库的 org.apache.commons.codec.binary.Base64):
    [java] view plain copy
     print?
    1. // use String to hold cipher binary data  
    2. Base64 base64 = new Base64();   
    3. String cipherTextBase64 = base64.encodeToString(cipherText);  
    4.   
    5. // get cipher binary data back from String  
    6. byte[] cipherTextArray = base64.decode(cipherTextBase64);  

    6. 每次生成的密文都不一致证明你选用的加密算法很安全

    一个优秀的加密必须每次生成的密文都不一致,即使每次你的明文一样、使用同一个公钥。因为这样才能把明文信息更安全地隐藏起来。
    Java 默认的 RSA 实现是 "RSA/None/PKCS1Padding"(比如 Cipher cipher = Cipher.getInstance("RSA");句,这个 Cipher 生成的密文总是不一致的),Bouncy Castle 的默认 RSA 实现是 "RSA/None/NoPadding"。
    为什么 Java 默认的 RSA 实现每次生成的密文都不一致呢,即使每次使用同一个明文、同一个公钥?这是因为 RSA 的 PKCS #1 padding 方案在加密前对明文信息进行了随机数填充。
    你可以使用以下办法让同一个明文、同一个公钥每次生成同一个密文,但是你必须意识到你这么做付出的代价是什么。比如,你可能使用 RSA 来加密传输,但是由于你的同一明文每次生成的同一密文,攻击者能够据此识别到同一个信息都是何时被发送。
    [java] view plain copy
     print?
    1. Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    2. final Cipher cipher = Cipher.getInstance("RSA/None/NoPadding""BC");  

    7. 可以通过调整算法提供者来减小密文长度

    Java 默认的 RSA 实现 "RSA/None/PKCS1Padding" 要求最小密钥长度为 512 位(否则会报 java.security.InvalidParameterException: RSA keys must be at least 512 bits long 异常),也就是说生成的密钥、密文长度最小为 64 个字节。如果你还嫌大,可以通过调整算法提供者来减小密文长度:
    [java] view plain copy
     print?
    1. Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    2. final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA""BC");  
    3. keyGen.initialize(128);  

    如此这般得到的密文长度为 128 位(16 个字节)。但是这么干之前请先回顾一下本文第 2 点所述。


    8. Cipher 是有状态的,而且是线程不安全的

    javax.crypto.Cipher 是有状态的,不要把 Cipher 当做一个静态变量,除非你的程序是单线程的,也就是说你能够保证同一时刻只有一个线程在调用 Cipher。否则你可能会像笔者似的遇到 Java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block 异常。遇见这个异常,你需要先确定你给 Cipher 加密的明文(或者需要解密的密文)是否过长;排除掉明文(或者密文)过长的情况,你需要考虑是不是你的 Cipher 线程不安全了。


    后记

    虽然《RSA Encryption Example》存在一些认识上的误区,但笔者仍然认为它是一篇很不错的入门级文章。结合本文所列内容,笔者将其代码做了一些调整以供参考:
    [java] view plain copy
     print?
    1. import java.io.File;  
    2. import java.io.FileInputStream;  
    3. import java.io.FileNotFoundException;  
    4. import java.io.FileOutputStream;  
    5. import java.io.IOException;  
    6. import java.io.ObjectInputStream;  
    7. import java.io.ObjectOutputStream;  
    8. import java.security.KeyPair;  
    9. import java.security.KeyPairGenerator;  
    10. import java.security.NoSuchAlgorithmException;  
    11. import java.security.PrivateKey;  
    12. import java.security.PublicKey;  
    13. import java.security.Security;  
    14.   
    15. import javax.crypto.Cipher;  
    16.   
    17. import org.apache.commons.codec.binary.Base64;  
    18.   
    19. /** 
    20.  * @author JavaDigest 
    21.  *  
    22.  */  
    23. public class EncryptionUtil {  
    24.   
    25.     /** 
    26.      * String to hold name of the encryption algorithm. 
    27.      */  
    28.     public static final String ALGORITHM = "RSA";  
    29.   
    30.     /** 
    31.      * String to hold name of the encryption padding. 
    32.      */  
    33.     public static final String PADDING = "RSA/NONE/NoPadding";  
    34.   
    35.     /** 
    36.      * String to hold name of the security provider. 
    37.      */  
    38.     public static final String PROVIDER = "BC";  
    39.   
    40.     /** 
    41.      * String to hold the name of the private key file. 
    42.      */  
    43.     public static final String PRIVATE_KEY_FILE = "e:/defonds/work/20150116/private.key";  
    44.   
    45.     /** 
    46.      * String to hold name of the public key file. 
    47.      */  
    48.     public static final String PUBLIC_KEY_FILE = "e:/defonds/work/20150116/public.key";  
    49.   
    50.     /** 
    51.      * Generate key which contains a pair of private and public key using 1024 
    52.      * bytes. Store the set of keys in Prvate.key and Public.key files. 
    53.      *  
    54.      * @throws NoSuchAlgorithmException 
    55.      * @throws IOException 
    56.      * @throws FileNotFoundException 
    57.      */  
    58.     public static void generateKey() {  
    59.         try {  
    60.   
    61.             Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    62.             final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(  
    63.                     ALGORITHM, PROVIDER);  
    64.             keyGen.initialize(256);  
    65.             final KeyPair key = keyGen.generateKeyPair();  
    66.   
    67.             File privateKeyFile = new File(PRIVATE_KEY_FILE);  
    68.             File publicKeyFile = new File(PUBLIC_KEY_FILE);  
    69.   
    70.             // Create files to store public and private key  
    71.             if (privateKeyFile.getParentFile() != null) {  
    72.                 privateKeyFile.getParentFile().mkdirs();  
    73.             }  
    74.             privateKeyFile.createNewFile();  
    75.   
    76.             if (publicKeyFile.getParentFile() != null) {  
    77.                 publicKeyFile.getParentFile().mkdirs();  
    78.             }  
    79.             publicKeyFile.createNewFile();  
    80.   
    81.             // Saving the Public key in a file  
    82.             ObjectOutputStream publicKeyOS = new ObjectOutputStream(  
    83.                     new FileOutputStream(publicKeyFile));  
    84.             publicKeyOS.writeObject(key.getPublic());  
    85.             publicKeyOS.close();  
    86.   
    87.             // Saving the Private key in a file  
    88.             ObjectOutputStream privateKeyOS = new ObjectOutputStream(  
    89.                     new FileOutputStream(privateKeyFile));  
    90.             privateKeyOS.writeObject(key.getPrivate());  
    91.             privateKeyOS.close();  
    92.         } catch (Exception e) {  
    93.             e.printStackTrace();  
    94.         }  
    95.   
    96.     }  
    97.   
    98.     /** 
    99.      * The method checks if the pair of public and private key has been 
    100.      * generated. 
    101.      *  
    102.      * @return flag indicating if the pair of keys were generated. 
    103.      */  
    104.     public static boolean areKeysPresent() {  
    105.   
    106.         File privateKey = new File(PRIVATE_KEY_FILE);  
    107.         File publicKey = new File(PUBLIC_KEY_FILE);  
    108.   
    109.         if (privateKey.exists() && publicKey.exists()) {  
    110.             return true;  
    111.         }  
    112.         return false;  
    113.     }  
    114.   
    115.     /** 
    116.      * Encrypt the plain text using public key. 
    117.      *  
    118.      * @param text 
    119.      *            : original plain text 
    120.      * @param key 
    121.      *            :The public key 
    122.      * @return Encrypted text 
    123.      * @throws java.lang.Exception 
    124.      */  
    125.     public static byte[] encrypt(String text, PublicKey key) {  
    126.         byte[] cipherText = null;  
    127.         try {  
    128.             // get an RSA cipher object and print the provider  
    129.             Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    130.             final Cipher cipher = Cipher.getInstance(PADDING, PROVIDER);  
    131.               
    132.             // encrypt the plain text using the public key  
    133.             cipher.init(Cipher.ENCRYPT_MODE, key);  
    134.             cipherText = cipher.doFinal(text.getBytes());  
    135.         } catch (Exception e) {  
    136.             e.printStackTrace();  
    137.         }  
    138.         return cipherText;  
    139.     }  
    140.   
    141.     /** 
    142.      * Decrypt text using private key. 
    143.      *  
    144.      * @param text 
    145.      *            :encrypted text 
    146.      * @param key 
    147.      *            :The private key 
    148.      * @return plain text 
    149.      * @throws java.lang.Exception 
    150.      */  
    151.     public static String decrypt(byte[] text, PrivateKey key) {  
    152.         byte[] dectyptedText = null;  
    153.         try {  
    154.             // get an RSA cipher object and print the provider  
    155.             Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    156.             final Cipher cipher = Cipher.getInstance(PADDING, PROVIDER);  
    157.   
    158.             // decrypt the text using the private key  
    159.             cipher.init(Cipher.DECRYPT_MODE, key);  
    160.             dectyptedText = cipher.doFinal(text);  
    161.   
    162.         } catch (Exception ex) {  
    163.             ex.printStackTrace();  
    164.         }  
    165.   
    166.         return new String(dectyptedText);  
    167.     }  
    168.   
    169.     /** 
    170.      * Test the EncryptionUtil 
    171.      */  
    172.     public static void main(String[] args) {  
    173.   
    174.         try {  
    175.   
    176.             // Check if the pair of keys are present else generate those.  
    177.             if (!areKeysPresent()) {  
    178.                 // Method generates a pair of keys using the RSA algorithm and  
    179.                 // stores it  
    180.                 // in their respective files  
    181.                 generateKey();  
    182.             }  
    183.   
    184.             final String originalText = "12345678901234567890123456789012";  
    185.             ObjectInputStream inputStream = null;  
    186.   
    187.             // Encrypt the string using the public key  
    188.             inputStream = new ObjectInputStream(new FileInputStream(  
    189.                     PUBLIC_KEY_FILE));  
    190.             final PublicKey publicKey = (PublicKey) inputStream.readObject();  
    191.             final byte[] cipherText = encrypt(originalText, publicKey);  
    192.   
    193.             // use String to hold cipher binary data  
    194.             Base64 base64 = new Base64();  
    195.             String cipherTextBase64 = base64.encodeToString(cipherText);  
    196.   
    197.             // get cipher binary data back from String  
    198.             byte[] cipherTextArray = base64.decode(cipherTextBase64);  
    199.   
    200.             // Decrypt the cipher text using the private key.  
    201.             inputStream = new ObjectInputStream(new FileInputStream(  
    202.                     PRIVATE_KEY_FILE));  
    203.             final PrivateKey privateKey = (PrivateKey) inputStream.readObject();  
    204.             final String plainText = decrypt(cipherTextArray, privateKey);  
    205.   
    206.             // Printing the Original, Encrypted and Decrypted Text  
    207.             System.out.println("Original=" + originalText);  
    208.             System.out.println("Encrypted=" + cipherTextBase64);  
    209.             System.out.println("Decrypted=" + plainText);  
    210.   
    211.         } catch (Exception e) {  
    212.             e.printStackTrace();  
    213.         }  
    214.     }  
    215. }  


    先生成一对密钥,供以后加解密使用(不需要每次加解密都生成一个密钥),密钥长度为 256 位,也就是说生成密文长度都是 32 字节的,支持加密最大长度为 32 字节的明文,因为使用了 nopadding 所以对于同一密钥同一明文,本文总是生成一样的密文;然后使用生成的公钥对你提供的明文信息进行加密,生成 32 字节二进制明文,然后使用 Base64 将二进制密文转换为字符串保存;之后演示了如何把 Base64 字符串转换回二进制密文;最后把二进制密文转换成加密前的明文。以上程序输出如下:
    Original=12345678901234567890123456789012
    Encrypted=GTyX3nLO9vseMJ+RB/dNrZp9XEHCzFkHpgtaZKa8aCc=
    Decrypted=12345678901234567890123456789012

    参考资料

    原文地址:https://blog.csdn.net/defonds/article/details/42775183#
  • 相关阅读:
    std thread
    windows更新包发布地址
    How to set up logging level for Spark application in IntelliJ IDEA?
    spark 错误 How to set heap size in spark within the Eclipse environment?
    hadoop 常用命令
    windows 安装hadoop 3.2.1
    windows JAVA_HOME 路径有空格,执行软连接
    day01MyBatisPlus条件构造器(04)
    day01MyBatisPlus的CRUD 接口(03)
    day01MyBatisPlus入门(02)
  • 原文地址:https://www.cnblogs.com/jpfss/p/10207044.html
Copyright © 2011-2022 走看看