0

I am trying to sign and verify text using DSA. I have written sample code to sign. But it doesn't work. Can you give a hand with this? This is a only a test because the idea is to sign data, record in a tag an when I read the tag I want to verify the data with the digital sign.

import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;

import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    private EditText texto;


    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        texto = (EditText) findViewById(R.id.texto);

    }

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

    public void test (View v){
        if(v.getId() == R.id.button1)

            firmar(texto.getText().toString());

    }



    public void firmar (String string){
    try{

        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA","SUN");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

        keyGen.initialize(1024, random);

        KeyPair pair = keyGen.generateKeyPair();
        PrivateKey priv = pair.getPrivate();
        PublicKey pub = pair.getPublic();


        /* Create a Signature object and initialize it with the private key */

        Signature dsa = Signature.getInstance("SHA1withDSA"); 

        dsa.initSign(priv);

        /* Update and sign the data */

        //String string = texto.getText().toString();
         byte [] bytes = string.getBytes();
         dsa.update(bytes);
         byte[] realSig = dsa.sign();


        AlertDialog.Builder newAlert= new AlertDialog.Builder(this);
        newAlert.setTitle("Firmado");
        newAlert.show();


        } catch (Exception e) {
            AlertDialog.Builder newAlert= new AlertDialog.Builder(this);
            newAlert.setTitle("No Firmado");
            newAlert.show();
        }
    }

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
mike
  • 1
  • 1
    What specifically is failing? Help others to help you by being explicit about the problem you are seeing. Is the code failing to compile? Does some method return unexpected results? – LogicG8 Jul 30 '13 at 19:25
  • The compilation is ok but when i put the text and i press the button for signing always appears the message in the exception ("no firmado") – mike Jul 30 '13 at 19:45

0 Answers0