Extension compiling error

Below is my code , i am getting below error

javac:
[mkdir] Created dir: /compiler/android/build/IJlxL/classes
[javac] Compiling 1 source file to /compiler/android/build/IJlxL/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /compiler/android/src/IJlxL/io/aesencryption/com/AESENCRYPTION4.java:3: error: class, interface, or enum expected
[javac] package com.yourname.aesencryption;
[javac] ^
[javac] 1 error
[javac] 1 warning

THIS IS MY CODE

package io.aesencryption.com;

import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.common.ComponentCategory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.InvalidAlgorithmParameterException;

@DesignerComponent(version = 1,
   description = "AES Encryption Extension",
   category = ComponentCategory.EXTENSION,
   nonVisible = true,
   iconName = "images/extension.png")
@SimpleObject(external = true)
public class AesEncryption extends AndroidNonVisibleComponent implements Component {

    private static final String AES = "AES";
    private static final String DEFAULT_PADDING = "PKCS5Padding";
    private static final String DEFAULT_TRANSFORMATION = AES + "/CBC/" + DEFAULT_PADDING;
    
    private String secretKey = "default_key";
    private String ivKey = "default_iv";
    private String paddingType = DEFAULT_PADDING;
    private String encryptionMethod = DEFAULT_TRANSFORMATION;
    
    public AesEncryption(ComponentContainer container) {
        super(container.$form());
    }

    @SimpleFunction(description = "Initialize the AES encryption settings.")
    public void Initialize() {
        // This can be used to set default values or reset keys
    }

    @SimpleFunction(description = "Set the private key for encryption.")
    public void SetPrivateKey(String key, int bits) {
        this.secretKey = key.substring(0, bits / 8); // Adjust key according to bits
    }

    @SimpleFunction(description = "Set the Initialization Vector (IV) key.")
    public void SetIVKey(String ivKey) {
        this.ivKey = ivKey;
    }

    @SimpleFunction(description = "Set the padding type for encryption.")
    public void SetPaddingType(String paddingType) {
        this.paddingType = paddingType;
        this.encryptionMethod = AES + "/CBC/" + paddingType;
    }

    @SimpleFunction(description = "Set the encryption method.")
    public void SetEncryptionMethod(String method) {
        this.encryptionMethod = method;
    }

    @SimpleFunction(description = "Encrypt the text.")
    public String EncryptText(String plainText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        Key keySpec = new SecretKeySpec(secretKey.getBytes(), AES);
        IvParameterSpec ivSpec = new IvParameterSpec(ivKey.getBytes());
        
        Cipher cipher = Cipher.getInstance(encryptionMethod);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        
        byte[] encrypted = cipher.doFinal(plainText.getBytes());
        return java.util.Base64.getEncoder().encodeToString(encrypted);
    }

    @SimpleFunction(description = "Decrypt the text.")
    public String DecryptText(String encryptedText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        Key keySpec = new SecretKeySpec(secretKey.getBytes(), AES);
        IvParameterSpec ivSpec = new IvParameterSpec(ivKey.getBytes());
        
        Cipher cipher = Cipher.getInstance(encryptionMethod);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        
        byte[] decodedValue = java.util.Base64.getDecoder().decode(encryptedText);
        byte[] decrypted = cipher.doFinal(decodedValue);
        return new String(decrypted);
    }
}

Try this:


package io.aesencryption;


import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.common.ComponentCategory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.InvalidAlgorithmParameterException;

@DesignerComponent(version = 1,
   description = "AES Encryption Extension",
   category = ComponentCategory.EXTENSION,
   nonVisible = true,
   iconName = "images/extension.png")
@SimpleObject(external = true)
public class AesEncryption extends AndroidNonvisibleComponent implements Component {

    private static final String AES = "AES";
    private static final String DEFAULT_PADDING = "PKCS5Padding";
    private static final String DEFAULT_TRANSFORMATION = AES + "/CBC/" + DEFAULT_PADDING;
    
    private String secretKey = "default_key";
    private String ivKey = "default_iv";
    private String paddingType = DEFAULT_PADDING;
    private String encryptionMethod = DEFAULT_TRANSFORMATION;
    
    public AesEncryption(ComponentContainer container) {
        super(container.$form());
    }

    @SimpleFunction(description = "Initialize the AES encryption settings.")
    public void Initialize() {
        // This can be used to set default values or reset keys
    }

    @SimpleFunction(description = "Set the private key for encryption.")
    public void SetPrivateKey(String key, int bits) {
        this.secretKey = key.substring(0, bits / 8); // Adjust key according to bits
    }

    @SimpleFunction(description = "Set the Initialization Vector (IV) key.")
    public void SetIVKey(String ivKey) {
        this.ivKey = ivKey;
    }

    @SimpleFunction(description = "Set the padding type for encryption.")
    public void SetPaddingType(String paddingType) {
        this.paddingType = paddingType;
        this.encryptionMethod = AES + "/CBC/" + paddingType;
    }

    @SimpleFunction(description = "Set the encryption method.")
    public void SetEncryptionMethod(String method) {
        this.encryptionMethod = method;
    }

    @SimpleFunction(description = "Encrypt the text.")
    public String EncryptText(String plainText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        Key keySpec = new SecretKeySpec(secretKey.getBytes(), AES);
        IvParameterSpec ivSpec = new IvParameterSpec(ivKey.getBytes());
        
        Cipher cipher = Cipher.getInstance(encryptionMethod);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        
        byte[] encrypted = cipher.doFinal(plainText.getBytes());
        return java.util.Base64.getEncoder().encodeToString(encrypted);
    }

    @SimpleFunction(description = "Decrypt the text.")
    public String DecryptText(String encryptedText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        Key keySpec = new SecretKeySpec(secretKey.getBytes(), AES);
        IvParameterSpec ivSpec = new IvParameterSpec(ivKey.getBytes());
        
        Cipher cipher = Cipher.getInstance(encryptionMethod);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        
        byte[] decodedValue = java.util.Base64.getDecoder().decode(encryptedText);
        byte[] decrypted = cipher.doFinal(decodedValue);
        return new String(decrypted);
    }
}

i tried the above code to compile in nitron ide , but getting this error @Django_s_Android_App

javac:
[mkdir] Created dir: /compiler/android/build/vwtRc/classes
[javac] Compiling 1 source file to /compiler/android/build/vwtRc/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /compiler/android/src/vwtRc/io/aesencryption/AESENCRYPTION6.java:27: error: class AesEncryption is public, should be declared in a file named AesEncryption.java
[javac] public class AesEncryption extends AndroidNonvisibleComponent implements Component {
[javac] ^
[javac] 1 error
[javac] 1 warning

Read the error message and use the correct filename
Taifun

I can compile it with AI Sources without problems

Because most probably you used the correct filename, which is AesEncryption.java and not AESENCRYPTION6.java as the OP

Taifun

i will try. this once

1 Like