Example usage for org.bouncycastle.bcpg SymmetricKeyAlgorithmTags NULL

List of usage examples for org.bouncycastle.bcpg SymmetricKeyAlgorithmTags NULL

Introduction

In this page you can find the example usage for org.bouncycastle.bcpg SymmetricKeyAlgorithmTags NULL.

Prototype

int NULL

To view the source code for org.bouncycastle.bcpg SymmetricKeyAlgorithmTags NULL.

Click Source Link

Usage

From source file:genkeys.java

License:Open Source License

private static String symmetricCipherName(int algorithm) throws PGPException {
    switch (algorithm) {
    case SymmetricKeyAlgorithmTags.NULL:
        return null;
    case SymmetricKeyAlgorithmTags.TRIPLE_DES:
        return "DESEDE";
    case SymmetricKeyAlgorithmTags.IDEA:
        return "IDEA";
    case SymmetricKeyAlgorithmTags.CAST5:
        return "CAST5";
    case SymmetricKeyAlgorithmTags.BLOWFISH:
        return "Blowfish";
    case SymmetricKeyAlgorithmTags.SAFER:
        return "SAFER";
    case SymmetricKeyAlgorithmTags.DES:
        return "DES";
    case SymmetricKeyAlgorithmTags.AES_128:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_192:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_256:
        return "AES";
    case SymmetricKeyAlgorithmTags.TWOFISH:
        return "Twofish";
    default://from w  ww  .  ja v  a  2s  .  co m
        throw new PGPException("unknown symmetric algorithm: " + algorithm);
    }
}

From source file:divconq.pgp.EncryptedFileStream.java

License:Open Source License

protected void writeDataInternal(byte[] bytes, int offset, int len) {
    if (this.algorithm != SymmetricKeyAlgorithmTags.NULL) {
        // hash the unprocessed bytes
        this.digest.update(bytes, offset, len);

        // TODO add debugging support by capturing bytes before compression

        // TODO add compression support
        // if compression flag is on, then send data through compressor

        // encrypt the data
        byte[] cout = this.cipher.update(bytes, offset, len);

        if (cout == null)
            return;

        int cpos = 0;

        // fill present buffer as much as possible
        if (this.out.writableBytes() > 0) {
            int clen = Math.min(cout.length, this.out.writableBytes());
            this.out.writeBytes(cout, cpos, clen);
            cpos += clen;//from w w w.  j a  va  2s .c om
        }

        int remaining = cout.length - cpos;

        if (remaining == 0)
            return;

        this.ensureBuffer(remaining);
        this.out.writeBytes(cout, cpos, remaining);
    } else {

        // TODO add debugging support by capturing bytes before compression

        // TODO add compression support
        // if compression flag is on, then send data through compressor

        // fill present buffer as much as possible
        if (this.out.writableBytes() > 0) {
            int clen = Math.min(len, this.out.writableBytes());
            this.out.writeBytes(bytes, offset, clen);
            offset += clen;
            len -= clen;
        }

        if (len == 0)
            return;

        this.ensureBuffer(len);
        this.out.writeBytes(bytes, offset, len);
    }
}

From source file:divconq.pgp.EncryptedFileStream.java

License:Open Source License

public void init() throws PGPException, IOException, InvalidKeyException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, NoSuchPaddingException {
    if (this.algorithm != SymmetricKeyAlgorithmTags.NULL) {
        // *******************************************************************
        // public key packet(s)
        // *******************************************************************

        if ((methods.size() == 1) && (methods.get(0) instanceof PBEKeyEncryptionMethodGenerator)) {
            PBEKeyEncryptionMethodGenerator method = (PBEKeyEncryptionMethodGenerator) methods.get(0);

            this.key = method.getKey(algorithm);

            ContainedPacket packet1 = method.generate(algorithm, null);

            byte[] encoded1 = packet1.getEncoded();

            this.ensureBuffer(encoded1.length);

            this.out.writeBytes(encoded1);
        } else {/* w  w  w .  j a va2s. c om*/
            this.key = org.bouncycastle.openpgp.PGPUtil.makeRandomKey(algorithm, rand);

            byte[] sessionInfo = new byte[key.length + 3];

            // add algorithm
            sessionInfo[0] = (byte) algorithm;

            // add key
            System.arraycopy(key, 0, sessionInfo, 1, key.length);

            // add checksum 
            int check = 0;

            for (int i = 1; i != sessionInfo.length - 2; i++)
                check += sessionInfo[i] & 0xff;

            sessionInfo[sessionInfo.length - 2] = (byte) (check >> 8);
            sessionInfo[sessionInfo.length - 1] = (byte) (check);

            for (PGPKeyEncryptionMethodGenerator method : methods) {
                ContainedPacket packet1 = method.generate(algorithm, sessionInfo);

                byte[] encoded1 = packet1.getEncoded();

                this.ensureBuffer(encoded1.length);

                this.out.writeBytes(encoded1);
            }
        }

        // *******************************************************************
        // encrypt packet, add IV to encryption though
        // *******************************************************************

        this.ensureBuffer(3);

        this.out.writeByte(0xC0 | PacketTags.SYM_ENC_INTEGRITY_PRO);
        this.out.writeByte(0); // unknown size
        this.out.writeByte(1); // version number

        // ******************** start encryption **********************

        String cName = PGPUtil.getSymmetricCipherName(algorithm) + "/CFB/NoPadding";

        DefaultJcaJceHelper helper = new DefaultJcaJceHelper();

        this.cipher = helper.createCipher(cName);

        byte[] iv = new byte[this.cipher.getBlockSize()];

        this.cipher.init(Cipher.ENCRYPT_MODE, PGPUtil.makeSymmetricKey(algorithm, key),
                new IvParameterSpec(iv));

        this.digest = MessageDigest.getInstance("SHA-1");

        // --- encrypt checksum for encrypt packet, part of the encrypted output --- 

        byte[] inLineIv = new byte[this.cipher.getBlockSize() + 2];

        rand.nextBytes(inLineIv);

        inLineIv[inLineIv.length - 1] = inLineIv[inLineIv.length - 3];
        inLineIv[inLineIv.length - 2] = inLineIv[inLineIv.length - 4];

        this.writeDataInternal(inLineIv, 0, inLineIv.length);
    }

    // ******************* Optionally add Compression **************************

    // TODO set compressor 

    // ******************** Literal data packet ***********************

    this.ensureBuffer(1);
    this.writeDataInternal((byte) (0xC0 | PacketTags.LITERAL_DATA));
}

From source file:divconq.pgp.EncryptedFileStream.java

License:Open Source License

public void close() throws PGPException {
    if (this.isClosed)
        return;/* ww  w . j a v  a 2s. c om*/

    this.isClosed = true;

    if (this.packetbuf != null) {
        // flush data if any packet space is available
        this.writeData(new byte[0], 0, 0);

        this.packetsize = this.packetbuf.readableBytes();
        this.packetpos = 0;

        // even if zero, this is fine, we need a final packet
        this.writeDataPacketLength(this.packetsize);

        if (this.packetsize > 0)
            this.writeData(new byte[0], 0, 0);

        this.packetbuf.release();
        this.packetbuf = null;
    }

    if (this.algorithm != SymmetricKeyAlgorithmTags.NULL) {
        this.ensureBuffer(22);

        this.writeDataInternal((byte) (0xC0 | PacketTags.MOD_DETECTION_CODE));

        this.writeDataInternal((byte) 20); // length of SHA-1 is always 20 bytes

        this.writeDataInternal(this.digest.digest(), 0, 20);

        // TODO final compression, pass into doFinal below

        byte[] fcipher;

        try {
            fcipher = this.cipher.doFinal();
        } catch (Exception x) {
            throw new PGPException("Problem with PGP cipher", x);
        }

        this.ensureBuffer(fcipher.length);
        this.out.writeBytes(fcipher); // write raw
    } else {
        // TODO final compression, if any
    }

    this.readyBuffers.add(this.out);
    this.out = null;
}

From source file:divconq.pgp.PGPUtil.java

License:Open Source License

public static String getSymmetricCipherName(int algorithm) {
    switch (algorithm) {
    case SymmetricKeyAlgorithmTags.NULL:
        return null;
    case SymmetricKeyAlgorithmTags.TRIPLE_DES:
        return "DESEDE";
    case SymmetricKeyAlgorithmTags.IDEA:
        return "IDEA";
    case SymmetricKeyAlgorithmTags.CAST5:
        return "CAST5";
    case SymmetricKeyAlgorithmTags.BLOWFISH:
        return "Blowfish";
    case SymmetricKeyAlgorithmTags.SAFER:
        return "SAFER";
    case SymmetricKeyAlgorithmTags.DES:
        return "DES";
    case SymmetricKeyAlgorithmTags.AES_128:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_192:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_256:
        return "AES";
    case SymmetricKeyAlgorithmTags.CAMELLIA_128:
        return "Camellia";
    case SymmetricKeyAlgorithmTags.CAMELLIA_192:
        return "Camellia";
    case SymmetricKeyAlgorithmTags.CAMELLIA_256:
        return "Camellia";
    case SymmetricKeyAlgorithmTags.TWOFISH:
        return "Twofish";
    default:/*from   ww w.  j  a  va  2s .  c  om*/
        throw new IllegalArgumentException("unknown symmetric algorithm: " + algorithm);
    }
}

From source file:org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.java

License:Open Source License

/**
 * Returns true on right passphrase//from  w  w  w  .j  a  v  a  2 s .  c o  m
 */
public boolean unlock(final Passphrase passphrase) throws PgpGeneralException {
    // handle keys on OpenPGP cards like they were unlocked
    S2K s2k = mSecretKey.getS2K();
    if (s2k != null && s2k.getType() == S2K.GNU_DUMMY_S2K
            && s2k.getProtectionMode() == S2K.GNU_PROTECTION_MODE_DIVERT_TO_CARD) {
        mPrivateKeyState = PRIVATE_KEY_STATE_DIVERT_TO_CARD;
        return true;
    }

    // try to extract keys using the passphrase
    try {

        int keyEncryptionAlgorithm = mSecretKey.getKeyEncryptionAlgorithm();
        if (keyEncryptionAlgorithm == SymmetricKeyAlgorithmTags.NULL) {
            mPrivateKey = mSecretKey.extractPrivateKey(null);
            mPrivateKeyState = PRIVATE_KEY_STATE_UNLOCKED;
            return true;
        }

        byte[] sessionKey;
        sessionKey = passphrase.getCachedSessionKeyForParameters(keyEncryptionAlgorithm, s2k);
        if (sessionKey == null) {
            PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
                    .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(passphrase.getCharArray());
            // this operation is EXPENSIVE, so we cache its result in the passed Passphrase object!
            sessionKey = keyDecryptor.makeKeyFromPassPhrase(keyEncryptionAlgorithm, s2k);
            passphrase.addCachedSessionKeyForParameters(keyEncryptionAlgorithm, s2k, sessionKey);
        }

        PBESecretKeyDecryptor keyDecryptor = new SessionKeySecretKeyDecryptorBuilder()
                .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(sessionKey);
        mPrivateKey = mSecretKey.extractPrivateKey(keyDecryptor);
        mPrivateKeyState = PRIVATE_KEY_STATE_UNLOCKED;
    } catch (PGPException e) {
        return false;
    }
    if (mPrivateKey == null) {
        throw new PgpGeneralException("error extracting key");
    }
    return true;
}