【问题标题】:Android secure, what to use instead of StringsAndroid 安全,用什么代替字符串
【发布时间】:2015-03-05 17:03:34
【问题描述】:

我是 Android 新手,我正在制作一个应用程序,我想专注于应用程序的安全性,我找到了这个 link,上面写着“将敏感信息保留在 RAM 中尽可能短的时间使用后将其设置为空。”后来它说“避免使用Java的String类来保存敏感信息。而是使用char数组或字节数组。这样做的原因是因为字符串是不可变的”

在我的应用程序中,我有一个类似的代码(此代码只是检查用户输入的 PIN 并将其与内部存储中的另一个 PIN 进行比较):

public class Class extends Activity implements OnClickListener{
    private static final String fileName = "FilePin";
    private Button button;
    private EditText editText = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_example); 
        editText = (editText) findViewById(R.id.editText); 
        button = (Button) findViewById(R.id.button);  
        button.setOnClickListener(this);
    }   

    @Override
    public void onClick(View v){
        if (readPin()) {
            textView.setText(new char[]{' '}, 0, 0);
            Intent intent = new Intent(this, OtherClass.class);  
            startActivity(intent);
        }
    }

//   this method read the file where the PIN the user create is save in the internal storage
    public boolean readPin(){
        StringBuilder stringBuilder = null;
        StringBuilder inputString;
        try {
            BufferedReader inputReader = new BufferedReader(new InputStreamReader
                    (openFileInput(fileName)));
            stringBuilder = new StringBuilder();
            while ((inputString = new StringBuilder(inputReader.readLine())) != null) {
                stringBuilder.append(inputString);
            }
            inputReader.close();
        } catch (Exception e) {
            e.printStackTrace();
            }
        inputString = new StringBuilder("");
        assert stringBuilder != null;
        boolean comparePin = compare(stringBuilder);
        stringBuilder = new StringBuilder("");
        return comparePin;
    }

//  this method compare the PIN saved with the PIN the users enters
    private boolean compare(StringBuilder pinSaved){
        if (!editText.getText().toString().equals(pinSaved.toString())) {
            Toast.makeText(getBaseContext(), "the PIN it´s incorrect"
                    , Toast.LENGTH_SHORT).show();
            pinSaved = new StringBuilder("");
            return false;
        }
        else {
            pinSaved = new StringBuilder("");
            return true;
        }
    }
}

对于我在预览link 中读到的内容,我没有使用 String,而是使用 StringBuilder,因为 StringBuilder 是可变的,在我使用它之后,将值更改为“stringBuilder = new StringBuilder("");”,我没有使用 char[] 因为我不知道如何将 editText 保存到 char[] 变量或如何将文件中保存的 PIN 保存在 char[] 变量中,而且我没有找到有关如何保存的示例在这些情况下使用 char[]。

我的问题是:这种情况对于 android 应用程序是安全的,还是更改为 char[] 变量更好?, StringBuffer 类对于 Android 是否不安全?如何将 editText 值保存在 char[] 中?如何将文件保存在 char[] 变量中?

【问题讨论】:

  • StringBuilder 只是String 的包装类,将StringBuilder 对象设置为null 并不意味着它使用的String 对象也变为null。
  • 所以最好转移到char[]byte[] 数组,我怎样才能用数组制作类似的方法?我试过了,但我没有成功
  • 您必须声明一个大小为 4 的数组,因为您的 PIN 由 4 个数字组成,每次点击小键盘时,您都必须用该数字填充数组。
  • 所以使用方法editText.setOnEditorActionListener(new OnEditorActionListener() {...} 我想我可以通过添加pin[i] = actionId; 来实现它,但actionId 它是一个int 参数我认为它是按下的键的ASCII 码对吗?,
  • 你不应该使用EditText,它使用存储输入代码的字符串属性,而不是像EditTexts这样的Android控件创建你自己的或谷歌的PIN视图图书馆或类似的东西

标签: android string security secure-coding


【解决方案1】:
stringBuilder = new StringBuilder("");

以上内容比将char[] 归零要弱。原始的StringBuilder 以及内部缓冲区将保留在内存中。在某个时候,垃圾收集器会将内存标记为空闲,但稍后可能会覆盖它。

char[] 归零可让您更好地控制何时覆盖机密。

另外,StringBuilder 带有在需要更多空间时自动复制其内部char[] 缓冲区的逻辑。您需要小心确保它永远不会这样做。一个普通的char[] 很不方便,因为您无法调整它的大小,但在这种情况下它很好,因为任何复制它的尝试都是显式的。

请参阅Java: convert a char[] to a CharSequence,了解如何将您的char[] 转换为可以传递给EditText.setText 的表单。

【讨论】:

  • 好的,我将尝试使用 TextWatcher 类并使用 CharSequencechar[] 的代码
猜你喜欢
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
相关资源
最近更新 更多