📜  使用凯撒密码算法的Android加解密应用

📅  最后修改于: 2022-05-13 01:55:05.257000             🧑  作者: Mango

使用凯撒密码算法的Android加解密应用

下面我们来做一个“加解密”的应用。通过制作这个应用程序,我们将能够了解如何将普通文本转换为密文并加密我们的消息。我们还将在密钥的帮助下解密我们的消息,再次将其转换为可读形式。本文将帮助您介绍android开发中密码学的基本概念。

先决条件:

在继续应用程序之前,您应该了解 Caesar Cipher Algorithm of Cryptography。如果您不知道,请使用密码学中的 Caesar Cipher 文章来了解它。

我们将在本文中构建什么?

在这个应用程序中,我们将提供一个空间(TextView)来显示加密或解密消息的输出。消息、密文和密钥将作为用户的输入。将使用名为实用程序的Java类来编写加密和解密按钮的逻辑。请注意,我们将使用Java语言实现此应用程序。下面给出了一个示例视频,以了解我们将在本文中做什么。

现在让我们看看这个应用程序的逐步实现。



分步实施

第 1 步:创建一个新项目

  • 打开一个新项目。
  • 我们将使用Java语言处理 Empty Activity。保持所有其他选项不变。
  • 您可以在方便时更改项目的名称。
  • 将有两个名为activity_main.xml 和 MainActivity 的默认文件。Java

如果您不知道如何在 Android Studio 中创建新项目,那么您可以参考如何在 Android Studio 中创建/启动新项目?

第 2 步:使用 activity_main.xml 文件

在这里,我们将设计应用程序的用户界面。我们将在各自的作品中使用以下组件:

  • TextView – 显示输出(加密或解密的消息)。
  • EditText – 接受输入(消息、密文和密钥)。
  • 按钮 – 单击时加密或解密消息。

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。

XML


  
    
    
  
    
    
  
    
    


Java
public class utility {
  
    // Declaration of all the required variables
    private static String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static int index;
    private static int updated_index;
    private static int final_index;
    private static int index_p_t_l;
    private static int index_s_t_l;
    private static String plainTxt;
    private static String cipherTxt;
    private static String finalTxt;
  
    // code for encryption
    public static String encrypt1(String plaintext, int encrptionKey) {
        reset();
        // plaintext is converted to uppercase 
        // so that it is easy to convert according
        // to Caesar Cipher algorithm
        plaintext = plaintext.toUpperCase();
        // using a for loop the use index and 
        // change text with help of it
        for (index = 0; index < plaintext.length(); index++) {
            // checking the condition for plaintext to be
            // null at some character position
            if (plaintext.charAt(index) != ' ') {
                index_p_t_l = alphabet.indexOf(plaintext.charAt(index));
                // index is being updated 
                // so that next and final index
                // be used for ciphertext
                updated_index = encrptionKey + alphabet.indexOf(plaintext.charAt(index));
                if (updated_index >= alphabet.length()) {
                    final_index = updated_index - alphabet.length();
                } else
                    final_index = updated_index;
                // substring is used so that every character
                // can be separately converted to ciphertext
                cipherTxt = alphabet.substring(final_index, final_index + 1);
                finalTxt = finalTxt + cipherTxt;
            }
        }
        // returning the 
        // final changed text
        return finalTxt;
    }
  
    // code for decryption
    public static String decrypt1(String ciphertext, int decryptionKey) {
        reset();
        ciphertext = ciphertext.toUpperCase();
        // using a for loop the use index and 
        // change text with help of it
        for (index = 0; index < ciphertext.length(); index++) {
            if (ciphertext.charAt(index) != ' ') {
                index_p_t_l = alphabet.indexOf(ciphertext.charAt(index));
                index_s_t_l = index_p_t_l;
                // index is updated with help of decryption
                // key which we provided as input
                updated_index = alphabet.indexOf(ciphertext.charAt(index)) - decryptionKey;
                if (updated_index < 0) {
                    final_index = updated_index + alphabet.length();
                } else
                    final_index = updated_index;
                // reverse of encryption is done as 
                // substring here is used to convert
                // each ciphertext character to plaintext 
                plainTxt = alphabet.substring(final_index, final_index + 1);
                finalTxt += plainTxt;
            }
        }
        // returning the 
        // final changed text
        return finalTxt;
    }
  
    // method to reset the text
    // in the output textview
    private static void reset() {
        finalTxt = "";
    }
}


Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // declaring all essential variables
    private Button encrypt, decrypt;
    private EditText message, cipher, key;
    private TextView screen_output;
    private static final String alphabetString = "abcdefghijklmnopqrstuvwxyz";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // findViewById is the method that 
        // finds the View by the ID it is given
        encrypt = findViewById(R.id.btnencrypt);
        decrypt = findViewById(R.id.btndecrypt);
        screen_output = findViewById(R.id.tV1);
        message = findViewById(R.id.inputMessage);
        cipher = findViewById(R.id.ciphEdt);
        key = findViewById(R.id.key_dt);
  
        // setting onCLickLister on encrypt button
        encrypt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                encrypt12(message.getText().toString(), Integer.parseInt(key.getText().toString()));
            }
        });
  
        // setting onCLickLister on decrypt button
        decrypt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                decrypt12(cipher.getText().toString(), Integer.parseInt(key.getText().toString()));
            }
        });
    }
  
    // method to show the final output on the output
    // textView when decrypt button is clicked
    public void decrypt12(String cipher, int key) {
        screen_output.setText((utility.decrypt1(cipher, key).toLowerCase()));
    }
  
    // method to show the final output on the output
    // textView when encrypt button is clicked
    public String encrypt12(String message, int shiftkey) {
        message = message.toLowerCase();
        String cipherText = "";
        for (int i = 0; i < message.length(); i++) {
            int charPosition = alphabetString.indexOf(message.charAt(i));
            int keyval = (shiftkey + charPosition) % 26;
            char replaceVAL = alphabetString.charAt(keyval);
            cipherText += replaceVAL;
            screen_output.setText(cipherText);
            cipher.setText(cipherText);
        }
  
        // returning the final ciphertext
        return cipherText;
    }
}


执行上述代码后,activity_main.xml 文件的设计是这样的。



第 3 步:创建一个新的Java类

创建一个新的Java类,如下所示,并将其命名为“ utility ”。

第 4 步:使用实用程序。 Java文件

加密和解密我们的消息的所有逻辑都将写在这个类中。使用本课程中提供的以下代码:

Java

public class utility {
  
    // Declaration of all the required variables
    private static String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static int index;
    private static int updated_index;
    private static int final_index;
    private static int index_p_t_l;
    private static int index_s_t_l;
    private static String plainTxt;
    private static String cipherTxt;
    private static String finalTxt;
  
    // code for encryption
    public static String encrypt1(String plaintext, int encrptionKey) {
        reset();
        // plaintext is converted to uppercase 
        // so that it is easy to convert according
        // to Caesar Cipher algorithm
        plaintext = plaintext.toUpperCase();
        // using a for loop the use index and 
        // change text with help of it
        for (index = 0; index < plaintext.length(); index++) {
            // checking the condition for plaintext to be
            // null at some character position
            if (plaintext.charAt(index) != ' ') {
                index_p_t_l = alphabet.indexOf(plaintext.charAt(index));
                // index is being updated 
                // so that next and final index
                // be used for ciphertext
                updated_index = encrptionKey + alphabet.indexOf(plaintext.charAt(index));
                if (updated_index >= alphabet.length()) {
                    final_index = updated_index - alphabet.length();
                } else
                    final_index = updated_index;
                // substring is used so that every character
                // can be separately converted to ciphertext
                cipherTxt = alphabet.substring(final_index, final_index + 1);
                finalTxt = finalTxt + cipherTxt;
            }
        }
        // returning the 
        // final changed text
        return finalTxt;
    }
  
    // code for decryption
    public static String decrypt1(String ciphertext, int decryptionKey) {
        reset();
        ciphertext = ciphertext.toUpperCase();
        // using a for loop the use index and 
        // change text with help of it
        for (index = 0; index < ciphertext.length(); index++) {
            if (ciphertext.charAt(index) != ' ') {
                index_p_t_l = alphabet.indexOf(ciphertext.charAt(index));
                index_s_t_l = index_p_t_l;
                // index is updated with help of decryption
                // key which we provided as input
                updated_index = alphabet.indexOf(ciphertext.charAt(index)) - decryptionKey;
                if (updated_index < 0) {
                    final_index = updated_index + alphabet.length();
                } else
                    final_index = updated_index;
                // reverse of encryption is done as 
                // substring here is used to convert
                // each ciphertext character to plaintext 
                plainTxt = alphabet.substring(final_index, final_index + 1);
                finalTxt += plainTxt;
            }
        }
        // returning the 
        // final changed text
        return finalTxt;
    }
  
    // method to reset the text
    // in the output textview
    private static void reset() {
        finalTxt = "";
    }
}

第 5 步:使用 MainActivity。 Java文件

在主活动中。加密和解密按钮上使用的是Java文件onClickListerner,工具类中的方法直接传递在这里。在MainActivity 中使用以下代码。 Java文件。

Java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // declaring all essential variables
    private Button encrypt, decrypt;
    private EditText message, cipher, key;
    private TextView screen_output;
    private static final String alphabetString = "abcdefghijklmnopqrstuvwxyz";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // findViewById is the method that 
        // finds the View by the ID it is given
        encrypt = findViewById(R.id.btnencrypt);
        decrypt = findViewById(R.id.btndecrypt);
        screen_output = findViewById(R.id.tV1);
        message = findViewById(R.id.inputMessage);
        cipher = findViewById(R.id.ciphEdt);
        key = findViewById(R.id.key_dt);
  
        // setting onCLickLister on encrypt button
        encrypt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                encrypt12(message.getText().toString(), Integer.parseInt(key.getText().toString()));
            }
        });
  
        // setting onCLickLister on decrypt button
        decrypt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                decrypt12(cipher.getText().toString(), Integer.parseInt(key.getText().toString()));
            }
        });
    }
  
    // method to show the final output on the output
    // textView when decrypt button is clicked
    public void decrypt12(String cipher, int key) {
        screen_output.setText((utility.decrypt1(cipher, key).toLowerCase()));
    }
  
    // method to show the final output on the output
    // textView when encrypt button is clicked
    public String encrypt12(String message, int shiftkey) {
        message = message.toLowerCase();
        String cipherText = "";
        for (int i = 0; i < message.length(); i++) {
            int charPosition = alphabetString.indexOf(message.charAt(i));
            int keyval = (shiftkey + charPosition) % 26;
            char replaceVAL = alphabetString.charAt(keyval);
            cipherText += replaceVAL;
            screen_output.setText(cipherText);
            cipher.setText(cipherText);
        }
  
        // returning the final ciphertext
        return cipherText;
    }
}

恭喜!!您已经成功地使用凯撒密码算法制作了加密和解密应用程序。这是我们应用程序的最终输出。

输出:

GitHub 链接: https : //github.com/Karan-Jangir/caesar_cipher_algorithm_crytography