Encrypt and decrypt text

Here is a simple example of encryption and decryption in Sketchware. Follow the steps below.

1. Create a new project in Sketchware.

2. In main.xml add a LinearV linear1 inside a ScrollV vscroll1. Inside this add an EditText edittext1, add two Buttons button1 and button2, and two TextViews textview1 and textview2.

3. Create a More Block extra, here use an add source directly block and put following codes.

}

javax.crypto.KeyGenerator keyGenerator;
javax.crypto.SecretKey secretKey;
byte[] secretKeyen;
String strSecretKey;
byte[] IV = new byte[16];
byte[] cipherText;

public static byte[] encrypt(byte[] plaintext, javax.crypto.SecretKey key, byte[] IV) throws Exception{
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES");
javax.crypto.spec.SecretKeySpec keySpec = new javax.crypto.spec.SecretKeySpec(key.getEncoded(), "AES");
javax.crypto.spec.IvParameterSpec ivSpec = new javax.crypto.spec.IvParameterSpec(IV);
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte[] cipherText = cipher.doFinal(plaintext);
return cipherText;
}

public static String decrypt(byte[] cipherText, javax.crypto.SecretKey key, byte[] IV){
try {
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES");
javax.crypto.spec.SecretKeySpec keySpec = new javax.crypto.spec.SecretKeySpec(key.getEncoded(), "AES");
javax.crypto.spec.IvParameterSpec ivSpec = new javax.crypto.spec.IvParameterSpec(IV);
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] decryptedText = cipher.doFinal(cipherText);
return new String(decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
{

4. In onCreate, make button2 and textview2 setVisible GONE.


5. In event button1 onClick, make button2 and textview2 setVisible VISIBLE, and then use add source directly block to put following codes.

try {
keyGenerator = javax.crypto.KeyGenerator.getInstance("AES");
keyGenerator.init(256);
secretKey = keyGenerator.generateKey();
secretKeyen=secretKey.getEncoded();
cipherText = encrypt(edittext1.getText().toString().getBytes(), secretKey, IV);
textview1.setText(android.util.Base64.encodeToString(cipherText, android.util.Base64.DEFAULT));
} catch ( java.security.NoSuchAlgorithmException e){
showMessage(e.toString());
} catch (Exception e){
showMessage(e.toString());
}

6. In event button2 onClick, use add source directly block to put following codes.
try {
javax.crypto.SecretKey originalSecretKey = new javax.crypto.spec.SecretKeySpec(secretKeyen, 0, secretKeyen.length, "AES");
String decryptedText = decrypt(cipherText, originalSecretKey, IV); textview2.setText(decryptedText);
} catch (Exception e) {
e.printStackTrace();
}

7. Save and run the project.

Comments

  1. I love your videos. And i have been waiting for some encryption to be added. This is good but it is base64 encoding and its easy to decode. All you need to do to decrypt it is run base64 -d in a terminal emulator.

    ReplyDelete
  2. Dear Sanjeev Kumar I'am From Bangladesh 👉🇧🇩.. Please make a tutorial how to setup Firebase Authentication,Database,Storage in Aide to help me 😫😫

    ReplyDelete
    Replies
    1. Make project in sketchware will everything, copy source code, paste ti aide. Done (btw I am from Bangladesh too)

      Delete
  3. excellent bro but i can't copy encrypted one
    plz help

    ReplyDelete
  4. He dosent work when we launch je said "éditetext1 cant ne resoved"

    ReplyDelete
  5. How about if I will decrypt it on other activity?

    ReplyDelete

Post a Comment

Popular posts from this blog

Create Music Player app in Sketchware

A Flash Light App in Sketchware

Admob rewarded video Ads in Sketchware

Firebase auth in Sketchware for Login Activity

Simple car racing android game in Sketchware