简体   繁体   English

使用BouncyCastle API进行RSA加密

[英]RSA encrypt with BouncyCastle API

I have a problem when I want to encrypt a file using RSA with BouncyCastle API in Java. 我想在Java中使用带有BouncyCastle API的RSA使用RSA加密文件时遇到问题。 The problem is the following: in the code below, the line in which I create an object of RSAKeyParameters,its constructor asks me three parameters: 问题如下:在下面的代码中,我在其中创建RSAKeyParameters对象的行,其构造函数询问三个参数:
1. If We want to cipher with public or private key. 1.如果我们想使用公钥或私钥进行加密。
2. A BigInteger with the modulus of the key. 2.一个BigInteger及其密钥的模数。
3. A BigInteger with the exponent of the key. 3.带有密钥指数的BigInteger。

The first parameter that my method receives is the file where the key is contained. 我的方法收到的第一个参数是包含密钥的文件。 So in the RSAKeyParameter's constructor, how can I pass it a BigInteger that is the modulus and exponent?, how can I get the modulus and the exponent from the file? 因此,在RSAKeyParameter的构造函数中,如何向其传递作为模数和指数的BigInteger?如何从文件中获取模数和指数?

PD: The file that contains the key has a CR and LF, that's why there are two readLine(). PD:包含密钥的文件具有CR和LF,这就是为什么有两个readLine()的原因。

void cifrar_asimetrica(String fichClave, String archivoClaro, String result, boolean conPrivada){

    byte[] modulo;
    byte[] exponente;

    try(
        BufferedReader lectorClave = new BufferedReader (new FileReader(fichClave));
        BufferedInputStream lectorFichero = new BufferedInputStream(new FileInputStream(archivoClaro));
        BufferedOutputStream fsalida = new BufferedOutputStream(new FileOutputStream(result))){

        modulo = Hex.decode(lectorClave.readLine()); 
        exponente = Hex.decode(lectorClave.readLine());


        RSAEngine cifrador = new RSAEngine();
        CipherParameters parametro = new RSAKeyParameters(conPrivada, new BigInteger(modulo.toString()), new BigInteger(exponente.toString()));

        cifrador.init(true,parametro); // vamos a cifrar

        byte[] datosLeidos = new byte[cifrador.getOutputBlockSize()];
        byte[] datosCifrados = new byte[cifrador.getOutputBlockSize()];
        int leidos = 0;
        //NO SE SI ES GETINPUTBLOCKSIZE O OUTPUT
        leidos = lectorFichero.read(datosLeidos, 0, cifrador.getOutputBlockSize());

        while(leidos > 0){
            datosCifrados = cifrador.processBlock(datosLeidos, 0, cifrador.getOutputBlockSize());
            fsalida.write(datosCifrados, 0, datosCifrados.length);
            leidos = lectorFichero.read(datosLeidos, 0, cifrador.getOutputBlockSize());
        }

    }catch(Exception e){
        e.printStackTrace();
    }
}

If your byte arrays, converted from hex in the file, are big-endian as is conventional, to convert a positive big-endian byte array to BigInteger look at the javadoc for BigInteger for the constructor that takes an int sign for positive and a big-endian byte array for magnitude. 如果从文件中的十六进制转换而来的字节数组按照常规方式是big-endian,要将正的big-endian字节数组转换为BigInteger,请在Javadoc中查看BigInteger的构造函数,该构造函数的正负号必须为int -endian字节数组的大小。

'Textbook' (unpadded) RSA is insecure; RSA的“教科书”(未填充)不安全; see crypto.SX security.SX and wikipedia. 请参阅crypto.SX安全性。SX和Wikipedia。 Using RSA for data larger than one block the way you've coded it will semi-randomly fail, and if you correct that, what amounts to ECB mode is inefficient and insecure; 将RSA用于大于您所编码方式的一个数据块的数据将半随机失败,并且如果您对此进行纠正,那么ECB模式的效率低下且不安全。 see crypto.SX security.SX and wikipedia. 请参阅crypto.SX安全性。SX和Wikipedia。 Using an unauthenticated public key is usually insecure. 使用未经身份验证的公钥通常是不安全的。

If you are doing this for fun because it makes you feel like a "l33t hack5r" or Bond supervillain, and don't care about actual securty, this is fine. 如果您这样做很有趣,因为它使您感觉像“ l33t hack5r”或Bond超级反派,并且不关心实际的安全性,那就很好。 If you need or want actual security, drop this and use programs written by people who know what they are doing, and/or search 'don't roll your own crypto'. 如果您需要或想要实际的安全性,请放弃此功能,并使用由知道自己在做什么的人编写的程序,和/或搜索“不要使用自己的加密货币”。

You're currently using toString on a byte array. 您当前在字节数组上使用toString This will only return a representative of the object reference, which has little to do with the value within the array. 这只会返回对象引用的代表,该引用与数组中的值无关。

Instead you can use the BigInteger constructor that takes a string and radix , using 16 as radix. 相反,您可以使用BigInteger构造函数,构造函数接受一个字符串和radix ,并将16作为基数。 Do make sure that you don't have any spurious or invalid characters in the hexadecimal representation though. 但是请确保您在十六进制表示形式中没有任何虚假或无效的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM