Package org.bouncycastle.crypto

Examples of org.bouncycastle.crypto.AsymmetricBlockCipher


    public byte[] generateRawSignature(AsymmetricKeyParameter privateKey, byte[] md5AndSha1)
        throws CryptoException
    {

        AsymmetricBlockCipher engine = createRSAImpl();
        engine.init(true, new ParametersWithRandom(privateKey, this.context.getSecureRandom()));
        return engine.processBlock(md5AndSha1, 0, md5AndSha1.length);
    }
View Full Code Here


    public boolean verifyRawSignature(byte[] sigBytes, AsymmetricKeyParameter publicKey, byte[] md5AndSha1)
        throws CryptoException
    {

        AsymmetricBlockCipher engine = createRSAImpl();
        engine.init(false, publicKey);
        byte[] signed = engine.processBlock(sigBytes, 0, sigBytes.length);
        return Arrays.constantTimeAreEqual(signed, md5AndSha1);
    }
View Full Code Here

    return this.mod;
  }

  private String generate() throws Exception{
    RSAKeyParameters pubParameters = new RSAKeyParameters(false, mod, exp);
    AsymmetricBlockCipher eng = new PKCS1Encoding(new RSAEngine());
    eng.init(true, pubParameters);
    byte[] data = Base16.fromHexString(this.sessionKey);
    data = eng.processBlock(data, 0, data.length);
    return Base16.toHexString(data);
  }
View Full Code Here

                                                        privStruct.getPrime2(),
                                                        privStruct.getExponent1(),
                                                        privStruct.getExponent2(),
                                                        privStruct.getCoefficient());
   
            AsymmetricBlockCipher   cipher = new OAEPEncoding(new RSAEngine());
   
            cipher.init(true, new ParametersWithRandom(pubParameters, new Rand()));
   
            byte[]  out;
            byte[]  input = new byte[]
                { (byte)0x54, (byte)0x85, (byte)0x9b, (byte)0x34, (byte)0x2c, (byte)0x49, (byte)0xea, (byte)0x2a };
   
            try
            {
                out = cipher.processBlock(input, 0, input.length);
            }
            catch (Exception e)
            {
                return new SimpleTestResult(false, getName() + ": exception - " + e.toString());
            }
   
   
            for (int i = 0; i != output.length; i++)
            {
                if (out[i] != output[i])
                {
                    return new SimpleTestResult(false, getName() + ": failed encryption");
                }
            }
   
            cipher.init(false, privParameters);
            try
            {
                out = cipher.processBlock(output, 0, output.length);
            }
            catch (Exception e)
            {
                return new SimpleTestResult(false, getName() + ": exception - " + e.toString());
            }
View Full Code Here

        byte[]              data = Hex.decode(edgeInput);

        //
        // RAW
        //
        AsymmetricBlockCipher   eng = new RSAEngine();

        eng.init(true, pubParameters);

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        eng.init(false, privParameters);

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        if (!edgeInput.equals(new String(Hex.encode(data))))
        {
            return new SimpleTestResult(false, "RSA: failed RAW edge Test");
        }

        data = Hex.decode(input);

        eng.init(true, pubParameters);

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        eng.init(false, privParameters);

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        if (!input.equals(new String(Hex.encode(data))))
        {
            return new SimpleTestResult(false, "RSA: failed RAW Test");
        }

        //
        // PKCS1 - public encrypt, private decrypt
        //
        eng = new PKCS1Encoding(eng);

        eng.init(true, pubParameters);

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        eng.init(false, privParameters);

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        if (!input.equals(new String(Hex.encode(data))))
        {
            return new SimpleTestResult(false, "RSA: failed PKCS1 public/private Test");
        }

        //
        // PKCS1 - private encrypt, public decrypt
        //
        eng = new PKCS1Encoding(((PKCS1Encoding)eng).getUnderlyingCipher());

        eng.init(true, privParameters);

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        eng.init(false, pubParameters);

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        if (!input.equals(new String(Hex.encode(data))))
        {
            return new SimpleTestResult(false, "RSA: failed PKCS1 private/public Test");
        }

        //
        // OAEP - public encrypt, private decrypt
        //
        eng = new OAEPEncoding(((PKCS1Encoding)eng).getUnderlyingCipher());

        eng.init(true, pubParameters);

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        eng.init(false, privParameters);

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        if (!input.equals(new String(Hex.encode(data))))
        {
            return new SimpleTestResult(false, "RSA: failed OAEP Test");
        }

        RSAKeyPairGenerator  pGen = new RSAKeyPairGenerator();
        RSAKeyGenerationParameters  genParam = new RSAKeyGenerationParameters(
                                            BigInteger.valueOf(0x11), new SecureRandom(), 768, 25);

        pGen.init(genParam);

        AsymmetricCipherKeyPair  pair = pGen.generateKeyPair();
       
        eng = new RSAEngine();

        if (((RSAKeyParameters)pair.getPublic()).getModulus().bitLength() < 762)
        {
            return new SimpleTestResult(false, "RSA: failed key generation (768) length test");
        }

        eng.init(true, pair.getPublic());

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        eng.init(false, pair.getPrivate());

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        if (!input.equals(new String(Hex.encode(data))))
        {
            return new SimpleTestResult(false, "RSA: failed key generation (768) Test");
        }

        genParam = new RSAKeyGenerationParameters(BigInteger.valueOf(0x11), new SecureRandom(), 1024, 25);

        pGen.init(genParam);
        pair = pGen.generateKeyPair();

        eng.init(true, pair.getPublic());

        if (((RSAKeyParameters)pair.getPublic()).getModulus().bitLength() < 1018)
        {
            return new SimpleTestResult(false, "RSA: failed key generation (1024) length test");
        }

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }

        eng.init(false, pair.getPrivate());

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "RSA: failed - exception " + e.toString());
        }
View Full Code Here

    protected byte[] encryptSessionInfo(PGPPublicKey pubKey, byte[] sessionInfo)
        throws PGPException
    {
        try
        {
            AsymmetricBlockCipher c = BcImplProvider.createPublicKeyCipher(pubKey.getAlgorithm());

            AsymmetricKeyParameter key = keyConverter.getPublicKey(pubKey);

            if (random == null)
            {
                random = new SecureRandom();
            }

            c.init(true, new ParametersWithRandom(key, random));

            return c.processBlock(sessionInfo, 0, sessionInfo.length);
        }
        catch (InvalidCipherTextException e)
        {
            throw new PGPException("exception encrypting session info: " + e.getMessage(), e);
        }
View Full Code Here

    public byte[] recoverSessionData(int keyAlgorithm, BigInteger[] secKeyData)
        throws PGPException
    {
        try
        {
            AsymmetricBlockCipher c = BcImplProvider.createPublicKeyCipher(keyAlgorithm);

            AsymmetricKeyParameter key = keyConverter.getPrivateKey(privKey);

            BufferedAsymmetricBlockCipher c1 = new BufferedAsymmetricBlockCipher(c);
View Full Code Here

    }

    static AsymmetricBlockCipher createPublicKeyCipher(int encAlgorithm)
        throws PGPException
    {
        AsymmetricBlockCipher c;

        switch (encAlgorithm)
        {
        case PGPPublicKey.RSA_ENCRYPT:
        case PGPPublicKey.RSA_GENERAL:
View Full Code Here

                    //
                   
                }
            }
            
            AsymmetricBlockCipher c = new PKCS1Encoding(new ElGamalEngine());

            c.init(true, pKey);
           
            byte[]  in = "hello world".getBytes();

            byte[]  out = c.processBlock(in, 0, in.length);
           
            pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass));
           
            c.init(false, keyConverter.getPrivateKey(pgpPrivKey));
           
            out = c.processBlock(out, 0, out.length);
           
            if (!areEqual(in, out))
            {
                fail("decryption failed.");
            }
View Full Code Here

      session.disconnect("Encryption was not requested");
    } else {
      int keySize = VanillaConfiguration.ENCRYPT_KEY_SIZE.getInt();
      String keyAlgorithm = VanillaConfiguration.ENCRYPT_KEY_ALGORITHM.getString();
      String keyPadding = VanillaConfiguration.ENCRYPT_KEY_PADDING.getString();
      AsymmetricBlockCipher cipher = SecurityHandler.getInstance().getAsymmetricCipher(keyAlgorithm, keyPadding);

      AsymmetricCipherKeyPair pair = SecurityHandler.getInstance().getKeyPair(keySize, keyAlgorithm);
      cipher.init(SecurityHandler.DECRYPT_MODE, pair.getPrivate());
      final byte[] initialVector = SecurityHandler.getInstance().processAll(cipher, message.getSecretArray());

      String sessionId = session.getDataMap().get(VanillaProtocol.SESSION_ID);

      final byte[] validateToken = SecurityHandler.getInstance().processAll(cipher, message.getVerifyTokenArray());
View Full Code Here

TOP

Related Classes of org.bouncycastle.crypto.AsymmetricBlockCipher

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.