RSA key pairs generating using bouncy castle. Making code runnable from java program

后端 未结 1 568
故里飘歌
故里飘歌 2021-02-04 22:25

I am using a Java code that I found that generates a public and a private key via the bouncy castle library. My problem is implementing it into code runnable by my android devi

1条回答
  •  长情又很酷
    2021-02-04 23:06

    Ok try following code instead yours and see what happens inside your try/catch block.

    TextView tv1; 
    TextView tv2; 
    TextView tv3; 
    Button convert; 
    String publicKeyFilename = null; 
    String privateKeyFilename = null; 
    String ST = "Ebenezersawesome"; 
    
    @Override public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        Log.d("Ebz", "Made it to onCreate"); 
        tv1 = (TextView) findViewById(R.id.tv1); 
        tv2 = (TextView) findViewById(R.id.tv2); 
        tv3 = (TextView) findViewById(R.id.tv3); 
        convert = (Button) findViewById(R.id.button1); 
    
        try { 
                    byte[]plainText = "ST".getBytes("UTF8"); 
                    Log.d("Ebz", "made it to Try Block"); 
                    KeyPairGenerator keyGen = 
                        KeyPairGenerator.getInstance("RSA"); 
                    keyGen.initialize(4096); 
                    KeyPair key = keyGen.generateKeyPair(); 
                    Cipher cipher = 
                        Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
                    tv3.setText(cipher.getProvider().getInfo().toString()); 
                    //tv3.setText(ST); 
                    cipher.init(Cipher.ENCRYPT_MODE, key.getPublic()); 
                    byte[]cipherText = cipher.doFinal(plainText); 
                    tv1.setText(new String(cipherText, "UTF8").toString()); 
                    //tv1.setText(ST); 
                    byte[]newPlainText = cipher.doFinal(cipherText); 
                    tv2.setText(new String(newPlainText, "UTF8").toString()); 
                    //tv2.setText(ST); 
                } catch(Exception e) { 
                    System.out.println("error"); 
                }
        // tv2.setText(ST); 
        convert.setOnClickListener(new OnClickListener() { 
            public void onClick(View v) { 
                Log.d("Ebz", "Made it to onCreate");  
            } 
        }); 
    }
    

    0 讨论(0)
提交回复
热议问题