silverkid
silverkid

Reputation: 9563

Syntax error on token ";", { expected after this token

why is there syntax error on this 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);

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

Upvotes: 4

Views: 66981

Answers (4)

Theri Muthu Selvam
Theri Muthu Selvam

Reputation: 152

You have to add your code inside the method not to class, hence you are facing this issue. Add your code inside a method, and it will resolve this issue.

Upvotes: 0

sagarp
sagarp

Reputation: 81

you add above code in a method main or other. example: public static void Code1 () { }

Upvotes: 0

Emil H
Emil H

Reputation: 40240

You forgot the entry point method declaration. Try adding:

public static void main(String[] args) {

before the line where you got the error.

Upvotes: 18

Bozho
Bozho

Reputation: 597076

your code should be inside a method. It appears to me that you have skipped the public void method(..) { line

Upvotes: 15

Related Questions