android - 无法调用其他类的方法

时间:2013-05-11 15:56:02

标签: java android methods call

我现在正在为Android制作一条消息签名APP。

APP由BlowfishCipherActivity,SignedMailExample.java(它成功地在纯java编译器上工作)组成,依此类推。

但是,当我执行应用程序时,它无法从'SignedMailExample.java'调用signMail()方法。请参阅第'userSignedMessage = SignedMailExample.signMail(userOriginalMessage);'在BlowfishCipherActivity.java中。

厌倦的事情是ACtivity类使用'pkcs10request = PKCS10Generater.reqGen();'成功调用另一个类的方法。

请帮忙。

- BlowfishCipherActivity.java

package exam.blowfishcipher;

import java.io.*;

import android.app.Activity;
import android.content.*;
import android.os.Bundle;
import android.util.*;
import android.view.*;
import android.widget.*;


public class BlowfishCipherActivity extends Activity {
    /** Called when the activity is first created. */

    TextView final_result;
    //add 20130510
    static String userOriginalMessage;
    static String userSignedMessage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {

                /////initial message
                EditText init_message = (EditText)findViewById(R.id.init_message);
                String message = init_message.getText().toString();

                //password
                EditText init_password = (EditText)findViewById(R.id.init_password);
                String password = init_password.getText().toString();

                //
                BlowTest BlowfishConverter = new BlowTest();
                //PKCS10Generater PKCS10Gen = new PKCS10Generater();
                //BlowfishConverter.encrypt(message, password);

                final_result = (EditText)findViewById(R.id.final_result);
                final_result.setText(String.format("%s", BlowfishConverter.encrypt(message, password)));
                userOriginalMessage = final_result.getText().toString();


            }
        });


            Button btn1 = (Button)findViewById(R.id.btn1);
            btn1.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {

                    /////initial message
                    EditText init_message = (EditText)findViewById(R.id.init_message);
                    String message = init_message.getText().toString();

                    //password
                    EditText init_password = (EditText)findViewById(R.id.init_password);
                    String password = init_password.getText().toString();

                    //
                    BlowTest BlowfishConverter = new BlowTest();
                    //BlowfishConverter.encrypt(message, password);

                    final_result = (EditText)findViewById(R.id.final_result);
                    try {
                        final_result.setText(String.format("%s", BlowfishConverter.decrypt(message, password)));
                        //userOriginalMessage = final_result.getText().toString();
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block

                        e.printStackTrace();
                    }


                }

        });

            Button btn2 = (Button)findViewById(R.id.btn2);
            btn2.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {

                //PKCS10Generater PKCS10Gen = new PKCS10Generater();
                try {
                    Log.e("Position", "BeforePKIGeneration");
                    //add 20130429
                    String pkcs10request;
                    pkcs10request = PKCS10Generater.reqGen();
                    //
                    Toast.makeText(BlowfishCipherActivity.this, "PKI generation succeed", Toast.LENGTH_SHORT).show();

                    //add 20130429
                    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    clipboard.setText(pkcs10request);

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(BlowfishCipherActivity.this, "An error has occured", Toast.LENGTH_SHORT).show();
                }


                }


        });

            Button btn3 = (Button)findViewById(R.id.btn3);
            btn3.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {

                try {
                    Log.e("Position", "BeforeSignMessage");
                    //String plainMessage = "test";
                    //SignedMailExample.signMail(userOriginalMessage);
                    //add 20130429
                    Log.e("Position", userOriginalMessage);
                    //add 20130511
                    userSignedMessage = SignedMailExample.signMail(userOriginalMessage);
                    Log.e("Position", userSignedMessage);
                    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    clipboard.setText(userSignedMessage);
                    Toast.makeText(BlowfishCipherActivity.this, "Signing SMS has succeed", Toast.LENGTH_SHORT).show();

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(BlowfishCipherActivity.this, "An error has occured", Toast.LENGTH_SHORT).show();
                }


                }


        });
    }
}

- SignedMailExample.java

package exam.blowfishcipher;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.cert.*;
import java.util.Arrays;

import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.spongycastle.asn1.ASN1EncodableVector;
import org.spongycastle.asn1.cms.AttributeTable;
import org.spongycastle.asn1.smime.SMIMECapabilitiesAttribute;
import org.spongycastle.asn1.smime.SMIMECapability;
import org.spongycastle.asn1.smime.SMIMECapabilityVector;
import org.spongycastle.asn1.smime.SMIMEEncryptionKeyPreferenceAttribute;
import org.spongycastle.jce.PKCS10CertificationRequest;
import org.spongycastle.mail.smime.SMIMESigned;
import org.spongycastle.mail.smime.SMIMESignedGenerator;
import org.spongycastle.mail.smime.SMIMEUtil;
import org.spongycastle.openssl.PEMReader;
import org.spongycastle.openssl.PEMWriter;

import android.os.*;
import android.util.*;

/**
 * a simple example that creates and processes a signed mail message.
 */

public class SignedMailExample
    extends SignedDataProcessor
{
    //static String userCert;
    //static String userPrivate;

    //add 20130510
    //static String signedMail;

    public static MimeMultipart createMultipartWithSignature(
        PrivateKey      key,
        X509Certificate cert,
        CertStore       certsAndCRLs,
        MimeBodyPart    dataPart) 
        throws Exception
    {
        // create some smime capabilities in case someone wants to respond
        ASN1EncodableVector         signedAttrs = new ASN1EncodableVector();
        SMIMECapabilityVector       caps = new SMIMECapabilityVector();

        caps.addCapability(SMIMECapability.aES256_CBC);
        caps.addCapability(SMIMECapability.dES_EDE3_CBC);
        caps.addCapability(SMIMECapability.rC2_CBC, 128);

        signedAttrs.add(new SMIMECapabilitiesAttribute(caps));
        signedAttrs.add(new SMIMEEncryptionKeyPreferenceAttribute(SMIMEUtil.createIssuerAndSerialNumberFor(cert)));

        // set up the generator
        SMIMESignedGenerator gen = new SMIMESignedGenerator();
        gen.addSigner(key, cert, SMIMESignedGenerator.DIGEST_SHA256, new AttributeTable(signedAttrs), null);
        gen.addCertificatesAndCRLs(certsAndCRLs);

        // create the signed message
        return gen.generate(dataPart, "BC");
    }
    ////////////////////////////////////////////added by jeon
    public static void pemEncodeToFile(String filename, Object obj, char[] password) throws Exception{
        PEMWriter pw = new PEMWriter(new FileWriter(filename));
           if (password != null && password.length > 0) {
               pw.writeObject(obj, "DESEDE", password, new SecureRandom());
           } else {
               pw.writeObject(obj);
           }
           pw.flush();
           pw.close();
    }
    ////////////////////////////////////////////added by jeon

    ////////////////////////////////////////////added by jeon
    public static KeyStore createCredentials_modified()
            throws Exception
        {
            KeyStore store = KeyStore.getInstance("JKS");

            store.load(null, null);

            X500PrivateCredential    rootCredential = createRootCredential_modified();

            store.setCertificateEntry(rootCredential.getAlias(), rootCredential.getCertificate());
            store.setKeyEntry(rootCredential.getAlias(), rootCredential.getPrivateKey(), "password".toCharArray(), 
                    new Certificate[] { rootCredential.getCertificate(), rootCredential.getCertificate(), rootCredential.getCertificate() });

            return store;
        }

    public static X500PrivateCredential createRootCredential_modified()
            throws Exception
        {
            PEMReader rootPriva = new PEMReader(
                      new InputStreamReader(
                         new FileInputStream(Environment.getExternalStorageDirectory()+"/pkcs10priv.key"))); //modified 20130510

            PEMReader rootCerti = new PEMReader(
                      new InputStreamReader(
                         new FileInputStream(Environment.getExternalStorageDirectory()+"/userCert.cer")));

            KeyPair rootPrivate = (KeyPair)rootPriva.readObject();
            X509Certificate rootCert = (X509Certificate)rootCerti.readObject();
            //KeyPair         rootPair = generateRSAKeyPair();
            //X509Certificate rootCert = generateRootCert(rootPair);
            return new X500PrivateCredential(rootCert, rootPrivate.getPrivate(), "root"); //?????? root
        }
////////////////////////////////////////////added by jeon


   public static String signMail(String plainMessage) throws Exception{

    KeyStore credentials = createCredentials_modified();
    PrivateKey      key = (PrivateKey)credentials.getKey("root", "password".toCharArray());
    Certificate[]   chain = credentials.getCertificateChain("root");
    CertStore       certsAndCRLs = CertStore.getInstance("Collection",
               new CollectionCertStoreParameters(Arrays.asList(chain)), "BC");
    X509Certificate cert = (X509Certificate)chain[0];
    Log.e("Position", "position1");
       // create the message we want signed
       MimeBodyPart    dataPart = new MimeBodyPart();
       //
       dataPart.setText(plainMessage);
    Log.e("Position", "position2");       
       // create the signed message
       MimeMultipart multiPart = createMultipartWithSignature(key, cert, certsAndCRLs, dataPart);

       // create the mail message
       MimeMessage mail = Utils.createMimeMessage("my signed message", multiPart, multiPart.getContentType());
    Log.e("Position", "position3");     
       //added by JEON
       FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory()+"/SignedSMS.mail");
       String SingedSMS = mail.toString();
       fos.write(SingedSMS.getBytes());
       fos.close();


       ////
       return SingedSMS;
   }
////   

}

0 个答案:

没有答案