Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the error

Main.java:1: error: class, interface, or enum expected
Crytpo.java
^
1 error


This is the code

Java
Crytpo.java

package encryptions;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

public class Crypto{

Cipher encryptions;
Cipher decryptions;
byte[] salting = {
(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
(byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03
};
int cnt = 19;
public Crypto() {

}

public String encryptions(String sec, String txt)
throws NoSuchAlgorithmException,
InvalidKeySpecException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
UnsupportedEncodingException,
IllegalBlockSizeException,
BadPaddingException{
KeySpec kspc = new PBEKeySpec(sec.toCharArray(), salting, cnt);
SecretKey keys = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(kspc);
AlgorithmParameterSpec param = new PBEParameterSpec(salting, cnt);
encryptions = Cipher.getInstance(keys.getAlgorithm());
encryptions.init(Cipher.ENCRYPT_MODE, keys, param);
String chara="UTF-8";   
byte[] ip = txt.getBytes(chara);
byte[] ot = encryptions.doFinal(ip);
String encr=new sun.misc.BASE64Encoder().encode(ot);
return encr;
}
public String decryptions(String sec, String encryptedTxt)
throws NoSuchAlgorithmException,
InvalidKeySpecException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
UnsupportedEncodingException,
IllegalBlockSizeException,
BadPaddingException,
IOException{
KeySpec kspc = new PBEKeySpec(sec.toCharArray(), salting, cnt);
SecretKey keys = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(kspc);
AlgorithmParameterSpec param = new PBEParameterSpec(salting, cnt);
decryptions=Cipher.getInstance(keys.getAlgorithm());
decryptions.init(Cipher.DECRYPT_MODE, keys,param);
byte[] enc = new sun.misc.BASE64Decoder().decodeBuffer(encryptedTxt);
byte[] lang = decryptions.doFinal(enc);
String chara="UTF-8";   
String plains = new String(lang, chara);
return plains;
}
public static void main(String[] args) throws Exception {
Crypto Crypto=new Crypto();
String keys="akshay";   
String plainsss="What is this encryption?";
String enc=Crypto.encryptions(keys, plainsss);
System.out.println("Original text: "+plainsss);
System.out.println("Encrypted text: "+enc);
String afterDec=Crypto.decryptions(keys, enc);
System.out.println("Original text after decryption: "+afterDec);
}
}


What I have tried:

I need a simple explanation as I am quite new to the field. Thank you!
Posted
Updated 8-Dec-22 0:51am
v3
Comments
Graeme_Grant 8-Dec-22 6:31am    
Your code dump is a mess. Please clean it up and follow the posting guidelines: CodeProject Quick Answers FAQ[^]

Comment out the first line of your code.
Change this:
Crytpo.java

package encryptions;
To this:
// Crytpo.java

package encryptions;
 
Share this answer
 
Comments
Member 15753667 8-Dec-22 7:01am    
New error after doing that


Main.java:17: error: class Crypto is public, should be declared in a file named Crypto.java
public class Crypto{
^
Main.java:47: error: cannot find symbol
String encr=new sun.misc.BASE64Encoder().encode(ot);
^
symbol: class BASE64Encoder
location: package sun.misc
Main.java:65: error: cannot find symbol
byte[] enc = new sun.misc.BASE64Decoder().decodeBuffer(encryptedTxt);
^
symbol: class BASE64Decoder
location: package sun.misc
3 errors
OriginalGriff 8-Dec-22 8:09am    
Read the error message!

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
I did a quick Google Search and found a number of answers for you: java error: class, interface, or enum expected - Google Search[^]

This looked like a good answer for you: How to resolve the "class interface or enum expected" error[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900