javaandroidflutterencryptioncamellia

How to encrypt & decrypt using Camellia 128bit in Android JAVA code?


What is the provider name syntax for Camellia 128bit in Android JAVA code?? i try to change from Cipher.getInstance("AES/CBC/PKCS7Padding") into Cipher.getInstance("Camellia/CBC/PKCS7Padding","BC") but it says Provider BC does not provide "Camellia/CBC/PKCS7Padding". Below is my code

try {
      String keyString = "Potatoman55@";//length of key is 16
      Cipher desCipher = Cipher.getInstance("Camellia/CBC/PKCS7Padding","BC");
      byte[] key = new byte[0];
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
              key = keyString.getBytes(StandardCharsets.UTF_8);
          }
      }
      MessageDigest sha = MessageDigest.getInstance("SHA-1");
      key = sha.digest(key);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
          key = Arrays.copyOf(key, 16); // use only first 128 bit
      }

      SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
      desCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

      String plainText = "hello wolrd";
      System.out.println("plaintext: "+plainText);

      byte[] text = plainText.getBytes("UTF-8");
      byte[] textencrypted = desCipher.doFinal(text);
      String textEnc = null;
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          System.out.println("encrypted: " + Base64.getEncoder().encodeToString(textencrypted));
          textEnc = Base64.getEncoder().encodeToString(textencrypted);
      }
      result.success(textEnc);

  } catch (Exception ex) {
      System.out.println(ex.toString());
  }

Solution

  • I successfully run the code by this code new org.bouncycastle.jce.provider.BouncyCastleProvider()); as the provider. Big thanks to this comment. below is the full syntax

    Cipher desCipher = Cipher.getInstance("Camellia/CBC/PKCS7Padding",new org.bouncycastle.jce.provider.BouncyCastleProvider());