user1657555
user1657555

Reputation: 35

How to pass args when calling one method from another

I have written the following code to create a key pair, store the private key locally, and then read the private key from that file.

When I try to call the methods savePrivateKey(); and retrievePrivateKey(); from testData(View view) I get an error that says (String[]) cannot be applied to (). I want to be able to call both of the above mentioned functions in testData(View view);

public class EncryptionActivity extends ActionBarActivity {
private static final String TAG = EncryptionActivity.class.getSimpleName();

TextView textPublicKey;
TextView textPrivateKey;
Button buttonTest;
TextView privateKey;
Integer n;
String FILENAME = "privateKey";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_encryption);

    // output keys to screen
    textPrivateKey = (TextView)findViewById(R.id.textPrivateKey);
    textPrivateKey.setMovementMethod(new ScrollingMovementMethod());
    // textPublicKey = (TextView)findViewById(R.id.textPublicKey);
}

private void AsymmetricAlgorithmRSA() {
    // 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");
    }

    //textPublicKey.setText(String.valueOf(publicKey));
    //textPrivateKey.setText(String.valueOf(privateKey));

}

public void savePrivateKey(String[] args) throws FileNotFoundException {
    try {
        // store private key locally
        String string = String.valueOf(privateKey);

        FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.write(string.getBytes());
        fos.close();
    }
    catch (Exception e) {
        Log.e(TAG, "Error saving file.");
    }
}

public void retrievePrivateKey(String[] args) throws FileNotFoundException {
    try {
        FileInputStream fis = openFileInput(FILENAME);
        StringBuffer fileContent = new StringBuffer("");

        byte[] buffer = new byte[1024];

        while ((n = fis.read(buffer)) != -1) ;
        {
            fileContent.append(new String(buffer, 0, n));
        }

        textPrivateKey.setText(String.valueOf(fileContent));
    }
    catch(IOException e) {
            Log.e(TAG, "Error opening file.");
        }
}

public void testData(View view){

    AsymmetricAlgorithmRSA();
    savePrivateKey();
    retrievePrivateKey();
}

Upvotes: 1

Views: 95

Answers (2)

Mureinik
Mureinik

Reputation: 311088

Both savePrivateKey and retrievePrivateKey accept a String[], although they do not use them. Just drop these redundant parameter specifications and you should be fine:

public void savePrivateKey() throws FileNotFoundException {
    // code here...
}

public void retrievePrivateKey() throws FileNotFoundException {
    // code here...
}

Upvotes: 1

Shriram
Shriram

Reputation: 4411

savePrivateKey(); --> method which has no arguments. But you have implemented a method with arguments as String[] public void savePrivateKey(String[] args) throws FileNotFoundException.. Pass as String[] as argument or change the method signature.

Upvotes: 0

Related Questions