Need help with RSA Encryption with ECB Mode
My algorithm is acting kind of strange...
At first, it was working unless it was thrown a line with a length longer than the bitsize of the key. Now, I'm not sure whats happening.
Here is my encryption of each block in RSA:
Code :
byte[] message = read_bytes(input_file); //get all bytes of the file into a byte array
byte[] cipher; //byte array to hold each block cipher
write_file(output_file, ""); //clear the output file
for(int i = 0; i < message.length; i+=128) { //go through the entire message
byte[] block = Arrays.copyOfRange(message, i, i+128); //get the next 128 bits
cipher = new BigInteger(block).modPow(public_exponent, public_modulus).toByteArray(); //get the cipher, convert to byte array
append_bytes(output_file, cipher); //add the cipher to the end of the file
}
Here is my decryption of each block in RSA:
Code :
byte[] encrypted = read_bytes(input_file); //read all of the encrypted bytes into array
byte[] message; //will be used for decrypted messages
write_file(output_file, ""); //clear the output file
for(int i = 0; i < encrypted.length; i+=128) { //loop through each block
byte[] block = Arrays.copyOfRange(encrypted, i, i+128); //get current block
message = new BigInteger(block).modPow(d, modulus).toByteArray(); //decrypt current block
append_bytes(output_file, decrypted); //write the bytes to the end of the file
}
Anything glaringly wrong here, or could the issue be somewhere else?
Also, for the block size, is that dependent on the key size?
The code works in strange ways. Sometimes messages > 128 bytes long never decrypt; sometimes they do decrypt, so long as the length of the line is not greater than the key size. Something is clearly wrong.
If it helps, I'll post the whole java class, but I don't think the issue lies within there, because encrypting small files seems to work fine.
Re: Need help with RSA Encryption with ECB Mode
Quote:
Originally Posted by
mdot
...
Also, for the block size, is that dependent on the key size?
For starters: The basic powMod encryption/decryption algorithm depends on the block size being smaller than the modulus. That is, in order to guarantee that you can recover the original from the encrypted message, the number of bits in the original message must be less than the number of bits in the modulus. (Note: that is properly less than. It does not mean less than or equal to.)
If this is violated, it may "just happen" that you can get the same stuff back, but I wouldn't count on it.
As far as "strange ways," well, I wouldn't hazard a guess without more information and, perhaps some code and a specific case instance of the file containing the test message.
As far as that goes, what do you mean by "Sometimes messages...never decrypt"? I am having a hard time trying to imagine the strangeness of things that "sometimes never" happen.
I am not giving up yet, but...
"The universe is not only stranger than we imagine,
it's stranger than we can imagine."
---Arthur C Clarke paraphrasing J.B.S. Haldane
Cheers!
Z