why do i get this syntax error at the line shown below

package org.temp2.cod1;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;

public class Code1 {


    byte[] plaintext = new byte[32];   // <<<<<<<<<<<<<<<<<<<<<<<<<< syntax error
    for (int i = 0; i < 32; i++) {
      plaintext[i] = (byte) (i % 16);
    }



    byte[] key = new byte[16];
    SecureRandom r = new SecureRandom();
    r.nextBytes(key);


//byte[] key = ;//... secret sequence of bytes
    //byte[] dataToSend =  ; //...

    Cipher c = Cipher.getInstance("AES");
    SecretKeySpec k =  new SecretKeySpec(key, "AES");
    c.init(Cipher.ENCRYPT_MODE, k);
    byte[] encryptedData = c.doFinal(plaintext);
}
}

Dani AI

Generated

The parser is complaining because you have executable statements (the for-loop and method calls) directly in the class body. In Java, the class body may contain field declarations, method/constructor declarations, and initializer blocks only. Put your loop and crypto setup inside a constructor, an instance initializer block, or a static initializer (for static fields). Also ensure your entry point uses the correct signature: public static void main(String[] args).

Example patterns you can use:

  • Instance initializer for per-instance setup:

    public class Code1 {
    private final byte[] plaintext = new byte[32];
    
    { // runs after fields are created, before the constructor finishes
      for (int i = 0; i < plaintext.length; i++) {
        plaintext[i] = (byte) (i & 0x0F);
      }
    }
    }
  • Static initializer for static fields:

    static final byte[] SBOX = new byte[256];
    static { /* fill SBOX here */ }
  • Or move all logic into main/a constructor and call it from main.

Crypto tip: prefer generating an AES key with KeyGenerator rather than filling a byte array yourself, and handle checked exceptions from Cipher.getInstance(...) and init(...).

Helpful references:

Recommended Answers

All 4 Replies

The reason you get a syntax error in that code is that you have an extra bracket.

Try the following code:

1.
      package org.temp2.cod1;
   2.
      import java.security.*;
   3.
      import javax.crypto.*;
   4.
      import javax.crypto.spec.*;
   5.
      import java.io.*;
   6.
       
   7.
      public class Code1 {
   8.
       
   9.
       
  10.
      byte[] plaintext = new byte[32]; // <<<<<<<<<<<<<<<<<<<<<<<<<< syntax error
  11.
      for (int i = 0; i < 32; i++) {
  12.
      plaintext[i] = (byte) (i % 16);
  13.
      }
  14.
       
  15.
       
  16.
       
  17.
      byte[] key = new byte[16];
  18.
      SecureRandom r = new SecureRandom();
  19.
      r.nextBytes(key);
  20.
       
  21.
       
  22.
      //byte[] key = ;//... secret sequence of bytes
  23.
      //byte[] dataToSend = ; //...
  24.
       
  25.
      Cipher c = Cipher.getInstance("AES");
  26.
      SecretKeySpec k = new SecretKeySpec(key, "AES");
  27.
      c.init(Cipher.ENCRYPT_MODE, k);
  28.
      byte[] encryptedData = c.doFinal(plaintext);
  29.
      }

Nope. The real problem is that that code needs to be inside a method. You can't simply code loops and other actions as part of the class definition, but rather as method/constructor/block definitions inside the class.

Java class construct:

public class Foo {
     private String datum;
     private int datum2;

     private void helperMethod() { ... }

     public String exposedMethod(int param)  {  helperMethod(); }

     public void main(String[] args) { //Special method invoked when java Foo is run; }
}

You need to study the Java syntax a little more carefully, I think. :)

I guess I do, since masijade's and Cronless' answers were the right ones.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.