凯撒加密Android实现

凯撒加密Android实现

工程代码

项目工程结构图
在这里插入图片描述

MainActivity:

package wyx.caesar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText inputEdit;
    private EditText offSet;
    private TextView resultText;
    private Button add_btn;
    private Button delete_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        inputEdit = (EditText) findViewById(R.id.edit01);
        offSet = (EditText) findViewById(R.id.edit02);
        resultText = (TextView) findViewById(R.id.text04);

        add_btn = (Button) findViewById(R.id.add_btn);
        delete_btn = (Button) findViewById(R.id.delete_btn);

        add_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = inputEdit.getText().toString();
                int key = Integer.parseInt(offSet.getText().toString());
                String result = encrypt(s, key);
                resultText.setText(result);

            }
        });

        delete_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = inputEdit.getText().toString();
                int key = Integer.parseInt(offSet.getText().toString());
                String result = decrypt(s, key);
                resultText.setText(result);

            }
        });
    }


    public String encrypt(String str, int k) {
        StringBuilder result = new StringBuilder();
        for (char c : str.toCharArray()) {
            // 如果字符串中的某个字符是小写字母
            if (c >= 'a' && c <= 'z') {
                c += k % 26; // 移动key%26位
                if (c < 'a')
                    c += 26; // 向左超界
                if (c > 'z')
                    c -= 26; // 向右超界
            }
            // 如果字符串中的某个字符是大写字母
            else if (c >= 'A' && c <= 'Z') {
                c += k % 26; // 移动key%26位
                if (c < 'A')
                    c += 26;// 同上
                if (c > 'Z')
                    c -= 26;// 同上
            }
            result.append(c);
        }
        return result.toString();
    }

    public String decrypt(String str, int k) {
        // 取相反数
        k = 0 - k;
        return encrypt(str, k);
    }

}


main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text01"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:text="明文: "
        android:textSize="20dp"/>


    <EditText
        android:id="@+id/edit01"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/text02"
            android:layout_width="70dp"
            android:layout_height="40dp"
            android:text="偏移量"
            android:textSize="20dp"/>
        <EditText
            android:id="@+id/edit02"
            android:layout_width="70dp"
            android:layout_height="50dp" />
        <Button
            android:id="@+id/add_btn"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="加密"
            android:textSize="20dp"/>
        <Button
            android:id="@+id/delete_btn"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="解密"
            android:textSize="20dp"/>
    </LinearLayout>
    <TextView
        android:id="@+id/text03"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:textSize="20dp"
        android:text="密文"/>
    <TextView
        android:id="@+id/text04"
        android:layout_width="match_parent"
        android:layout_height="40dp" />

</LinearLayout>


AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="wyx.caesar">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

实现效果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_39699765/article/details/84337595