 ## CPSC 110-08: Computing on Mobile Phones Spring 2012 Color Cipher

### Using a Color as a Secret Key

A color in App Inventor is represented by a number composed of the three RGB (Red, Green, Blue) primary color numbers plus the Alpha value, which is a measure of the color's transparency. Each of these four components is an integer in the range 0 to 255.

(If you are interested in seeing how App Inventor translates colors into numbers see this explanation.)

For the purpose of cipher we will just consider the three RGB values. That will give us 255 × 255 × 255 = 16,581,375 different colors, each of which can be a secret key for our cipher.

A cipher based on a color key would not be a secure cipher by today's standards. It would be an easy matter to search through all 16,581,375 possible keys. But it would make for an interesting exercise in making and breaking ciphers.

This demo illustrates a simple cipher system that uses color as keys. Scroll down for more details.

### The Color Cipher Demo

ScreenshotQR Code  Android Package: ColorCipher.apk
App Inventor Source Code: ColorCipher.zip ### The Color Cipher System

Alice and Bob share one of the 16 million colors as secret key which they use to encrypt and decrypt messages. A key that can be used both to encrypt and decrypt messages is known as a symmetric key.

Using this key, we devise a simple substitution cipher, in which letters of the alphabet are replaced by colors:

To encrypt a letter into a color:

1. We first convert the letter to a number between 0 and 25, where 'a' is 0, 'b' is 1, and so on. (In this example, all other characters, such as spaces or punctuation marks, are converted into the number 26.)

2. We then add the letter × a SCALING_FACTOR to the shared key. In this example, our scaling factor is the color ORANGE. (The purpose of the scaling factor is to avoid having consecutive letters, 'a', 'b', 'c', be encrypted as consecutive colors.)

In App Inventor blocks, here is how a letter is encrypted, where letterNum is the letter's number (0 to 25) and both key and SCALING_FACTOR are colors:

To decrypt a color into a letter, we perform the encrypt operations in reverse:

• We first subtract the key from the color.

• We then divide the result by the SCALING_FACTOR. This will give us a number between 0 and 25, which we then translate back into a letter between 'a' and 'z'.

In App Inventor blocks, here is how a letter is encrypted, where colorNum is color encryption of the letter and key1, and SCALING_FACTOR are both colors: