java - 方法kpg.initialization中的Android空指针异常

原文 标签 java android security rsa

我是android的新手,我正在尝试制作有关cypher / decypher应用程序的“简单”教程。我有以下代码

public class MainActivity extends AppCompatActivity {
    KeyPairGenerator kpg;
    KeyPair kp;
    PublicKey publicKey;
    PrivateKey privateKey;
    byte[] encryptedBytes, decryptedBytes;
    Cipher cipher, cipher1;
    String encrypted, decrypted;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            generateKey();
            TextView txtPuk = (TextView) findViewById(R.id.tViewPUK);
            txtPuk.setText(kp.getPublic().toString());
        }catch(Exception e){
            e.printStackTrace();
            Toast.makeText(getBaseContext(), e.toString(),Toast.LENGTH_LONG).show();
        }

        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void generateKey() throws NoSuchAlgorithmException, NoSuchPaddingException,
            IllegalBlockSizeException,BadPaddingException,InvalidKeyException{
        try{
                kpg = KeyPairGenerator.getInstance("RSA");
            kpg.initialize(1024); //NULLPOINTER HERE
            kp = kpg.genKeyPair();

        }catch(Exception e){
            e.printStackTrace();
            Toast.makeText(getBaseContext(), e.toString(),Toast.LENGTH_LONG).show();

        }

    }
    public byte[] RSAEncrypt(final String plain) throws NoSuchAlgorithmException, NoSuchPaddingException,
            IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
        ;
        publicKey = kp.getPublic();
        privateKey = kp.getPrivate();

        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        encryptedBytes = cipher.doFinal(plain.getBytes());
        return encryptedBytes;
    }

    public String RSADecrypt(final byte[] encryptedBytes) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

        cipher1 = Cipher.getInstance("RSA");
        cipher1.init(Cipher.DECRYPT_MODE, privateKey);
        decryptedBytes = cipher1.doFinal(encryptedBytes);
        decrypted = new String(decryptedBytes);
        return decrypted;
    }

}


但是正如标题所说,我在kpg.initialize(1024)中得到了一个N​​ullPointer。
由于该理论似乎是正确的,所以我觉得这可能是我没有遵循的盲目的把戏,所以这可能是什么原因

编辑:编辑后的代码以删除冗余代码,例如

最佳答案

问题是在findViewById(R.id.tViewPUK)之前调用setContentView(R.layout.activity_main);

关于java - 方法kpg.initialization中的Android空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30607905/

相关文章:

java - 验证用户名和密码

java - Android中带有线程的异步套接字通信

java - 如果未输入有效的整数,如何使程序终止并出现错误?

java - Spring MVC如何显示静态页面

java - 在java中从一个文本文件创建多个文件

android - 使用SupportToolbar进行电话布局和使用独立的操作栏进行平板电脑布局

android - Koin + Retrofit在运行时添加标题

javascript - 加密通行证的重新哈希使用Node.js中的加密返回不同的结果

security - 防止 session 劫持的最佳方法是什么?

java - 串联JSTL