Examples of BlockCipher


Examples of freenet.crypt.BlockCipher

      // create new
      byte[] newsalt = new byte[0x10];
      random.nextBytes(newsalt);
      byte[] diskSalt = newsalt;
      if(masterKey != null) {
        BlockCipher cipher;
        try {
          cipher = new Rijndael(256, 128);
        } catch (UnsupportedCipherException e) {
          throw new Error("Impossible: no Rijndael(256,128): "+e, e);
        }
        cipher.initialize(masterKey);
        diskSalt = new byte[0x10];
        cipher.encipher(newsalt, diskSalt);
        if(logDEBUG)
          Logger.debug(this, "Encrypting with "+HexUtil.bytesToHex(newsalt)+" from "+HexUtil.bytesToHex(diskSalt));
      }
      cipherManager = new CipherManager(newsalt, diskSalt);

      writeConfigFile();
      return true;
    } else {
      try {
        // try to load
        RandomAccessFile raf = new RandomAccessFile(configFile, "r");
        try {
          byte[] salt = new byte[0x10];
          raf.readFully(salt);

          byte[] diskSalt = salt;
          if(masterKey != null) {
            BlockCipher cipher;
            try {
              cipher = new Rijndael(256, 128);
            } catch (UnsupportedCipherException e) {
              throw new Error("Impossible: no Rijndael(256,128): "+e, e);
            }
            cipher.initialize(masterKey);
            salt = new byte[0x10];
            cipher.decipher(diskSalt, salt);
            if(logDEBUG)
              Logger.debug(this, "Encrypting (new) with "+HexUtil.bytesToHex(salt)+" from "+HexUtil.bytesToHex(diskSalt));
          }

          cipherManager = new CipherManager(salt, diskSalt);
View Full Code Here

Examples of org.bouncycastle.crypto.BlockCipher

                buf[i] ^= block[i - blockSize];
            }

            if (cipher instanceof CBCBlockCipher)
            {
                BlockCipher c = ((CBCBlockCipher)cipher).getUnderlyingCipher();

                c.processBlock(buf, blockSize, out, outOff);
            }
            else
            {
                cipher.processBlock(buf, blockSize, out, outOff);
            }

            System.arraycopy(block, 0, out, outOff + blockSize, len);
        }
        else
        {
            byte[]  lastBlock = new byte[blockSize];

            if (cipher instanceof CBCBlockCipher)
            {
                BlockCipher c = ((CBCBlockCipher)cipher).getUnderlyingCipher();

                c.processBlock(buf, 0, block, 0);
            }
            else
            {
                cipher.processBlock(buf, 0, block, 0);
            }
View Full Code Here

Examples of org.bouncycastle.crypto.BlockCipher

        if (keyParam == null)
        {
            throw new IllegalStateException("CCM cipher unitialized.");
        }
       
        BlockCipher ctrCipher = new SICBlockCipher(cipher);
        byte[] iv = new byte[blockSize];
        byte[] out;

        iv[0] = (byte)(((15 - nonce.length) - 1) & 0x7);
       
        System.arraycopy(nonce, 0, iv, 1, nonce.length);
       
        ctrCipher.init(forEncryption, new ParametersWithIV(keyParam, iv));
       
        if (forEncryption)
        {
            int index = inOff;
            int outOff = 0;
           
            out = new byte[inLen + macSize];
           
            calculateMac(in, inOff, inLen, macBlock);
           
            ctrCipher.processBlock(macBlock, 0, macBlock, 0);   // S0
           
            while (index < inLen - blockSize)                   // S1...
            {
                ctrCipher.processBlock(in, index, out, outOff);
                outOff += blockSize;
                index += blockSize;
            }
           
            byte[] block = new byte[blockSize];
           
            System.arraycopy(in, index, block, 0, inLen - index);
           
            ctrCipher.processBlock(block, 0, block, 0);
           
            System.arraycopy(block, 0, out, outOff, inLen - index);
           
            outOff += inLen - index;

            System.arraycopy(macBlock, 0, out, outOff, out.length - outOff);
        }
        else
        {
            int index = inOff;
            int outOff = 0;
           
            out = new byte[inLen - macSize];
           
            System.arraycopy(in, inOff + inLen - macSize, macBlock, 0, macSize);
           
            ctrCipher.processBlock(macBlock, 0, macBlock, 0);
           
            for (int i = macSize; i != macBlock.length; i++)
            {
                macBlock[i] = 0;
            }
           
            while (outOff < out.length - blockSize)
            {
                ctrCipher.processBlock(in, index, out, outOff);
                outOff += blockSize;
                index += blockSize;
            }
           
            byte[] block = new byte[blockSize];
           
            System.arraycopy(in, index, block, 0, out.length - outOff);
           
            ctrCipher.processBlock(block, 0, block, 0);
           
            System.arraycopy(block, 0, out, outOff, out.length - outOff);
           
            byte[] calculatedMacBlock = new byte[blockSize];
           
View Full Code Here

Examples of org.bouncycastle.crypto.BlockCipher

public class AESCipherCBCnoPad {
    private BlockCipher cbc;
   
    /** Creates a new instance of AESCipher */
    public AESCipherCBCnoPad(boolean forEncryption, byte[] key) {
        BlockCipher aes = new AESFastEngine();
        cbc = new CBCBlockCipher(aes);
        KeyParameter kp = new KeyParameter(key);
        cbc.init(forEncryption, kp);
    }
View Full Code Here

Examples of org.bouncycastle.crypto.BlockCipher

public class AESCipher {
    private PaddedBufferedBlockCipher bp;
   
    /** Creates a new instance of AESCipher */
    public AESCipher(boolean forEncryption, byte[] key, byte[] iv) {
        BlockCipher aes = new AESFastEngine();
        BlockCipher cbc = new CBCBlockCipher(aes);
        bp = new PaddedBufferedBlockCipher(cbc);
        KeyParameter kp = new KeyParameter(key);
        ParametersWithIV piv = new ParametersWithIV(kp, iv);
        bp.init(forEncryption, piv);
    }
View Full Code Here

Examples of org.bouncycastle.crypto.BlockCipher

public class AESCipherCBCnoPad {
    private BlockCipher cbc;
   
    /** Creates a new instance of AESCipher */
    public AESCipherCBCnoPad(boolean forEncryption, byte[] key) {
        BlockCipher aes = new AESFastEngine();
        cbc = new CBCBlockCipher(aes);
        KeyParameter kp = new KeyParameter(key);
        cbc.init(forEncryption, kp);
    }
View Full Code Here

Examples of org.bouncycastle.crypto.BlockCipher

                buf[i] ^= block[i - blockSize];
            }

            if (cipher instanceof CBCBlockCipher)
            {
                BlockCipher c = ((CBCBlockCipher)cipher).getUnderlyingCipher();

                c.processBlock(buf, blockSize, out, outOff);
            }
            else
            {
                cipher.processBlock(buf, blockSize, out, outOff);
            }

            System.arraycopy(block, 0, out, outOff + blockSize, len);
        }
        else
        {
            byte[]  lastBlock = new byte[blockSize];

            if (cipher instanceof CBCBlockCipher)
            {
                BlockCipher c = ((CBCBlockCipher)cipher).getUnderlyingCipher();

                c.processBlock(buf, 0, block, 0);
            }
            else
            {
                cipher.processBlock(buf, 0, block, 0);
            }
View Full Code Here

Examples of org.bouncycastle.crypto.BlockCipher

        byte[]          input = Hex.decode("4e6f7720");
        byte[]          out1 = new byte[4];
        byte[]          out2 = new byte[4];


        BlockCipher ofb = new OFBBlockCipher(new DESEngine(), 32);

        ofb.init(true, new ParametersWithIV(key, Hex.decode("1122334455667788")));

        ofb.processBlock(input, 0, out1, 0);

        ofb.init(false, new ParametersWithIV(key, Hex.decode("1122334455667788")));
        ofb.processBlock(out1, 0, out2, 0);

        if (!isEqualTo(out2, input))
        {
            return new SimpleTestResult(false, getName() + ": test 1 - in != out");
        }

        ofb.init(true, new ParametersWithIV(key, Hex.decode("11223344")));

        ofb.processBlock(input, 0, out1, 0);

        ofb.init(false, new ParametersWithIV(key, Hex.decode("0000000011223344")));
        ofb.processBlock(out1, 0, out2, 0);

        if (!isEqualTo(out2, input))
        {
            return new SimpleTestResult(false, getName() + ": test 2 - in != out");
        }

        BlockCipher cfb = new CFBBlockCipher(new DESEngine(), 32);

        cfb.init(true, new ParametersWithIV(key, Hex.decode("1122334455667788")));

        cfb.processBlock(input, 0, out1, 0);

        cfb.init(false, new ParametersWithIV(key, Hex.decode("1122334455667788")));
        cfb.processBlock(out1, 0, out2, 0);

        if (!isEqualTo(out2, input))
        {
            return new SimpleTestResult(false, getName() + ": test 3 - in != out");
        }

        cfb.init(true, new ParametersWithIV(key, Hex.decode("11223344")));

        cfb.processBlock(input, 0, out1, 0);

        cfb.init(false, new ParametersWithIV(key, Hex.decode("0000000011223344")));
        cfb.processBlock(out1, 0, out2, 0);

        if (!isEqualTo(out2, input))
        {
            return new SimpleTestResult(false, getName() + ": test 4 - in != out");
        }
View Full Code Here

Examples of org.bouncycastle.crypto.BlockCipher

                buf[i] ^= block[i - blockSize];
            }

            if (cipher instanceof CBCBlockCipher)
            {
                BlockCipher c = ((CBCBlockCipher)cipher).getUnderlyingCipher();

                c.processBlock(buf, blockSize, out, outOff);
            }
            else
            {
                cipher.processBlock(buf, blockSize, out, outOff);
            }

            System.arraycopy(block, 0, out, outOff + blockSize, len);
        }
        else
        {
            byte[]  lastBlock = new byte[blockSize];

            if (cipher instanceof CBCBlockCipher)
            {
                BlockCipher c = ((CBCBlockCipher)cipher).getUnderlyingCipher();

                c.processBlock(buf, 0, block, 0);
            }
            else
            {
                cipher.processBlock(buf, 0, block, 0);
            }
View Full Code Here

Examples of org.bouncycastle.crypto.BlockCipher

    }

    public TestResult perform()
    {
        KeyParameter        key = new KeyParameter(keyBytes);
        BlockCipher         cipher = new DESEngine();
        Mac                 mac = new CBCBlockCipherMac(cipher);

        //
        // standard DAC - zero IV
        //
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.