0

I have already encrypted a file (plaintext.txt). Then I need to re-encrypted this file again. However, I'm not sure whether this is because the encrypted file is unreadable or not. So I want to convert it to binary file or some other readable file so that I can re-encrypt again.

Firstly, I have used this code(the public key and private have been generated) to encrypt a .txt file named encrypt.txt.

To try to see whether it can encrypt an encrypted file or not, now I wanna use the same code to re-encrypt "encrypt.txt" file, to generat a re-encrypted file "encrypt2.txt".

However, there is no information in "encrypt2.txt" file, also this file size is 0kb.

Therefore, what I'm asking is that whether this can be used to re-encrypt again or not?

If yes, how can it happen that no information exits in the "encrypt2.txt". Otherwise, how can I do this re-encryption?

Thank you for any illumination!

Following is my code.

 public static void main(String[] args) throws Exception {

Security.addProvider(new FlexiCoreProvider());

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "FlexiCore");
Cipher cipher = Cipher.getInstance("RSA", "FlexiCore");

kpg.initialize(1024);
KeyPair keyPair = kpg.generateKeyPair();
PrivateKey privKey = keyPair.getPrivate();
PublicKey pubKey = keyPair.getPublic();

// Encrypt

cipher.init(Cipher.ENCRYPT_MODE, pubKey);

String cleartextFile = "cleartext.txt";
String ciphertextFile = "ciphertextRSA.txt";

FileInputStream fis = new FileInputStream(cleartextFile);
FileOutputStream fos = new FileOutputStream(ciphertextFile);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);

byte[] block = new byte[32];
int i;
while ((i = fis.read(block)) != -1) {
    cos.write(block, 0, i);
}
cos.close();

// Decrypt

String cleartextAgainFile = "cleartextAgainRSA.txt";

cipher.init(Cipher.DECRYPT_MODE, privKey);

fis = new FileInputStream(ciphertextFile);
CipherInputStream cis = new CipherInputStream(fis, cipher);
fos = new FileOutputStream(cleartextAgainFile);

while ((i = cis.read(block)) != -1) {
    fos.write(block, 0, i);
}
fos.close();
}
Clarence
  • 41
  • 6
  • 2
    There's no reason why you can't just re-encrypt the encrypted file. An encrypted file *is* a "binary file" - it's just a sequence of bytes, after all. – Jon Skeet Feb 15 '14 at 09:28
  • That's why I'm confused. I also thought this can be done directly but it's not the case. Could you please give me more hint or provide me an example of re-encryption? Thanks a lot! – Clarence Feb 15 '14 at 11:46
  • 1
    "it's not the case" doesn't tell us what you've tried or what went wrong. We have no idea how you're trying to do this, so we can't comment on what the mistake is. Please edit your question to show your code. – Jon Skeet Feb 15 '14 at 11:50
  • Above is my code. I'm sorry not to arrange it properly. Hope you can understand it. Actually even I just wanna encrypt a ciphertext it can't work well. However if I use the same way to encrypt a plaintext it is OK. – Clarence Feb 15 '14 at 12:49
  • 1
    Please edit the code into your question - that's where it belongs, not in a comment. And say what goes wrong - just saying "it doesn't work well" isn't enough information. – Jon Skeet Feb 15 '14 at 13:03
  • Sorry for making it so hard to understand. This is the first time I post question here. Sorry again. – Clarence Feb 15 '14 at 16:34
  • 1
    Are you actually *closing* any of these streams? If not, it's probably all just buffered... You should be closing `cos`. – Jon Skeet Feb 15 '14 at 18:33
  • Yes. Of cause I have closed it(I didn't copy it here). That's why I'm confused. Because if I use a plain text it can output a cipher text. But when I'm using the encrypted cipher text there is no re-encrypted cipher text showed. – Clarence Feb 16 '14 at 03:07
  • 1
    There's no "of course" about it - that would have explained things, potentially, and plenty of people forget to close streams. You're closing `cos` *before* you close `fos`? If you change your debug output to print `i` rather than `block`, how many bytes do you get to see being read? (Oh, and it seems pointless to create a buffer with the size of the *filename* in characters. I'd just create a buffer of size 8K or something like that. I'm sure that it's the problem, but it's odd...) – Jon Skeet Feb 16 '14 at 07:38
  • 17 17 17 17 17 17 17 9 -1 These are the outputs in console. Actually I just inputted "cleartext" in the original .txt text. I'm still confusing now I don't think this is something wrong with my close of cos and fos. Because I can output the encrypted file firstly. Later I just change the path of two String – Clarence Feb 16 '14 at 14:39
  • 1
    Right, so it *is* writing data to the stream, and I strongly suspect the problem is in some of the code that you haven't shown us. It would *really* help if you'd show us a short but complete program demonstrating the problem, partly so we could try to reproduce it ourselves. – Jon Skeet Feb 16 '14 at 14:47
  • I have attached the original encryption code and it works well. All I have done is just change String cleartextFile = "cleartext.txt"; String ciphertextFile = "ciphertextRSA.txt"; to -re-encrypt the encrypted file "ciphertextRSA.txt" (Anyway, there is no difference if I just changed the text name) – Clarence Feb 16 '14 at 15:06
  • 1
    Well now you're showing *decrypting* rather than encrypting after the initial encryption. I thought the problem was encrypting twice. It's very hard to keep track of a problem which changes like that. I suggest you start from scratch, describing precisely one issue and showing *that code*. Don't talk about "this is the same code but with a bit more changed" - post a short but complete program which we can just run to show the issue. – Jon Skeet Feb 16 '14 at 15:08
  • I also want you to run it however this is a code I downloaded from internet. So you need to import some packages. Here is the website:http://www.flexiprovider.de/examples/ExampleRSA.html – Clarence Feb 16 '14 at 15:14
  • Also I'm not sure whether you know my question. I just want to do the re-encryption. So I try to use the same code to encrypt an encrypted file. If you have other viable ways to help me do the re-encryption hope you to share your opinion. – Clarence Feb 16 '14 at 15:17
  • 1
    All I'm looking for is a way of reproducing the problem. You haven't provided that yet. One program, which doesn't need any changes to anything, that I can compile, and run, and see the problem you're seeing. – Jon Skeet Feb 16 '14 at 15:19

0 Answers0