当前位置: 首页>>代码示例>>Java>>正文


Java PGPCompressedData.ZIP属性代码示例

本文整理汇总了Java中org.spongycastle.openpgp.PGPCompressedData.ZIP属性的典型用法代码示例。如果您正苦于以下问题:Java PGPCompressedData.ZIP属性的具体用法?Java PGPCompressedData.ZIP怎么用?Java PGPCompressedData.ZIP使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在org.spongycastle.openpgp.PGPCompressedData的用法示例。


在下文中一共展示了PGPCompressedData.ZIP属性的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: encrypt

public String encrypt(String plainText) throws NoSuchAlgorithmException, IOException, PGPException {
    byte[] rawText = plainText.getBytes();
    //This needs, like, three metric fucktons of explanation and/or cleaning up
    ByteArrayOutputStream encOut = new ByteArrayOutputStream();
    OutputStream out = new ArmoredOutputStream(encOut);
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
    OutputStream cos = comData.open(bOut);
    PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
    OutputStream pOut = lData.open(cos, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, rawText.length, new Date());
    pOut.write(rawText);
    lData.close();
    comData.close();
    BcPGPDataEncryptorBuilder builder = new BcPGPDataEncryptorBuilder(PGPEncryptedData.AES_256);
    builder.setWithIntegrityPacket(true);
    builder.setSecureRandom(new SecureRandom());
    PGPEncryptedDataGenerator cpk = new PGPEncryptedDataGenerator(builder);
    cpk.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
    byte[] bytes = bOut.toByteArray();
    OutputStream cOut = cpk.open(out, bytes.length);
    cOut.write(bytes);
    cOut.close();
    out.close();
    return new String(encOut.toByteArray(), Charset.forName("UTF-8"));
}
 
开发者ID:TpmKranz,项目名称:SMS-Forward,代码行数:25,代码来源:PGPPubkeyEncryptionUtil.java

示例2: encryptData

private byte[] encryptData(String mime, CharSequence data)
        throws PGPException, IOException, SignatureException {

    String from = mKey.getUserId(mServer.getNetwork());
    String[] to = new String[mRecipients.length];
    for (int i = 0; i < to.length; i++)
        to[i] = PGP.getUserId(PGP.getMasterKey(mRecipients[i]), mServer.getNetwork());

    // secure the message against the most basic attacks using Message/CPIM
    CPIMMessage cpim = new CPIMMessage(from, to, new Date(), mime, data);
    byte[] plainText = cpim.toByteArray();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(plainText);

    // setup data encryptor & generator
    BcPGPDataEncryptorBuilder encryptor = new BcPGPDataEncryptorBuilder(PGPEncryptedData.AES_192);
    encryptor.setWithIntegrityPacket(true);
    encryptor.setSecureRandom(new SecureRandom());

    // add public key recipients
    PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(encryptor);
    for (PGPPublicKeyRing rcpt : mRecipients)
        encGen.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(PGP.getEncryptionKey(rcpt)));

    OutputStream encryptedOut = encGen.open(out, new byte[BUFFER_SIZE]);

    // setup compressed data generator
    PGPCompressedDataGenerator compGen = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
    OutputStream compressedOut = compGen.open(encryptedOut, new byte[BUFFER_SIZE]);

    // setup signature generator
    PGPSignatureGenerator sigGen = new PGPSignatureGenerator
            (new BcPGPContentSignerBuilder(mKey.getSignKeyPair()
                .getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA256));
    sigGen.init(PGPSignature.BINARY_DOCUMENT, mKey.getSignKeyPair().getPrivateKey());

    PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
    spGen.setSignerUserID(false, mKey.getUserId(mServer.getNetwork()));
    sigGen.setUnhashedSubpackets(spGen.generate());

    sigGen.generateOnePassVersion(false)
        .encode(compressedOut);

    // Initialize literal data generator
    PGPLiteralDataGenerator literalGen = new PGPLiteralDataGenerator();
    OutputStream literalOut = literalGen.open(
        compressedOut,
        PGPLiteralData.BINARY,
        "",
        new Date(),
        new byte[BUFFER_SIZE]);

    // read the "in" stream, compress, encrypt and write to the "out" stream
    // this must be done if clear data is bigger than the buffer size
    // but there are other ways to optimize...
    byte[] buf = new byte[BUFFER_SIZE];
    int len;
    while ((len = in.read(buf)) > 0) {
        literalOut.write(buf, 0, len);
        sigGen.update(buf, 0, len);
    }

    in.close();
    literalGen.close();
    // Generate the signature, compress, encrypt and write to the "out" stream
    sigGen.generate().encode(compressedOut);
    compGen.close();
    encGen.close();

    return out.toByteArray();
}
 
开发者ID:kontalk,项目名称:androidclient,代码行数:72,代码来源:PGPCoder.java

示例3: encrypt

@SuppressWarnings("deprecation")
public final static void encrypt(InputStream is, OutputStream os, byte[] publicKey) throws NoSuchProviderException, PGPException, IOException {
	
	BouncyCastleProvider bc = new BouncyCastleProvider();
	int bufferSize = 1 << 16;
	
	Security.addProvider(bc);
	
	OutputStream aos = new ArmoredOutputStream(os);
	
	PGPEncryptedDataGenerator edg = new PGPEncryptedDataGenerator(PGPEncryptedData.AES_256, true, new SecureRandom(), bc);
	edg.addMethod(KeyUtility.extractPublicKeyFromBytes(publicKey));
	OutputStream encOs = edg.open(aos, new byte[bufferSize]);
	
	PGPCompressedDataGenerator cdg = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
	OutputStream compOs = cdg.open(encOs);
	
	PGPLiteralDataGenerator ldg = new PGPLiteralDataGenerator();
	OutputStream litOs = ldg.open(compOs, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(System.currentTimeMillis()), new byte[bufferSize]);
	
	byte[] buf = new byte[bufferSize];
	
	int len;
	while((len = is.read(buf)) > 0)
		litOs.write(buf, 0, len);
	
	litOs.flush();
	litOs.close();
	ldg.close();
	
	compOs.flush();
	compOs.close();
	cdg.close();
	
	encOs.flush();
	encOs.close();
	edg.close();
	
	aos.close();
	
	is.close();
		
}
 
开发者ID:guardianproject,项目名称:CameraV,代码行数:43,代码来源:EncryptionUtility.java


注:本文中的org.spongycastle.openpgp.PGPCompressedData.ZIP属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。