如何在不同的活动中加密和解密?

时间:2014-02-24 17:25:11

标签: android encryption rsa

我在一个活动中加密(RSA)密码,并希望在另一个活动中解密密码,但它无法正常工作。谁能帮我? :(

Encrypt.class

    @Override
    public void onClick(View arg0) {
// TODO Auto-generated method stub
        FILENAME = filename.getText().toString();
        PASSWORD = pass.getText().toString();

        // Generate key pair for 1024-bit RSA encryption and decryption
        Key publicKey = null;
        Key privateKey = null;
        try {
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
            kpg.initialize(1024);
            KeyPair kp = kpg.genKeyPair();
            publicKey = kp.getPublic();
            privateKey = kp.getPrivate();
        } catch (Exception e) {
            Log.e(TAG, "RSA key pair error");
        }

        // Encode the original data with RSA private key
        byte[] encodedBytes = null;
        try {
            Cipher c = Cipher.getInstance("RSA");
            c.init(Cipher.ENCRYPT_MODE, privateKey);
            encodedBytes = c.doFinal(PASSWORD.getBytes());
        } catch (Exception e) {
            Log.e(TAG, "RSA encryption error");
        }

        PASSWORD = Base64.encodeToString(encodedBytes, Base64.DEFAULT);

        try {
            FileOutputStream fos = openFileOutput(FILENAME,
                    Context.MODE_PRIVATE);
            fos.write(PASSWORD.getBytes());
            fos.close();
            Toast.makeText(getApplicationContext(),
                    "Your Credentials are saved.", Toast.LENGTH_SHORT).show();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

Decrypt.class

private void openFile(String filename) {
        // TODO Auto-generated method stub
        String value = "";

        FileInputStream fisw;

        try {
            fisw = openFileInput(filename);
            byte[] input = new byte[fisw.available()];
            while (fisw.read(input) != -1) {
                value += new String(input);

            }
            fisw.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          // Decode the encoded data with RSA public key
         Key publicKey = null;

        byte[] decodedBytes = null;

        try {
            Cipher c = Cipher.getInstance("RSA");
            c.init(Cipher.DECRYPT_MODE, publicKey);
            decodedBytes = c.doFinal(encodedBytes);
        } catch (Exception e) {
            e.printStackTrace();
    }
        entry.setText(value);


    }

我在decrypt.class活动中遇到错误。错误在下面这一行。

  decodedBytes = c.doFinal(encodedBytes);

错误说encodeBytes无法解析为变量。知道怎么解决这个问题?

1 个答案:

答案 0 :(得分:0)

您永远不会在解密类中初始化encodedBytes ...

我猜你要解密value而不是?