0

申し訳ありませんが、私は Android 開発の初心者であり、最終プロジェクトでこの単純な問題が発生し、stackoverflow から学んだすべてのことを試しましたが、うまくいきませんでした。私のプロジェクトは、単純なクライアント/サーバー暗号化メッセージ アプリケーションです。サンプル コードを見つけてクラスを作成しました。クラスは正常に動作していますが、連携できません:( クラス 1 (EncryptActivity) RSA を使用して独自の入力からデータを暗号化します。クラス 2 (MyActivity) はサーバーとの TCP 接続を確立して送信しますプレーンテキスト。

しかし、平文を MyActivity から EncryptActivity に送信して暗号化されたテキストを取得したい場合、常に「null」になります。

データを渡そうとした方法は、コメント「//」を含むコードにあります

私を助けてください :((

暗号化アクティビティ:

public class EncryptActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    try {
        generateKey();
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.encryptinput);

    final Button enButton = (Button) findViewById(R.id.bEncrypt);
    final Button deButton = (Button) findViewById(R.id.bDecrypt);
    final EditText input = (EditText) findViewById(R.id.etPlainText);
    final EditText Raw = (EditText) findViewById(R.id.etEncryptedText);
    final EditText output = (EditText) findViewById(R.id.etDecryptedText);

    enButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                // Encrypted text
                Raw.setText(encrypt(input.getText().toString()));

                // Receive plain text from MyActivity
                //
                // Bundle gotContainer = getIntent().getExtras();
                // String gotPlainText = gotContainer.getString("key");
                // input.setText(gotPlainText);

                // Send encrypted to MyActivty
                //
                // Bundle container2 = new Bundle();
                // container2.putString(encrypt(input.getText().toString()),
                // "key2");
                // Intent myIntent2 = new Intent(EncryptActivity.this,
                // MyActivity.class);
                // myIntent2.putExtras(container2);
                // startActivity(myIntent2);

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

        };
    });

    deButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                output.setText(String.valueOf(decrypt(Raw.getText()
                        .toString())));
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

}

private final static String RSA = "RSA";
public static PublicKey uk;
public static PrivateKey rk;

public static void generateKey() throws Exception {
    KeyPairGenerator gen = KeyPairGenerator.getInstance(RSA);
    gen.initialize(512, new SecureRandom());
    KeyPair keyPair = gen.generateKeyPair();
    uk = keyPair.getPublic();
    rk = keyPair.getPrivate();
}

private static byte[] encrypt(String text, PublicKey pubRSA)
        throws Exception {
    Cipher cipher = Cipher.getInstance(RSA);
    cipher.init(Cipher.ENCRYPT_MODE, pubRSA);
    return cipher.doFinal(text.getBytes());
}

public final static String encrypt(String text) {
    try {
        return byte2hex(encrypt(text, uk));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public final static String decrypt(String data) {
    try {
        return new String(decrypt(hex2byte(data.getBytes())));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

private static byte[] decrypt(byte[] src) throws Exception {
    Cipher cipher = Cipher.getInstance(RSA);
    cipher.init(Cipher.DECRYPT_MODE, rk);
    return cipher.doFinal(src);
}

public static String byte2hex(byte[] b) {
    String hs = "";
    String stmp = "";
    for (int n = 0; n < b.length; n++) {
        stmp = Integer.toHexString(b[n] & 0xFF);
        if (stmp.length() == 1)
            hs += ("0" + stmp);
        else
            hs += stmp;
    }
    return hs.toUpperCase();
}

public static byte[] hex2byte(byte[] b) {
    if ((b.length % 2) != 0)
        throw new IllegalArgumentException("hello");

    byte[] b2 = new byte[b.length / 2];

    for (int n = 0; n < b.length; n += 2) {
        String item = new String(b, n, 2);
        b2[n / 2] = (byte) Integer.parseInt(item, 16);
    }
    return b2;
}

}

マイアクティビティ:

public class MyActivity extends Activity {

private ListView mList;
private ArrayList<String> arrayList;
private MyCustomAdapter mAdapter;
private TCPClient mTcpClient;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    arrayList = new ArrayList<String>();

    final EditText editText = (EditText) findViewById(R.id.editText);
    Button send = (Button) findViewById(R.id.send_button);

    // relate the listView from java to the one created in xml
    mList = (ListView) findViewById(R.id.list);
    mAdapter = new MyCustomAdapter(this, arrayList);
    mList.setAdapter(mAdapter);

    // connect to the server
    new connectTask().execute("");

    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String message = editText.getText().toString();

            // String encMessage =
            // EncryptActivity.encrypt(editText.getText().toString());

            // Send plain text to EncryptActivity
            //
            // Bundle container = new Bundle();
            // container.putString(message, "key");
            // Intent myIntent = new Intent(MyActivity.this,
            // EncryptActivity.class);
            // myIntent.putExtras(container);
            // startActivity(myIntent);

            // Receive encrypted data from EncryptActivity
            //
            // Bundle gotContainer2 = getIntent().getExtras();
            // String gotEncrypted = gotContainer2.getString("key2");

            // add the text in the arrayList
            arrayList.add("Me: " + message);

            // sends the message to the server
            // Change message to gotEncrypted but doesn't work :(
            if (mTcpClient != null) {
                mTcpClient.sendMessage(message);
            }

            // refresh the list
            mAdapter.notifyDataSetChanged();
            editText.setText("");
        }
    });

}

public class connectTask extends AsyncTask<String, String, TCPClient> {

    @Override
    protected TCPClient doInBackground(String... message) {

        // we create a TCPClient object and
        mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
            @Override
            // here the messageReceived method is implemented
            public void messageReceived(String message) {
                // this method calls the onProgressUpdate
                publishProgress(message);
            }
        });
        mTcpClient.run();

        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);

        // in the arrayList we add the messaged received from server
        arrayList.add(values[0]);
        // notify the adapter that the data set has changed. This means that
        // new message received
        // from server was added to the list
        mAdapter.notifyDataSetChanged();
    }
}

}

4

2 に答える 2

2

key,valueここでペアが逆になっているようです

container.putString(message, "key");

keyを取得するために使用する を最初extraにするgetString()必要があります。周りを変える

container.putString("key", message);

インテント ドキュメント

于 2013-11-04T13:18:52.930 に答える
0

皆さん @codeMagic と @Joffrey に感謝します。あなたが言及した問題を修正しました。そして、EncryptActivity が「null」を返す理由を見つけました。意図は setOnClickListener 内にあり、EncryptActivity でクリックがなかったため、戻り値は「null」でした。

MyActivity クラスはプレーン テキストを EncryptActivity に送信し、暗号化されたテキストを受信できるようになりました

EncryptActivityのこの部分を変更しました:

public void onCreate(Bundle savedInstanceState) {
    try {
        generateKey();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    super.onCreate(savedInstanceState);

    // Receive plain text from MyActivity
    String plainText = getIntent().getStringExtra("plainKey");
    String cipherText = encrypt(plainText);

    // Send encrypted to MyActivty
    Intent cryptIntent = new Intent(EncryptActivity.this, MyActivity.class);
    cryptIntent.putExtra("cipherKey", cipherText);
    startActivity(cryptIntent);
}

MyActivityクラスのいくつかの変更:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    initialize();
}

private void initialize() {

    arrayList = new ArrayList<String>();
    editText = (EditText) findViewById(R.id.editText);
    Button send = (Button) findViewById(R.id.send_button);
    mList = (ListView) findViewById(R.id.list);
    mAdapter = new MyCustomAdapter(this, arrayList);
    mList.setAdapter(mAdapter);
    // connect to the server
    new connectTask().execute("");
    send.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {

    String plainText = editText.getText().toString();

    // Send plain text to EncryptActivity
    Intent plainIntent = new Intent(MyActivity.this, EncryptActivity.class);
    plainIntent.putExtra("plainKey", plainText);
    startActivity(plainIntent);

    // Receive encrypted data from EncryptActivity
    String message = getIntent().getStringExtra("cipherKey");

    // add the text in the arrayList
    arrayList.add("Me: " + plainText);

    // sends the message to the server
    if (mTcpClient != null) {
        mTcpClient.sendMessage(message);
    }

    // refresh the list
    mAdapter.notifyDataSetChanged();
    editText.setText("");
}
于 2013-11-05T10:41:46.623 に答える