RSA使用BigInteger类

时间:2017-03-12 15:56:57

标签: java encryption cryptography rsa biginteger

我正在做一个图像隐写术项目,我正在实施RSA加密,以使隐藏的消息更安全。

我正在使用BigInteger类,并且RSA本身就可以,但是在从封面图像中提取加密文本之后使用它时,它在解密密文时是不一致的。

RSA key = new RSA(1024);
BigInteger[] privateKey = key.getPrivKey();
String s = "Hello World";
        byte[] bytes = s.getBytes();
        BigInteger message = new BigInteger(bytes);

        BigInteger encrypt = key.encrypt(message);

        Stego steg = new Stego();
        steg.encode(ImageIO.read(new File("horse_PNG2538.png")), encrypt.toString(), "stegTest");

String toDecrypt = steg.decode(ImageIO.read(new File("stegTest.png")));
BigInteger messageD = new BigInteger(toDecrypt);

BigInteger decrypt = key.decrypt(messageD, privateKey);
String plainText = new String(decrypt.toByteArray());

Encode在其参数中包含BufferedImage,String(图像中隐藏的文本)和输出图像的名称。

解码获取图像并提取隐藏文本并返回String。

我比较了messageD(从图像中提取的cipherText)和加密的toString(),两者都应该一直相同,但在某些运行中,它们的输出不同,在解密时给出错误的plainText。

我如何才能保持这种一致性?

编辑其他代码:这就是我在Stego课程中将文字嵌入图片的方式;

    public static BufferedImage encode(BufferedImage coverImage, String text, String newFileName) {
    //create userSpace
    BufferedImage userSpace = user_space(coverImage);
    return embedText(userSpace, text, newFileName);
}//END OF ENCODE

public static BufferedImage embedText(BufferedImage image, String text, String name) {
    int bitMask = 0x00000001;   
    int currentChar;               
    int x = 0;              
    int y = 0;

    String toEmbed = "***" + text.length() + "***" + text;
    byte[] msg = toEmbed.getBytes(); 
    System.out.println("" + toEmbed.length());

    for (int i = 0; i < msg.length; i++) {
        currentChar = msg[i];       
        for (int j = 0; j < 8; j++) { //0-7 bits

             int flag = currentChar & bitMask;   // get 1 digit from the current character

            if (x < image.getWidth()) {
                image.setRGB(x, y, image.getRGB(x, y)& 0xFE | flag);    
                x++;
            } else {
                x = 0; 
                y++;
                image.setRGB(x, y, image.getRGB(x, y)&0xFE | flag);    
            }

            currentChar = currentChar >> 1;             // get the next digit from the character
        }
    }


    try {
        File outputfile = new File(name + ".png");
        ImageIO.write(image, "png", outputfile);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Something went wrong with creating the new image");
    }
    return image;
} 

以及我如何提取文字;

public static String decode(BufferedImage image) {
    Object[] details = isEncrypted(image);
    int length = 0;
    String secretMessage = "";
    if ((boolean) details[0] == false) {
        System.out.println("Not Encrypted");
        return "nothing";
    }

    length = (int) details[1];

    char[] temp = extractText(image, length);
    for(int i = 0; i<temp.length; i++){
        secretMessage += (new StringBuilder().append(temp[i])).toString();
    }

    return secretMessage.substring((int)details[2]);
}

public static char[] extractText(BufferedImage image, int length) {

    int bitMask = 0x00000001;   
    int x = 0;                  
    int y = 0;                  
    int flag;
    char[] c = new char[length];    

    for (int i = 0; i < length; i++) {
        int bit = 0;


        for (int j = 0; j < 8; j++) {
            if (x < image.getWidth()) {
                flag = image.getRGB(x, y) & bitMask;   
                x++;
            } else {
                x = 0;
                y++;
                flag = image.getRGB(x, y) & bitMask;    
            }


            if (flag == 1) {
                bit = bit >> 1;
                bit = bit | 0x80;
            } else {
                bit = bit >> 1;
            }
        }
        c[i] = (char) bit;  


    }
    return c;
}

0 个答案:

没有答案