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.

Komentar

Cara pembuatan Apps paling Populer

Create Stopwatch App in Android using Sketchware

How to enable download in webview in Sketchware apps?

TextInputLayout in Sketchware

Create Stopwatch App in Android using Sketchware

How to integrate Admob Ads in Sketchware project using AIDE?

How to find and​ highlight a word in a text field in Sketchware?

Code for implementing Notifications in Sketchware

A Flash Light App in Sketchware

Create a Stopwatch App using Chronometer in Sketchware

Create app to save all passwords, protected using voice key