tags:

views:

314

answers:

2

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


//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);
}
}
+6  A: 

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

Bozho
+2  A: 

You forgot the entry point method declaration. Try adding:

public static void main(String[] args) {

before the line where you got the error.

Emil H
eclipse suggested to add the public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {is that ok ?
silverkid
Yes - although you can omit the throws clause if you are not throwing any of those exceptions.
Chris Harcourt