List of usage examples for org.bouncycastle.openpgp.operator PBEKeyEncryptionMethodGenerator getKey
public byte[] getKey(int encAlgorithm) throws PGPException
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 {//from w ww .j a v a2s . com 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.test.pgp.PGPWriter.java
License:Open Source License
public void open(int algorithm, PGPKeyEncryptionMethodGenerator... methods) throws IOException, PGPException, IllegalStateException { if (methods.length == 0) throw new IllegalStateException("no encryption methods specified"); this.algorithm = algorithm; // ******************************************************************* // public key packet // ******************************************************************* // TODO this condition untested and perhaps not helpful, review (PBE - password based encryption) if ((methods.length == 1) && (methods[0] instanceof PBEKeyEncryptionMethodGenerator)) { PBEKeyEncryptionMethodGenerator m = (PBEKeyEncryptionMethodGenerator) methods[0]; this.key = m.getKey(algorithm); this.writePacket(m.generate(algorithm, null)); } else {/*from w w w . j a v a 2s . com*/ this.key = org.bouncycastle.openpgp.PGPUtil.makeRandomKey(algorithm, rand); byte[] sessionInfo = this.createSessionInfo(); for (int i = 0; i < methods.length; i++) { PGPKeyEncryptionMethodGenerator m = (PGPKeyEncryptionMethodGenerator) methods[i]; this.writePacket(m.generate(algorithm, sessionInfo)); } } int packet1 = this.out.writerIndex(); System.out.println("packet 1: " + packet1 + " final bytes: " + HexUtil.bufferToHex(this.out.array(), this.out.arrayOffset() + packet1 - 5, 5)); // ******************************************************************* // encrypt packet, add IV to // ******************************************************************* try { 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, this.key), new IvParameterSpec(iv)); // // we have to add block size + 2 for the generated IV and + 1 + 22 if integrity protected // this.ensureBuffer(this.cipher.getBlockSize() + 2 + 1 + 22); this.startPartialPacket(PacketTags.SYM_ENC_INTEGRITY_PRO); //, this.cipher.getBlockSize() + 2 + 1 + 22); this.out.writeByte(1); // version number byte[] inLineIv = new byte[this.cipher.getBlockSize() + 2]; this.rand.nextBytes(inLineIv); inLineIv[inLineIv.length - 1] = inLineIv[inLineIv.length - 3]; inLineIv[inLineIv.length - 2] = inLineIv[inLineIv.length - 4]; // TODO byte[] any = this.cipher.update(inLineIv); if (any != null) this.out.writeBytes(any); // we may include this in digest, TODO review } catch (InvalidKeyException e) { throw new PGPException("invalid key: " + e.getMessage(), e); } catch (InvalidAlgorithmParameterException e) { throw new PGPException("imvalid algorithm parameter: " + e.getMessage(), e); } catch (GeneralSecurityException e) { throw new PGPException("cannot create cipher: " + e.getMessage(), e); } int packet2 = this.out.writerIndex(); System.out.println("packet 2: first bytes: " + HexUtil.bufferToHex(this.out.array(), this.out.arrayOffset() + packet1, 25)); System.out.println("packet 2: " + packet2 + " final bytes: " + HexUtil.bufferToHex(this.out.array(), this.out.arrayOffset() + packet2 - 5, 5)); // ******************************************************************* // TODO compress packet, if any // ******************************************************************* int packet3 = this.out.writerIndex(); //System.out.println("packet 3: first bytes: " + HexUtil.bufferToHex(this.out.array(), this.out.arrayOffset() + packet2, 25)); //System.out.println("packet 3: " + packet3 + " final bytes: " + HexUtil.bufferToHex(this.out.array(), this.out.arrayOffset() + packet3 - 5, 5)); // ******************************************************************* // literal packet start // ******************************************************************* this.startPartialPacket(PacketTags.LITERAL_DATA); byte[] encName = Utf8Encoder.encode(this.fileName); // TODO don't hard code int len = 1 + 1 + encName.length + 4 + 99; // type + name length + name + mod time + file content //out.writeByte(len); this.writeNewPacketLength(len); out.writeByte(PGPLiteralData.BINARY); out.writeByte((byte) encName.length); out.writeBytes(encName); long modDate = this.modificationTime / 1000; out.writeByte((byte) (modDate >> 24)); out.writeByte((byte) (modDate >> 16)); out.writeByte((byte) (modDate >> 8)); out.writeByte((byte) (modDate)); int packet4 = this.out.writerIndex(); System.out.println("packet 4: first bytes: " + HexUtil.bufferToHex(this.out.array(), this.out.arrayOffset() + packet3, 25)); System.out.println("packet 4: " + packet4 + " final bytes: " + HexUtil.bufferToHex(this.out.array(), this.out.arrayOffset() + packet4 - 5, 5)); // TODO new SHA1PGPDigestCalculator(); //if (digestCalc != null) // genOut = new TeeOutputStream(digestCalc.getOutputStream(), genOut); }