Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.crypto.engines;
  
  
this does your basic RSA algorithm.
 
 {
     private RSAKeyParameters key;
     private boolean          forEncryption;

    
initialise the RSA engine.

Parameters:
forEncryption true if we are encrypting, false otherwise.
param the necessary RSA key parameters.
 
     public void init(
         boolean          forEncryption,
         CipherParameters param)
     {
         if (param instanceof ParametersWithRandom)
         {
             ParametersWithRandom    rParam = (ParametersWithRandom)param;
 
              = (RSAKeyParameters)rParam.getParameters();
         }
         else
         {
              = (RSAKeyParameters)param;
         }
 
         this. = forEncryption;
     }

    
Return the maximum size for an input block to this engine. For RSA this is always one byte less than the key size on encryption, and the same length as the key size on decryption.

Returns:
maximum size for an input block.
 
     public int getInputBlockSize()
     {
         int     bitSize = .getModulus().bitLength();
 
         if ()
         {
             return (bitSize + 7) / 8 - 1;
         }
         else
         {
             return (bitSize + 7) / 8;
         }
     }

    
Return the maximum size for an output block to this engine. For RSA this is always one byte less than the key size on decryption, and the same length as the key size on encryption.

Returns:
maximum size for an output block.
 
     public int getOutputBlockSize()
     {
         int     bitSize = .getModulus().bitLength();
 
         if ()
         {
             return (bitSize + 7) / 8;
         }
         else
         {
             return (bitSize + 7) / 8 - 1;
         }
     }
 
     public BigInteger convertInput(
         byte[]  in,
         int     inOff,
         int     inLen)
     {
         if (inLen > (getInputBlockSize() + 1))
         {
             throw new DataLengthException("input too large for RSA cipher.");
         }
         else if (inLen == (getInputBlockSize() + 1) && !)
         {
             throw new DataLengthException("input too large for RSA cipher.");
         }
 
         byte[]  block;
        if (inOff != 0 || inLen != in.length)
        {
            block = new byte[inLen];
            System.arraycopy(ininOffblock, 0, inLen);
        }
        else
        {
            block = in;
        }
        BigInteger res = new BigInteger(1, block);
        if (res.compareTo(.getModulus()) >= 0)
        {
            throw new DataLengthException("input too large for RSA cipher.");
        }
        return res;
    }
    public byte[] convertOutput(
        BigInteger result)
    {
        byte[]      output = result.toByteArray();
        if ()
        {
            if (output[0] == 0 && output.length > getOutputBlockSize())        // have ended up with an extra zero byte, copy down.
            {
                byte[]  tmp = new byte[output.length - 1];
                System.arraycopy(output, 1, tmp, 0, tmp.length);
                return tmp;
            }
            if (output.length < getOutputBlockSize())     // have ended up with less bytes than normal, lengthen
            {
                byte[]  tmp = new byte[getOutputBlockSize()];
                System.arraycopy(output, 0, tmptmp.length - output.lengthoutput.length);
                return tmp;
            }
        }
        else
        {
            if (output[0] == 0)        // have ended up with an extra zero byte, copy down.
            {
                byte[]  tmp = new byte[output.length - 1];
                System.arraycopy(output, 1, tmp, 0, tmp.length);
                return tmp;
            }
        }
        return output;
    }
    public BigInteger processBlock(BigInteger input)
    {
        if ( instanceof RSAPrivateCrtKeyParameters)
        {
            //
            // we have the extra factors, use the Chinese Remainder Theorem - the author
            // wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for
            // advice regarding the expression of this.
            //
            RSAPrivateCrtKeyParameters crtKey = (RSAPrivateCrtKeyParameters);
            BigInteger p = crtKey.getP();
            BigInteger q = crtKey.getQ();
            BigInteger dP = crtKey.getDP();
            BigInteger dQ = crtKey.getDQ();
            BigInteger qInv = crtKey.getQInv();
            BigInteger mPmQhm;
            // mP = ((input mod p) ^ dP)) mod p
            mP = (input.remainder(p)).modPow(dPp);
            // mQ = ((input mod q) ^ dQ)) mod q
            mQ = (input.remainder(q)).modPow(dQq);
            // h = qInv * (mP - mQ) mod p
            h = mP.subtract(mQ);
            h = h.multiply(qInv);
            h = h.mod(p);               // mod (in Java) returns the positive residual
            // m = h * q + mQ
            m = h.multiply(q);
            m = m.add(mQ);
            return m;
        }
        else
        {
            return input.modPow(
                        .getExponent(), .getModulus());
        }
    }
New to GrepCode? Check out our FAQ X