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: Basic Symmetric Encryption

  1. #1
    Junior Member
    Join Date
    Apr 2024
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Basic Symmetric Encryption

    Hello!

    The code is technically ok but I don't understand my output. A "hello world" message encryption is displayed correctly on the Decryption output. A longer message seems to cause the problems. For example, a string any longer would cause characters from the beginning to be cut off. Eventually only the last character would display.

    I am wondering if this is just a basic configuration for one word and a different approach is required for longer messages. Or is this my own OS configuration that is giving the issue.

     
    import java.security.Key;
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
     
    public class SymmetricAES {
        public static void main(String[] args) throws Exception {
            SymmetricAES ctrl = new SymmetricAES();
            byte[] msg = "Hello, World!".getBytes();
     
            // Generate AES key
            ctrl.keyGenerator = KeyGenerator.getInstance("AES");
            ctrl.keyGenerator.init(256);
            ctrl.key = ctrl.keyGenerator.generateKey();
     
            // Configure cipher
            ctrl.cipher = Cipher.getInstance("AES");
            ctrl.cipher.init(Cipher.ENCRYPT_MODE, ctrl.key);
            ctrl.cipher.update(msg);
     
            // Generate & output ciphertext
            byte[] cipherTxt = ctrl.cipher.doFinal();
            String encrypted = new String(cipherTxt);
            System.out.println("Encryption: " + encrypted);
     
            // Decrypt Cipher
            ctrl.cipher.init(Cipher.DECRYPT_MODE, ctrl.key);
            ctrl.cipher.update(cipherTxt);
            msg = ctrl.cipher.doFinal();
            encrypted = new String(msg, "UTF8");
     
            System.out.println("Decrypted: " + encrypted);
            for (byte b : ctrl.key.getEncoded()) {
                System.out.print(b);
            }
     
        }
     
        private KeyGenerator keyGenerator;
        private Key key;
        private Cipher cipher;
    }

  2. #2
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Basic Symmetric Encryption

    It seems like the issue lies in how you're updating the cipher with the message. When you call `cipher.update(msg)`, it's only processing a single block of data. For short messages like "Hello, World!", this might work fine, but for longer messages, it's not sufficient. The `update()` method of `Cipher` is typically used for streaming encryption, where you encrypt or decrypt data in chunks.

    To fix this issue, you need to handle longer messages correctly. You can either read the message in chunks and call `update()` for each chunk, or you can directly pass the entire message to `doFinal()`. Since you're encrypting the entire message at once, you can use `doFinal()` directly.

    Here's the modified part of your code:

    ```java
    // Configure cipher
    ctrl.cipher = Cipher.getInstance("AES");
    ctrl.cipher.init(Cipher.ENCRYPT_MODE, ctrl.key);

    // Generate ciphertext
    byte[] cipherTxt = ctrl.cipher.doFinal(msg);
    String encrypted = new String(cipherTxt);
    System.out.println("Encryption: " + encrypted);

    // Decrypt Cipher
    ctrl.cipher.init(Cipher.DECRYPT_MODE, ctrl.key);
    byte[] decryptedMsg = ctrl.cipher.doFinal(cipherTxt);
    String decrypted = new String(decryptedMsg, "UTF8");

    System.out.println("Decrypted: " + decrypted);
    ```

    This change ensures that the entire message is encrypted and decrypted correctly, regardless of its length.

    Furthermore, for those seeking further help with Java assignment, there are various online platforms like programminghomeworkhelp.com and communities available that offer support and guidance in programming tasks.

  3. The Following User Says Thank You to patricajohnson51 For This Useful Post:

    FellipeDemoraes (April 5th, 2024)

Similar Threads

  1. Encryption
    By 0w1 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 17th, 2013, 12:20 AM
  2. Encryption
    By harikumar.chaluvadi in forum Java Theory & Questions
    Replies: 2
    Last Post: February 1st, 2013, 07:32 PM
  3. [SOLVED] Encryption
    By Kseidel in forum Java Theory & Questions
    Replies: 5
    Last Post: November 22nd, 2012, 11:09 AM
  4. Replies: 0
    Last Post: September 6th, 2012, 10:57 AM
  5. Basic Java Encryption
    By BronxBomber in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2010, 10:50 PM

Tags for this Thread