This is the error
Main.java:1: error: class, interface, or enum expected
Crytpo.java
^
1 error
This is the code
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!