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


Java PGPLiteralDataGenerator类代码示例

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


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

示例1: encrypt

import org.spongycastle.openpgp.PGPLiteralDataGenerator; //导入依赖的package包/类
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,代码行数:26,代码来源:PGPPubkeyEncryptionUtil.java

示例2: encryptFile

import org.spongycastle.openpgp.PGPLiteralDataGenerator; //导入依赖的package包/类
public static boolean encryptFile(InputStream in, OutputStream out, File pubKeyFile,
                           boolean integrityCheck, Date fileDate,String filename){
    boolean status;
   try{
       PGPPublicKey encKey=MyPGPUtil.readPublicKey(new FileInputStream(pubKeyFile));
       Security.addProvider(new BouncyCastleProvider());

       PGPEncryptedDataGenerator cPk =
               new PGPEncryptedDataGenerator(
                       new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).
                               setWithIntegrityPacket(integrityCheck).
                               setSecureRandom(
                                       new SecureRandom()
                               )
               );

       cPk.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey));

       OutputStream                cOut = cPk.open(out, new byte[1 << 16]);

       PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(
               PGPCompressedData.UNCOMPRESSED);

       PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
       OutputStream pOut = lData.open(comData.open(cOut), PGPLiteralData.BINARY,filename ,fileDate, new byte[1 << 16]);
       PGPUtil.pipeDocumentFileContents(in,pOut, new byte[1 << 16].length);
       comData.close();

       cOut.close();
       Log.d(TAG,"file successfully encrypted");
       status=true;
   }catch (Exception ex){
       Log.d(TAG, "encryptFile: error in encrypting document file");
       ex.printStackTrace();
       status=false;
   }

    return status;
}
 
开发者ID:mosamabinomar,项目名称:RootPGPExplorer,代码行数:40,代码来源:DocumentFileEncryption.java

示例3: writeFileToLiteralData

import org.spongycastle.openpgp.PGPLiteralDataGenerator; //导入依赖的package包/类
/**
 * Write out the contents of the provided file as a literal data packet.
 *
 * @param out the stream to write the literal data to.
 * @param fileType the {@link PGPLiteralData} type to use for the file data.
 * @param file the file to write the contents of.
 *
 * @throws IOException if an error occurs reading the file or writing to the output stream.
 */
public static void writeFileToLiteralData(
    OutputStream    out,
    char            fileType,
    File            file)
    throws IOException
{
    PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
    OutputStream pOut = lData.open(out, fileType, file);
    pipeFileContents(file, pOut, 4096);
}
 
开发者ID:mosamabinomar,项目名称:RootPGPExplorer,代码行数:20,代码来源:PGPUtil.java

示例4: encrypt

import org.spongycastle.openpgp.PGPLiteralDataGenerator; //导入依赖的package包/类
public String encrypt(String msgText) throws IOException, PGPException {
    byte[] clearData = msgText.getBytes();
    PGPPublicKey encKey = getPublicKey(pkr);
    ByteArrayOutputStream encOut = new ByteArrayOutputStream();
    OutputStream out = new ArmoredOutputStream(encOut);
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedDataGenerator.ZIP);
    OutputStream cos = comData.open(bOut);
    PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
    OutputStream pOut = lData.open(cos, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, clearData.length, new Date());
    pOut.write(clearData);
    lData.close();
    comData.close();
    PGPEncryptedDataGenerator encGen =
            new PGPEncryptedDataGenerator(
                    new JcePGPDataEncryptorBuilder(PGPEncryptedData.AES_256).setWithIntegrityPacket(true).setSecureRandom(
                            new SecureRandom()).setProvider(PROVIDER));
    if (encKey != null) {
        encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey).setProvider(PROVIDER));
        byte[] bytes = bOut.toByteArray();
        OutputStream cOut = encGen.open(out, bytes.length);
        cOut.write(bytes);
        cOut.close();
    }
    out.close();
    return new String(encOut.toByteArray());
}
 
开发者ID:guardianproject,项目名称:proofmode,代码行数:28,代码来源:PgpUtils.java

示例5: encrypt

import org.spongycastle.openpgp.PGPLiteralDataGenerator; //导入依赖的package包/类
public static byte[] encrypt(byte[] payload) throws IOException, PGPException {
    ByteArrayOutputStream packetBaos = new ByteArrayOutputStream();
    PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
    OutputStream packetOut = lData.open(packetBaos, PGPLiteralData.BINARY, "picture.jpeg", payload.length, new Date());

    packetOut.write(payload);
    packetOut.close();
    byte[]packet =packetBaos.toByteArray();

    PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
            new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5)
                    .setWithIntegrityPacket(true)
                    .setSecureRandom(new SecureRandom())
                    .setProvider("SC"));

    encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(key).setProvider("SC"));

    ByteArrayOutputStream resultBaos = new ByteArrayOutputStream();

    OutputStream encOut = encGen.open(resultBaos, packet.length);
    encOut.write(packet);
    encOut.close();

    byte[] result = resultBaos.toByteArray();

    return result;
}
 
开发者ID:PassableShots,项目名称:PassableShots,代码行数:28,代码来源:EncryptionSystem.java

示例6: writeToLiteralData

import org.spongycastle.openpgp.PGPLiteralDataGenerator; //导入依赖的package包/类
public static void writeToLiteralData(InputStream in, ByteArrayOutputStream out, char fileType, String name) throws IOException {
	PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
       OutputStream pOut = lData.open(out, fileType, name, new Date(), new byte[1 << 16]);
       byte[] buf = new byte[1 << 16]; 
       
       int len;
       while ((len = in.read(buf)) > 0)
       {
           pOut.write(buf, 0, len);
       }
       in.close();
       pOut.close();
}
 
开发者ID:snuk182,项目名称:aceim,代码行数:14,代码来源:EncryptionUtils.java

示例7: encryptData

import org.spongycastle.openpgp.PGPLiteralDataGenerator; //导入依赖的package包/类
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,代码行数:73,代码来源:PGPCoder.java

示例8: encrypt

import org.spongycastle.openpgp.PGPLiteralDataGenerator; //导入依赖的package包/类
@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,代码行数:44,代码来源:EncryptionUtility.java


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