【问题标题】:Simple Encrypt Decrypt String with array in Android在Android中使用数组简单加密解密字符串
【发布时间】:2016-04-12 01:13:23
【问题描述】:

我的问题似乎很简单,我正在尝试在 android 中加密一个字符串。

What the program should do

该程序的目标是,在 textbox1 中输入一个单词或一个字母,单击按钮 encrypt 并且 textbox2 应该在您输入的字母的第二个数组中的相同位置显示符号。

例如我写letter A (array 1 [0])必须显示symbol $ (array 2 [0])如果点击ENCRYPT按钮,如果我放一个符号并点击DECRYPT必须显示与数组位置等效的字母

需要帮助。抱歉语法和编辑问题,第一次没有说英语,我试图让它更简单。

Arreglo 类,仅用于声明 2 个数组。

package PaqueteArreglo;

public class Arreglo {
public String [] encrypt = new String[3];
public String [] decrypt = new String[3];
}

主要活动

package com.example.encrypt;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import PaqueteArreglo.Arreglo;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

//Instance the class with the arrays
PaqueteArreglo.Arreglo ins = new Arreglo();
public Button button1,button2;
public EditText text1, text2;

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

  ins.encrypt[0] = "a";
  ins.encrypt[1] = "b";
  ins.encrypt[2] = "c";

  ins.decrypt[0] = "$";
  ins.decrypt[1] = "%";
  ins.decrypt[2] = "&";

}

private void initialize(){
  text1 = (EditText)findViewById(R.id.txtWord);
  text2 = (EditText)findViewById(R.id.txtResult);

  button1 = (Button)findViewById(R.id.btnEncrypt);
  button1.setOnClickListener(this);

  button2 =(Button)findViewById(R.id.btnDecrypt);
  button2.setOnClickListener(this);

}

@Override
public void onClick(View view) {
  String word = String.valueOf(this.text1.getText());

  if (view.getId() == R.id.btnEncrypt)
  {
     String demo = "";
     int lon = 0;
     lon = word.length();
     for (int i = 0; i < lon; i++ )
     {
        if (ins.encrypt[i].equalsIgnoreCase(String.valueOf(word.charAt(i))))
        {
           demo = demo + ins.decrypt[i];
           text2.setText(demo);
        }
     }
  }
}
}

【问题讨论】:

  • 您可能应该使用某种映射(如 HashMap)将您的加密字符映射到它的匹配解密字符。此外,目前还不清楚您遇到的具体问题是什么。你能把问题说清楚一点吗?
  • 感谢您的回答,我编辑问题以使解释更简单,我不知道使用(为此不需要)HashMap,我需要用这种方法解决问题,谢谢。
  • 请编辑以显示Arreglo
  • @RogerJimenez 你解决问题了吗?

标签: java android arrays string encryption


【解决方案1】:

使用两种不同的方法可以解决您的问题:

1- 使用您定义的结构(顺便说一下没有优化),您需要放置两个嵌套循环进行检查:

for (int i = 0; i < lon; i++ )
 {
    for(int j = 0; j<ins.encrypt.length;j++)
    {
       if (ins.encrypt[j].equalsIgnoreCase(String.valueOf(word.charAt(i))))
       {
          demo = demo + ins.decrypt[j];
       }
    }
 }

text2.setText(demo);

2- 使用更好的数据结构,例如 HashMap:

将您的 Arreglo 更改为以下内容:

public class Arreglo {
   public HashMap<Character, Character> encrypt = new HashMap<>();
   public HashMap<Character, Character> decrypt = new HashMap<>();
}

现在更改添加数据的方式,例如:

ins.encrypt.put('a','@');
ins.decrypt.put('@','a');
...

最后像这样进行加密:

for (int i = 0; i < lon; i++ )
{
   demo = demo + ins.encrypt.get(word.charAt(i));
}
text2.setText(demo);

第二种实现效率更高

【讨论】:

    猜你喜欢
    • 2017-02-28
    • 1970-01-01
    • 2011-07-10
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    相关资源
    最近更新 更多