如何修复一些错误编译Java

时间:2019-07-11 12:07:33

标签: java

我只需要您帮助以下Java文件

import java.lang.Math;
import java.util.Base64;
import java.security.InvalidAlgorithmParameterException; 
import java.security.InvalidKeyException; 
import java.security.Key; 
import java.security.NoSuchAlgorithmException; 
import java.security.spec.AlgorithmParameterSpec; 
import javax.crypto.BadPaddingException; 
import javax.crypto.Cipher; 
import javax.crypto.IllegalBlockSizeException; 
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.SecretKeySpec; 

public class HelloWorld
{
  public static void main(String[] args)
  {
    OtherClass myObject = new OtherClass("pYQ4/pRt1g0EEdMuf8j2hg==", "nFgAIwY", "bkZnQUl3WVF1bXl2ZUpUYg==");
    System.out.print(myObject);
  }
}

public class OtherClass
{
  private String message;
  private boolean answer = false;
  public String OtherClass(str,str2,str3)
  { 
    AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(Base64.getDecoder().decode(str3.getBytes())); 
    Key secretKeySpec = new SecretKeySpec(str2.getBytes(), "AES"); 
    Cipher instance = Cipher.getInstance("AES/CFB/NoPadding"); 
    instance.init(2, secretKeySpec, ivParameterSpec); 
    return new String(instance.doFinal(Base64.getDecoder().decode(str.getBytes()))); 
  }
  public String toString()
  {
    return message;
  }
}

我已经尝试在某些站点上在线测试Java代码,并收到以下错误消息:检查类头...解析单个文件失败!

2 个答案:

答案 0 :(得分:1)

OtherClass myObject = new OtherClass("pYQ4/pRt1g0EEdMuf8j2hg==", "nFgAIwY", "bkZnQUl3WVF1bXl2ZUpUYg==");

您调用了构造函数。首先,您需要了解构造函数和方法。

修改

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.SecretKeySpec;

public class SampleCode {
   public static void main(String[] args) throws Exception, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, BadPaddingException, Exception
  {
    SampleCode sampleCode =new SampleCode();
    OtherClass myObject = sampleCode.new OtherClass();
    System.out.print(myObject.OtherClassMethod("pYQ4/pRt1g0EEdMuf8j2hg==", "nFgAIwY", "bkZnQUl3WVF1bXl2ZUpUYg=="));
  }

 public class OtherClass {
    private String message;
    private boolean answer = false;

    public String OtherClassMethod(String str, String str2, String str3) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, Exception,
            BadPaddingException {
        AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(Base64.getDecoder().decode(str3.getBytes()));
        Key secretKeySpec = new SecretKeySpec(str2.getBytes("UTF-16"), "AES");
        Cipher instance = Cipher.getInstance("AES/CFB/NoPadding");
        instance.init(2, secretKeySpec, ivParameterSpec);
        return new String(instance.doFinal(Base64.getDecoder().decode(str.getBytes())));
    }

    @Override
    public String toString() {
        return message;
    }
  }
}

尝试

答案 1 :(得分:0)

请学习Java基础知识以了解构造函数,方法,并使用eclipse或Intellij for Java之类的IDE。

进一步,请尝试以下。它不会给出编译错误:

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;

public class Test
{
    public static void main(String[] args)
    {
        OtherClass myObject = new OtherClass();
        String myObjectStr= myObject.otherClassMethod("pYQ4/pRt1g0EEdMuf8j2hg==", "nFgAIwY", "bkZnQUl3WVF1bXl2ZUpUYg" +
                "==");
        System.out.print(myObjectStr);
    }
}

 class OtherClass
{
    private String message;
    private boolean answer = false;

    public String otherClassMethod(String str,String str2,String str3)
    {
        try {
            AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(Base64.getDecoder().decode(str3.getBytes()));
            Key secretKeySpec = new SecretKeySpec(str2.getBytes(), "AES");
            Cipher instance = Cipher.getInstance("AES/CFB/NoPadding");
            instance.init(2, secretKeySpec, ivParameterSpec);
            return new String(instance.doFinal(Base64.getDecoder().decode(str.getBytes())));
        }catch (Exception e){

        }
        return null;
    }
    public String toString()
    {
        return message;
    }
}