Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with RSA Encryption with ECB Mode

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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:

    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.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Need help with RSA Encryption with ECB Mode

    Quote Originally Posted by mdot View Post
    ...
    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

Similar Threads

  1. Replies: 0
    Last Post: September 6th, 2012, 11:57 AM
  2. [SOLVED] RSA encryption based from a Sample Alice and Bob's key.
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 22nd, 2011, 09:38 AM
  3. Replies: 0
    Last Post: June 19th, 2011, 03:16 AM
  4. RSA Decryption with Java.security - Hex to dec to byte array conversion...
    By SeanSeanston in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 15th, 2010, 09:48 AM
  5. Works on debug mode but not on run mode
    By alfonsoraul in forum Member Introductions
    Replies: 0
    Last Post: April 14th, 2010, 03:58 PM