Home » Uncategorized » A simple way to encrypt texts with App Inventor

A simple way to encrypt texts with App Inventor



This post was initially published for AppInventor classic, but currently sources are available also for AI2. However, I have not updated the comments and images because the code is roughly the same.

This post explains a simple method to encrypt/decrypt a text using exclusively the functions provided by App Inventor, i.e. without need to call a specialized library. One of the advantages of using only client code is that it does not require an Internet connection to work.

An encryption algorithm is a function that applies a certain transformation (encryption) to an input string in order to obtain an output string which can afterwards be transformed again (decrypted) in order to get back to the original string. Encryption algorithms are usually public, but (in theory) only the encrypter system is able to perform the decryption because the transformation algorithms use a secret key only known by the system.

The reason for building the solution explained in this tutorial is that I could not find one that met my requirements. As explained in this post from Google Groups, we can consider 3 ways to implement encryption in AI:

  1. Call a server side library using the StartActivity component. The AI Extensions mentioned in the same post seem to be an excellent choice for this purpose
  2. Use javascript in the client side. This is based on the clever idea of downloading such scripts in the first run as explained this post from Puravida
  3. Program your own algorithm using AI code blocks. One solution for this is the Encryption and Decryption v3 from Abacus apps.

I didn’t like the first solution because in my case I have the original text in the client side so it looks safer sending it already encrypted to the server. The second option meets this requirement but I was not comfortable with relying on javascript and additional downloaded files. So I decided to try the third approach, which is the one explained in this utorial.

Next question is why not the solution mentioned in the third point. Well, there are three reasons for that. The first one (and to be honest the real reason) is that this solution was not posted when I needed it. The second reason why my solution might still be useful is that it’s so simple that it has immediate response time whereas it seems the mentioned solution can take up to 22 seconds as explained in this post. The final reason is that this site intends to provide some instructive material that helps intermediate readers improve their skills on AI and development in general, and I hope the post can be useful in this sense regardless of whether the results is used.

That being said, if you’re lloking for a more sophisticated solution developed in App Inventor you should refer to the solution mentioned in the third point which will probably have more support and evolution than my humble contribution.

What are we going to build

The method is inspired in standard encryption principles but with several workarounds to overcome the lack of some functions in App Inventor. Actually it is simpler to program than to explain, so maybe you’ll prefer to skip next section and go directly to the code or to the example below. Anyway for those interested I’ll try to explain the theory behind this code.

How it works

We’re going to implement a symmetric encryption algorithm, i.e. one that uses the same key to encrypt and decrypt. A usual means to achieve this is the XOR function because it has the property that XORing a data twice with the same key results in the same data. The XOR function receives two arguments (in our scenario the input string and the secret key) and obtains the output by comparing each bit from the two arguments and generating a 1 for different bits and 0 otherwise. For example, if the input string is “ac” and the key is “12” we would do the following:


So the output string would be “PQ”. Now we’ll apply the same algorithm to obtain the original string:


Unluckily App Inventor does not provide the XOR function (see related issue) nor bitwise manipulation functions that would allow simulating it. So we’ll use a substitute mechanism: create a string containing all the possible characters of the input string, and substitute each character by the opposed in this string, i.e. substitute the first character by the last one, the second by the last but one, etc. An example is simpler to understand: if the allowed characters are “aeiouAEIOU” and the input string is “aA” then the output string will be “Uu”. Notice that if we apply the same transformation to the string “Uu” we obtain again the original “aA” string.  This way we have a reversible function like XOR, but with this alone our encryption would be very weak because anyone reading this tutorial (or with some imagination) would be able to decrypt it. That’s where the key comes into play. What we’ll do is “disorder” the allowed characters string by placing at the beginning the characters of the key. For example if the allowed characters are aeiouAEIOU and the key is “aEiO” we’ll generate the encrypter string “aEiOeouAIU


I have no isolated sources for this tutorial because its implementation is embedded into a more general application (a “Login template” that uses encryption to store passwords). So if you need to implement this algorithm you can either type the code looking at the images below or download the whole sources of the login template. By the way, if you’re interested in the template you can have a look at the Login template tutorial.

User interface

I won’t show the graphical part of this functionality because it’s mixed with other controls so it would be difficult to show separately the specific components. Moreover, the interface has no secret: if you want to a make a quick test you just need to create a screen with two textboxes (for the input string and the key), a button (to perform the encryption) and a label (to show the results).

So let’s go with the code!

Code blocks

Before seeing the encryption and decryption functions, let’s have a look at the required initializations:



  • ALPHANUMERIC_CHARS: A string composed by all the characters that can appear in the input string. You can add or remove characters if you wish to
  • CRYPTO_KEY: Your specific key. You SHOULD change its value. You can use only characters contained in the ALPHANUMERIC_CHARS variable. The string can have any length, but it’s advisable for it to have at least 10 characters. You can repeat the same character several times in the key, but only the first one will be taken into account


  • sEncrypterKey: contains the key string used by the encrypter and decrypter algorithms
  • sVal, sRet_xor: auxiliary variables

Initialization functions

  • InitCrypto: This function informs the sEncrypterKey variable with the same characters as the ALPHANUMERIC_CHARS variable but disordered (i.e. the characters from CRYPTO_KEY are moved at the beginning). The code consists of a simple loop through the CRYPTO_KEY characters in order to move them at the beginning. Notice the “if” line which is used to ignore any repeated characters

Main functions


  • encrypt , decrypt: Yes, as easy as that! The output string is simply obtained by applying the pseudoXOR function to the input string
  • pseudoXOR: The “reversing” function. It’s based on a loop that reads each character from the input string and locates its opposed character within the sEncrypterKey variable.

Further ideas

The algorithm explained in this tutorial is very basic. It would not be very hard for an expert to decipher the key if he was able to capture enough couples of encrypted-decrypted strings. If you want to sophisticate it but don’t wish to recur to the above-mentioned more complex solutions, you can extend the code in many ways. For example we could add a random number of characters at the beginning and at the end of the deciphered string in order to mislead hackers. In this case, for the decryption to work we should also include a couple of counters indicating where is the real string. This is better explained with an example:

Input string: dog
Key: sample
Allowed characters: abcdefghijklmnopqrstuvwxyz
Previous characters: 1
Ending characters: 5
1) Disorder the allowed characters string in order to obtain the encrypter string: samplebcdfghijknopqrtuvwxyz
2) Reverse the input string: qbp
3) Add random characters: wqbppoldw
4) Add two counters at the beginning: amwqbppoldw (see next point to understand it)
1) Obtain the real string. “a” is the second character in the encrypter string and “m” is the third. This means the real string starts at position 2 and has a length of 3, i.e. it’s the string qbp
2) Reverse the string: dog

To implement this variation we should modify the encrypt and decrypt functions in the following way:




  1. John Bayly says:

    This is an excellent way to encrypt text but unfortunately it crashes. I realised that using characters outside those declared in the variable ALPHANUMERIC_CHARS was causing a crash in the plaintext input for encryption but once I declared the additional characters or removed them, the crash stopped. However, if I then changed the key, there is a crash based on the key value. Unfortunately it seems to be random as some keys work and others don’t for particular text.

    Is there another, simple, method of encryption?

    • eixerits says:

      Thanks by the comments John. Indeed youcan only use the characters specified in the variable ALPHANUMERIC_CHARS so you did the right thing adding any extra characters you wish to use.

      About the second crash, I can’t figure out the reason. Can you give more more information (e.g. an example that crashes?).

  2. First of all thanks for your interesting article and the resources.
    This thread is more than a year old but I just got into it because I was looking for a simple encryption method in order to generate an API key.
    I’m having Jonh’s same problems: some chars crash (i.e. “n”) the app, that throws a “Segment: Start is less than 1 (0).
    I don’t know if you could be still available to give a look, in case I can send you the testing app I was playing with that uses exactly your same blocks from the AI2 file.

  3. First of all thanks for your interesting article and the resources.
    This thread is more than a year old but I just got into it because I was looking for a simple encryption method in order to generate an API key.
    I’m having Jonh’s same problems: some chars crash (i.e. “n”) the app, that throws a “Segment: Start is less than 1 (0).
    I don’t know if you could be still available to give a look, in case I can send you the testing app I was playing with that uses exactly your same blocks from the AI2 file.

  4. Ok, the aia file is ready. Please let me know the best way you want to have it. I could post a temporary download link, but I prefer to wait your authorization first.

  5. Just a small update after looking better at your code.
    The “n” char in the encrypt string makes the app crash because is missing in your original ‘ALPHANUMERIC_CHARS’ variable, so it’s not a real problem (sorry for not recognizing it before).

    Another thing I noticed is that there’s a wrong counting in the ‘pseudoXOR’ procedure’s loop that shiftes 1 unity.
    In fact the replacement between the first char of ‘sEncrypterKey’ and the last one (using your defaults ABC……..key) occurs beetween the first and the penultimate: ‘A’ is replaced by ‘e’ and not ‘y’.
    When the loop goes through all the string it ends with the last char that goes 0 and throws the ‘Segment: Start is less than 1 (0)’ error.
    So if in the text to encrypt (or decrypt) is placed the last char of ‘sEncrypterKey’ (in this case the char ‘y’) then we have the crashing error, in all other cases your code works fine.

    • eixerits says:

      You’re absolutely right! I’ll fix both issues immediately to avoid other people falling into them. Most likely they were also the cause of the problem reported by John.

      I understand there’s no need to review your aia any more, but I’ll send you an email anyway so that you have my contact.

      Thanks for contributing to debug the blog!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: