Android Open Source - asecrypto-goes-mobile-app Parameter Calculation Helper Task






From Project

Back to project page asecrypto-goes-mobile-app.

License

The source code is released under:

GNU General Public License

If you think the Android project asecrypto-goes-mobile-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa;
//from  w  w w  .  ja  va  2 s.  c  o m
import android.os.AsyncTask;
import android.util.Log;

import at.fhj.gaar.asecrypto.mobile.crypto.AseInteger;
import at.fhj.gaar.asecrypto.mobile.crypto.RSAHelper;
import at.fhj.gaar.asecrypto.mobile.ui.TaskFinishedCallable;

/**
 * Calculates n and phi(n) given to primes p and q.
 */
public class ParameterCalculationHelperTask extends AsyncTask<AseInteger, Void, ParameterCalculationHelperTask.CalculationResult> {

    private TaskFinishedCallable<CalculationResult> callable;

    public ParameterCalculationHelperTask(TaskFinishedCallable<CalculationResult> callable) {
        this.callable = callable;
    }

    @Override
    protected CalculationResult doInBackground(AseInteger... primes) {
        if (primes.length != 3) {
            throw new IllegalStateException("exactly three AseIntegers are expected (primes" +
                    " p and q and cipher e)");
        }

        AseInteger p = primes[0];
        if (p == null) {
            throw new IllegalStateException("AseInteger p is null");
        }

        AseInteger q = primes[1];
        if (q == null) {
            throw new IllegalStateException("AseInteger q is null");
        }

        AseInteger e = primes[2]; // null is ok

        // check primalities
        boolean pIsPrime = p.isProbablePrime(50);
        boolean qIsPrime = q.isProbablePrime(50);

        AseInteger phiOfN = RSAHelper.calculatePhiOfN(p, q);

        boolean gcdIsOne = true;
        boolean eTooBig = false;
        AseInteger decryptionNumber = null;


        if (e != null && pIsPrime && qIsPrime) {
            eTooBig = e.compareTo(phiOfN) > -1;

            AseInteger[] bezoutResult = e.getBezout(phiOfN);
            gcdIsOne = bezoutResult[2].compareTo(AseInteger.ONE) == 0;
            decryptionNumber = bezoutResult[0];
        }

        return new CalculationResult(RSAHelper.calculateN(p, q), phiOfN,
                pIsPrime, qIsPrime, decryptionNumber, gcdIsOne, eTooBig);
    }


    @Override
    protected void onPostExecute(CalculationResult calculationResult) {
        super.onPostExecute(calculationResult);

        callable.onAsyncTaskFinished(this, calculationResult);
    }

    public class CalculationResult {

        private AseInteger n;

        private AseInteger phiOfN;

        private boolean pPrime;

        private boolean qPrime;

        private AseInteger decryptionNumber;

        /**
         * Carries an error state.
         */
        private boolean gcdOfEAndPhiOfNEqualsOne;

        private boolean eTooBig;

        public CalculationResult(AseInteger n, AseInteger phiOfN, boolean pPrime, boolean qPrime,
                                 AseInteger decryptionNumber, boolean gcdOfEAndPhiOfNEqualsOne,
                                 boolean eTooBig) {
            this.n = n;
            this.phiOfN = phiOfN;
            this.pPrime = pPrime;
            this.qPrime = qPrime;
            this.decryptionNumber = decryptionNumber;
            this.gcdOfEAndPhiOfNEqualsOne = gcdOfEAndPhiOfNEqualsOne;
            this.eTooBig = eTooBig;
        }

        public AseInteger getN() {
            return n;
        }

        public AseInteger getPhiOfN() {
            return phiOfN;
        }

        public boolean isPPrime() {
            return pPrime;
        }

        public boolean isQPrime() {
            return qPrime;
        }

        public AseInteger getDecryptionNumber() {
            return decryptionNumber;
        }

        public boolean isGcdOfEAndPhiOfNEqualsOne() {
            return gcdOfEAndPhiOfNEqualsOne;
        }

        public boolean isETooBig() {
            return eTooBig;
        }
    }
}




Java Source Code List

at.fhj.gaar.asecrypto.mobile.crypto.AseInteger.java
at.fhj.gaar.asecrypto.mobile.crypto.RSAHelper.java
at.fhj.gaar.asecrypto.mobile.ui.FragmentFactory.java
at.fhj.gaar.asecrypto.mobile.ui.MainActivity.java
at.fhj.gaar.asecrypto.mobile.ui.SectionAttachable.java
at.fhj.gaar.asecrypto.mobile.ui.TaskFinishedCallable.java
at.fhj.gaar.asecrypto.mobile.ui.TaskIntermediateCallable.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.BaseFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.bezout.BezoutFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.bezout.BezoutIterativeTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.bezout.BezoutRecursiveTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.bezout.BezoutResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.carmichael.CarmichaelFinderTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.carmichael.CarmichaelGeneratorFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.carmichael.CarmichaelResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.euclid.EuclidFactorialTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.euclid.EuclidFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.euclid.EuclidIterativeTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.euclid.EuclidRecursiveTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.euclid.EuclidResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.exponentiation.ExponentiationResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.exponentiation.FastExponentiationFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.exponentiation.FastExponentiationTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.exponentiation.SlowExponentiationFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.exponentiation.SlowExponentiationTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.fermat.FermatProgress.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.fermat.FermatResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.fermat.FermatTaskArguments.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.fermat.FermatTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.fermat.FermatTestFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.millerrabin.MillerRabinArguments.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.millerrabin.MillerRabinProgress.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.millerrabin.MillerRabinResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.millerrabin.MillerRabinTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.millerrabin.MillerRabinTestFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.numbercounter.NumberCounterFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.numbercounter.NumberCounterTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.primitiveroots.FinderArguments.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.primitiveroots.PrimitiveRootFinderFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.primitiveroots.PrimitiveRootLookupTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.primitiveroots.PrimitiveRootResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.ParameterCalculationHelperTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSACalculationFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSADecryptionParameters.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSADecryptionTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSAEncryptionParameters.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSAEncryptionTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSAResult.java
at.fhj.gaar.asecrypto.mobile.ui.navigation.DrawerItemIdentifiers.java
at.fhj.gaar.asecrypto.mobile.ui.navigation.DrawerItem.java
at.fhj.gaar.asecrypto.mobile.ui.navigation.NavigationDrawerCallable.java
at.fhj.gaar.asecrypto.mobile.ui.navigation.NavigationDrawerFragment.java
at.fhj.gaar.asecrypto.mobile.util.NumberChoiceSelector.java
at.fhj.gaar.asecrypto.mobile.util.NumberHelper.java
at.fhj.gaar.asecrypto.mobile.util.StopWatch.java