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

标签 java android security rsa

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

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) 中得到一个 NullPointer。 既然理论似乎是正确的,我觉得这可能是我没有遵循的盲目伎俩,那么发生这种情况的原因可能是什么

编辑:编辑代码以删除冗余代码,如建议的

最佳答案

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

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

相关文章:

java - 从另一个类访问 Java 类中的私有(private)字段

java - 当一个类只调用另一个类有很多方法的一个方法时,如何降低耦合?

android - 在同一应用中不同 Activity 的调用之间保留文本字段上的数据

java - Terracotta Ehcache : server disconnects during debug

java - maven flex 构建失败

java - 获取用户当前的纬度/经度

android - fragment 中的 MergeAdapter 布局问题

security - `--disable-web-security` 命令似乎不再起作用

php - 使用 $_GET

php - Symfony2 安全 - 用户 "Ibw\UserBundle\Entity\User"没有用户提供者