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);
}
}

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.