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


Java StreamCipher类代码示例

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


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

示例1: enChacha

import org.spongycastle.crypto.StreamCipher; //导入依赖的package包/类
public static byte[] enChacha(byte[] raw, byte[] key, byte[] iv) {
    boolean encrypt = true;

    CipherParameters cp = new KeyParameter(key);
    ParametersWithIV params = new ParametersWithIV(cp, iv);
    StreamCipher engine = new ChaChaEngine();
    //noinspection ConstantConditions
    engine.init(encrypt, params);

    byte[] ciphertext = new byte[raw.length];
    engine.processBytes(raw, 0, raw.length, ciphertext, 0);


    byte[] macKeyBytes = Arrays.copyOf(key, key.length);
    Poly1305KeyGenerator.clamp(macKeyBytes);
    KeyParameter macKey = new KeyParameter(macKeyBytes);  //initRecord(engine, encrypt, 0, iv);
    byte[] mac = calculateMAC(macKey, ciphertext, 0, ciphertext.length);

    byte[] res = new byte[ciphertext.length + mac.length];

    System.arraycopy(ciphertext, 0, res, 0, ciphertext.length);
    System.arraycopy(mac, 0, res, ciphertext.length, mac.length);

    KeyUtil.erase(ciphertext);
    KeyUtil.erase(mac);

    return res;
}
 
开发者ID:oversecio,项目名称:oversec_crypto,代码行数:29,代码来源:OversecChacha20Poly1305.java

示例2: deChacha

import org.spongycastle.crypto.StreamCipher; //导入依赖的package包/类
public static byte[] deChacha(byte[] ciphertext, byte[] key, byte[] iv) throws MacMismatchException {
    boolean encrypt = false;

    CipherParameters cp = new KeyParameter(key);
    ParametersWithIV params = new ParametersWithIV(cp, iv);
    StreamCipher engine = new ChaChaEngine();
    //noinspection ConstantConditions
    engine.init(encrypt, params);
    if (getPlaintextLimit(ciphertext.length) < 0) {
        throw new IllegalArgumentException();
    }

    byte[] macKeyBytes = Arrays.copyOf(key, key.length);
    Poly1305KeyGenerator.clamp(macKeyBytes);
    KeyParameter macKey = new KeyParameter(macKeyBytes);  //initRecord(engine, encrypt, 0, iv);

    int plaintextLength = ciphertext.length - 16;


    byte[] calculatedMAC = calculateMAC(macKey, ciphertext, 0, plaintextLength);
    byte[] receivedMAC = Arrays.copyOfRange(ciphertext, ciphertext.length - 16, ciphertext.length);

    if (!Arrays.constantTimeAreEqual(calculatedMAC, receivedMAC)) {
        throw new MacMismatchException();
    }

    byte[] output = new byte[plaintextLength];
    engine.processBytes(ciphertext, 0, plaintextLength, output, 0);

    KeyUtil.erase(calculatedMAC);
    KeyUtil.erase(receivedMAC);

    return output;
}
 
开发者ID:oversecio,项目名称:oversec_crypto,代码行数:35,代码来源:OversecChacha20Poly1305.java

示例3: generateRecordMACKey

import org.spongycastle.crypto.StreamCipher; //导入依赖的package包/类
private static KeyParameter generateRecordMACKey(StreamCipher cipher) {
    byte[] firstBlock = new byte[64];
    cipher.processBytes(firstBlock, 0, firstBlock.length, firstBlock, 0);

    KeyParameter macKey = new KeyParameter(firstBlock, 0, 32);
    Arrays.fill(firstBlock, (byte) 0);
    return macKey;
}
 
开发者ID:oversecio,项目名称:oversec_crypto,代码行数:9,代码来源:OversecChacha20Poly1305.java

示例4: initRecord

import org.spongycastle.crypto.StreamCipher; //导入依赖的package包/类
private static KeyParameter initRecord(StreamCipher cipher, boolean forEncryption, long seqNo, byte[] iv) {
    cipher.init(forEncryption, new ParametersWithIV(null, iv));
    return generateRecordMACKey(cipher);
}
 
开发者ID:oversecio,项目名称:oversec_crypto,代码行数:5,代码来源:OversecChacha20Poly1305.java


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