I am experimenting with cryptography and I was wondering if somebody has some suggestion how I could improve my code and methodology.
I want my process to generate more securely the cryptography process if that is possible
import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
@SuppressWarnings("ClassWithoutLogger")
public class Crypto_Security {
static public final String CRY_ALGO = "AES";
static public String encrypt(byte[] pSecret, String pValue) throws GeneralSecurityException {
Key aesKey = new SecretKeySpec(pSecret, CRY_ALGO);
Cipher cipher = Cipher.getInstance(CRY_ALGO);
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(pValue.getBytes());
Base64.Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(encrypted);
}
static public String decrypt(byte[] pSecret, String pEncrypted) throws GeneralSecurityException {
SecretKeySpec skeySpec = new SecretKeySpec(pSecret, CRY_ALGO);
Cipher cipher = Cipher.getInstance(CRY_ALGO);
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(Base64.getDecoder().decode(pEncrypted));
return new String(original);
}
static public void main( String[] args ) {
try {
System.out.println("enc " + encrypt( "2144226321110063".getBytes() , "serial.txt" ));
System.out.println("dcr " + decrypt( "2144226321110063".getBytes() , "oLQgEUxg3Z3zMSsSkiKpBg==" ));
} catch( GeneralSecurityException e ) {
e.printStackTrace();
}
return;
}
private Crypto_Security() {
}
}
```