#Need specific cipher transformation that isn't available for some reason.

1 messages ยท Page 1 of 1 (latest)

strange crane
#

I need to use Cipher class (https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/javax/crypto/Cipher.html) with exact transformation "AES/CBC/PKCS5Padding". But I get an error NoSuchAlgorithmException.

I found a function online that shows all available transformations and apparently I don't have it (https://imgur.com/zDPmdPw).

I thought these are tied to java version, so I tried java 21, java 11 java 8, java 7, different versions of Eclipse Adoptium, but no luck.

Please help with enabling "AES/CBC/PKCS5Padding"

still arrowBOT
#

<@&987246399047479336> please have a look, thanks.

still arrowBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

rmation "AES/CBC/PKCS5Padding".

strange crane
agile kernel
#

i need to see

  • SecretKey
  • IvParameterSpec
  • Algorithm Name
#

how you declare those

strange crane
#
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.Charset;

public class ByteDecrypter {
   public static String DecryptFromByte(byte[] Src, String Key, String Mode) {

      Cipher cipher = Cipher.getInstance(Mode);
      byte[] key = Key.getBytes(Charset.forName("ASCII"));
      SecretKeySpec keySpec = new SecretKeySpec(key, "AES");

      cipher.init(Cipher.DECRYPT_MODE, keySpec);
      byte[] encrypted1 = Src;
      byte[] original = cipher.doFinal(encrypted1);
      String originalString = new String(original);
      return originalString;
   }

   public static void main(String[] args) {
      byte[] fileContent = Files.readAllBytes(Paths.get("file.byte"));
      String output = DecryptFromByte(fileContent, "PASSWORD_HERE", "AES/CBC/PKCS5Padding");
      System.out.println(output);
   }
}
still arrowBOT
strange crane
#

Tried to import everything I could find

#

It the first time i'm working with Java

agile kernel
#

ah i see

#

yeah but that's not all

strange crane
#

I'm getting NoSuchAlgorithmException

#

I'll add try catch and show what it return in an hour

agile kernel
#

I think the problem is: you need to use SecretKey

#

not SecretKeySpec

#
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey key = keyGenerator.generateKey();
#

then you do

cipher.init(Cipher.DECRYPT_MODE, key);
strange crane
agile kernel
#

the object must be of type SecretKey

#

the current one you using is SecretKeySpec

strange crane
#

Ok, I'll try this too

agile kernel
#

i think you must set IV

#

initialization vector

#

you can create one easily like this

byte[] iv = new byte[16];
#

your new method would be like this

public static String DecryptFromByte(byte[] Src, String Key, String Mode, byte[] iv) throws Exception {

      Cipher cipher = Cipher.getInstance(Mode);
      byte[] key = Key.getBytes(Charset.forName("ASCII"));
      SecretKeySpec keySpec = new SecretKeySpec(key, "AES");

      IvParameterSpec ivSpec = new IvParameterSpec(iv); // Initialization Vector

      cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
      byte[] original = cipher.doFinal(Src);
      String originalString = new String(original);
      return originalString;
   }
strange crane
#

added try-catch to my code

public static String DecryptFromByte(byte[] Src, String Key, String Mode) {
      try {
         Cipher cipher = Cipher.getInstance(Mode);
         byte[] key = Key.getBytes(Charset.forName("ASCII"));
         SecretKeySpec keySpec = new SecretKeySpec(key, "AES");

         cipher.init(Cipher.DECRYPT_MODE, keySpec);
         byte[] encrypted1 = Src;
         byte[] original = cipher.doFinal(encrypted1);
         String originalString = new String(original);
         return originalString;
      } catch(Exception e) {
         System.out.println(e);
      }

      return "forced to add return";
   }

   public static void main(String[] args) {
      try {
         byte[] fileContent = Files.readAllBytes(Paths.get("./file.byte"));
         String output = DecryptFromByte(fileContent, "PASSWORD_HERE", "AES/CBC/PKCS5Padding");
         System.out.println(output);
      } catch(Exception e) {
         System.out.println(e);
      }
   }

the result is Missing Parameter https://imgur.com/bth8usb
before that it was throwing IOExceptions on Paths.get("./file.byte") so I silenced it too xd

#

I went back to the version of code without try-catch
and tried to change SecretKeySpec to SecretKey
now it shows error

#

and if I change back SecretKey to SecretKeySpec, leaving iv, I get this:

agile kernel
#

you can just add throws Exception

#

to your method declaration

agile kernel
#

im not sure, what's this

strange crane
#

no I just found on some website that java can be compiled with javac and then run with java

#

via command line

#

i am sure that this function should work on a very old version of java

agile kernel
#

where are you exploring your java files?

strange crane
#

in VSCode

agile kernel
#

are you using the java extension ?

strange crane
#

no, but highlighting works

agile kernel
#

it's not enough, it seems you have compiler errors

#

you need to see them up

strange crane
#

let's stop for now
I'll bash my head against this decryption for a bit and later tell if i manage to make it work
i am sure this function worked "as is" at some time

still arrowBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you ๐Ÿ‘