Java 令牌上的语法错误

Java 令牌上的语法错误,java,Java,好的,我让它工作了,但它不会用随机键初始化我的keyValue 这是我的AESencrp.java package encript; import java.math.BigInteger; import java.security.Key; import java.security.NoSuchAlgorithmException; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import ja

好的,我让它工作了,但它不会用随机键初始化我的keyValue

这是我的AESencrp.java

        package encript;

import java.math.BigInteger;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESencrp {

    KeyGenerator gen;
    private static final byte[] keyValue = new byte[16] ;

    public AESencrp() throws NoSuchAlgorithmException {
        this.gen = KeyGenerator.getInstance("AES");
        gen.init(128); /* 128-bit AES */

        SecretKey secret = gen.generateKey();
        byte[] keyValue = secret.getEncoded();
        String text = String.format("%032X", new BigInteger(+1, keyValue));
        System.out.println(text);
    }

    private static final String ALGO = "AES";
    /*private static final byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B',
            'e', 's', 't', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };*/

    private static String toHex(final byte[] data) {
        final StringBuilder sb = new StringBuilder(data.length * 2);
        for (final byte b : data) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    public static String encrypt(String Data) throws Exception {
        Key key = generateKey();

        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

    public static String decrypt(String encryptedData) throws Exception {
        Key key = generateKey();

        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
    }

}
package encript;




public class Checker {

    public static void main(String[] args) throws Exception {

        String password = "mypassword";
        String passwordEnc = AESencrp.encrypt(password);
        String passwordDec = AESencrp.decrypt(passwordEnc);

        System.out.println("Plain Text : " + password);
        System.out.println("Encrypted Text : " + passwordEnc);
        System.out.println("Decrypted Text : " + passwordDec);


        }
    }
这是Checker.java

        package encript;

import java.math.BigInteger;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESencrp {

    KeyGenerator gen;
    private static final byte[] keyValue = new byte[16] ;

    public AESencrp() throws NoSuchAlgorithmException {
        this.gen = KeyGenerator.getInstance("AES");
        gen.init(128); /* 128-bit AES */

        SecretKey secret = gen.generateKey();
        byte[] keyValue = secret.getEncoded();
        String text = String.format("%032X", new BigInteger(+1, keyValue));
        System.out.println(text);
    }

    private static final String ALGO = "AES";
    /*private static final byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B',
            'e', 's', 't', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };*/

    private static String toHex(final byte[] data) {
        final StringBuilder sb = new StringBuilder(data.length * 2);
        for (final byte b : data) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    public static String encrypt(String Data) throws Exception {
        Key key = generateKey();

        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

    public static String decrypt(String encryptedData) throws Exception {
        Key key = generateKey();

        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
    }

}
package encript;




public class Checker {

    public static void main(String[] args) throws Exception {

        String password = "mypassword";
        String passwordEnc = AESencrp.encrypt(password);
        String passwordDec = AESencrp.decrypt(passwordEnc);

        System.out.println("Plain Text : " + password);
        System.out.println("Encrypted Text : " + passwordEnc);
        System.out.println("Decrypted Text : " + passwordDec);


        }
    }
如果我把

    KeyGenerator gen = KeyGenerator.getInstance("AES");
    gen.init(128); /* 128-bit AES */
    SecretKey secret = gen.generateKey();
    byte[] binary = secret.getEncoded();
    String text = String.format("%032X", new BigInteger(+1, binary));
    System.out.println(text);

在我的主类中,代码可以工作,但我希望它在另一个类中。

AESencrp
中使用构造函数:

KeyGenerator gen;

public AESencrp() throws NoSuchAlgorithmException {
    this.gen = KeyGenerator.getInstance("AES");
    gen.init(128); /* 128-bit AES */
}
这些进口

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

这是个坏主意。
sun.
软件包不适合直接使用。我推荐Commons Codec中的类。请参见将代码放入另一个类的构造函数中

可以在方法定义之外的类中编写的唯一代码是

static {
    System.out.println("in static block.");
}
但从静态代码中,您无法访问实例属性。因此,构造函数是进行这些初始化的自然场所


如果,OTOH,在AESencrp中您真的只需要静态文件,那么您需要将其声明为静态,并且初始化代码必须位于静态块中。

我不能在主类之外声明gen。您的意思是什么?为什么不呢<代码>发电机用于
AESecnrp
。在那里初始化它。仍然不起作用,也不能导入commons codex类。1。使用构造函数。这对我有用。2.只有在那之后,使用Commons编解码器。我使用构造函数,它不会给出任何错误,但也不会打印。键发生器;public AESencrp()抛出NoSuchAlgorithmException{this.gen=KeyGenerator.getInstance(“AES”);gen.init(128);/*128位AES*/SecretKey secret=gen.gen.generateKey();byte[]binary=secret.getEncoded();String text=String.format(“%032X”,新的BigInteger(+1,binary));System.out.println(text);}但是我希望这个类生成密钥并使用它,而不是我的主类。呃,我太困惑了。。如果我把它放在我的main中,我不能在这一个中使用keyValue,因为它没有定义。现在你有了两次
keyValue
:一个静态字段和一个局部变量在构造函数中。