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


Java CPIMMessage类代码示例

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


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

示例1: encryptData

import org.kontalk.util.CPIMMessage; //导入依赖的package包/类
private String encryptData(String data, String mime) {
    List<PGPUtils.PGPCoderKey> receiverKeys = this.loadKeysOrNull();
    if (receiverKeys == null)
        return "";

    // secure the message against replay attacks using Message/CPIM
    String from = myKey.getUserId();
    String[] tos = receiverKeys.stream()
            .map(key -> key.userID)
            .toArray(String[]::new);
    CPIMMessage cpim = new CPIMMessage(from, tos, new Date(), mime, data);
    byte[] plainText;
    try {
        plainText = cpim.toByteArray();
    } catch (UnsupportedEncodingException ex) {
        LOGGER.log(Level.WARNING, "CPIM's charset not supported", ex);
        plainText = cpim.toString().getBytes();
    }

    return encrypt(plainText, receiverKeys);
}
 
开发者ID:kontalk,项目名称:desktopclient-java,代码行数:22,代码来源:Encryptor.java

示例2: encryptData

import org.kontalk.util.CPIMMessage; //导入依赖的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

示例3: decryptMessage

import org.kontalk.util.CPIMMessage; //导入依赖的package包/类
static boolean decryptMessage(DecryptMessage message, PersonalKey myKey) {
    if (!message.isEncrypted()) {
        LOGGER.warning("message not encrypted");
        return false;
    }

    // decrypt
    String encryptedContent = message.getEncryptedContent();
    if (encryptedContent.isEmpty()) {
        LOGGER.warning("no encrypted data in encrypted message");
    }
    byte[] encryptedData = org.kontalk.util.EncodingUtils.base64ToBytes(encryptedContent);

    // if sender signing key not found -> can decrypt but not verify
    PGPUtils.PGPCoderKey senderKey = Coder.contactkey(message.getContact()).orElse(null);
    InputStream encryptedIn = new ByteArrayInputStream(encryptedData);
    ByteArrayOutputStream plainOut = new ByteArrayOutputStream();
    DecryptionResult decResult;
    try {
        decResult = decryptAndVerify(encryptedIn,
                plainOut,
                myKey.getPrivateEncryptionKey(),
                senderKey != null ?
                        Optional.of(senderKey.signKey) :
                        Optional.empty());
    } catch (IOException | PGPException ex) {
        LOGGER.log(Level.WARNING, "can't decrypt message", ex);
        return false;
    }
    EnumSet<Coder.Error> allErrors = decResult.errors;
    message.setSigning(decResult.signing);

    // parse decrypted CPIM content
    String myUID = myKey.getUserId();
    String senderUID = senderKey != null ?
            senderKey.userID :
            null;
    String decryptedContent = EncodingUtils.getString(
            plainOut.toByteArray(),
            CPIMMessage.CHARSET);

    MessageContent content;
    // NOTE: we are not restricting the expected decrypted content to match the outer protocol
    // extension. E.g. somebody could wrap a CPIM message inside a XEP-0373 extension element
    // TODO ugly, but working
    if (decryptedContent.startsWith("<" + SignCryptElement.ELEMENT_NAME)) {
        content = parseSignCryptElement(decryptedContent, allErrors);
    } else {
        content = parseCPIMOrNull(decryptedContent, myUID, Optional.ofNullable(senderUID),
                allErrors);
    }

    // set errors
    message.setSecurityErrors(allErrors);

    if (content != null) {
        // everything went better than expected
        LOGGER.info("message decryption successful");
        message.setDecryptedContent(content);
        return true;
    } else {
        LOGGER.warning("message decryption failed");
        return false;
    }
}
 
开发者ID:kontalk,项目名称:desktopclient-java,代码行数:66,代码来源:Decryptor.java


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