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


Java PKCS7Padding类代码示例

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


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

示例1: decrypt

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
@Override
    public String decrypt(byte[] encrypted) {
//        Cipher cipher = null;
        String plain;
        try {
//            Security.addProvider(new BouncyCastlePQCProvider());
//            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastlePQCProvider());
//            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(encryptionKey, "AES"), new IvParameterSpec(iv));
//            plain = new String(cipher.doFinal(encrypted), "UTF-8");
            KeyParameter keyParam = new KeyParameter(encryptionKey);
            CipherParameters params = new ParametersWithIV(keyParam, iv);
            BlockCipherPadding padding = new PKCS7Padding();
            BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                    new CBCBlockCipher(new AESEngine()), padding);
            cipher.reset();
            cipher.init(false, params);
            byte[] buffer = new byte[cipher.getOutputSize(encrypted.length)];
            int len = cipher.processBytes(encrypted, 0, encrypted.length, buffer, 0);
            len += cipher.doFinal(buffer, len);
            byte[] out = Arrays.copyOfRange(buffer, 0, len);
            plain = new String(out, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException("decrypt error in SimpleAesManaged", e);
        }
        return plain;
    }
 
开发者ID:timerickson,项目名称:lastpass-java,代码行数:27,代码来源:SimpleAesManaged.java

示例2: EncryptAes256

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
private static byte[] EncryptAes256(byte[] data, byte[] encryptionKey)
{
    try {
        KeyParameter keyParam = new KeyParameter(encryptionKey);
        BlockCipherPadding padding = new PKCS7Padding();
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                new CBCBlockCipher(new AESEngine()), padding);
        cipher.reset();
        cipher.init(true, keyParam);
        byte[] buffer = new byte[cipher.getOutputSize(data.length)];
        int len = cipher.processBytes(data, 0, data.length, buffer, 0);
        len += cipher.doFinal(buffer, len);
        return Arrays.copyOfRange(buffer, 0, len);
    } catch (Exception e) {
        throw new RuntimeException("decrypt error in SimpleAesManaged", e);
    }
}
 
开发者ID:timerickson,项目名称:lastpass-java,代码行数:18,代码来源:ParserHelperTest.java

示例3: doCbc

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
private void doCbc(byte[] key, byte[] iv, byte[] pt, byte[] expected)
    throws Exception
{
    PaddedBufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(new SerpentEngine()), new PKCS7Padding());

    byte[] ct = new byte[expected.length];

    c.init(true, new ParametersWithIV(new KeyParameter(key), iv));

    int l = c.processBytes(pt, 0, pt.length, ct, 0);

    c.doFinal(ct, l);

    if (!Arrays.areEqual(expected, ct))
    {
        fail("CBC test failed");
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:19,代码来源:SerpentTest.java

示例4: wrap

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
public static byte[] wrap(byte[] key, byte[] data) {
  AESWrapEngine engine = new AESWrapEngine();
  KeyParameter params = new KeyParameter(key);
  engine.init(true, params);
  PKCS7Padding padding = new PKCS7Padding();
  byte[] unpadded = data;

  //
  // Add padding
  //

  byte[] padded = new byte[unpadded.length + (8 - unpadded.length % 8)];
  System.arraycopy(unpadded, 0, padded, 0, unpadded.length);
  padding.addPadding(padded, unpadded.length);

  //
  // Wrap
  //

  byte[] encrypted = engine.wrap(padded, 0, padded.length);

  return encrypted;
}
 
开发者ID:cityzendata,项目名称:warp10-platform,代码行数:24,代码来源:CryptoUtils.java

示例5: decrypt

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
/**
 * A password-based data decryption using a constant salt value "<b>constantSalt</b>"
 * @param cipher
 * @param password
 * @param salt
 * @param iterationCount
 * @return
 * @throws Exception
 */
public static byte[] decrypt(byte[] cipher, String password) throws Exception
{
    PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(new SHA256Digest());
    char[] passwordChars = password.toCharArray();
    final byte[] pkcs12PasswordBytes = PBEParametersGenerator.PKCS12PasswordToBytes(passwordChars);
    pGen.init(pkcs12PasswordBytes, constantSalt.getBytes(), iterations);
    CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine());
    ParametersWithIV aesCBCParams = (ParametersWithIV) pGen.generateDerivedParameters(256, 128);
    aesCBC.init(false, aesCBCParams);
    PaddedBufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding());
    byte[] plainTemp = new byte[aesCipher.getOutputSize(cipher.length)];
    int offset = aesCipher.processBytes(cipher, 0, cipher.length, plainTemp, 0);
    int last = aesCipher.doFinal(plainTemp, offset);
    final byte[] plain = new byte[offset + last];
    System.arraycopy(plainTemp, 0, plain, 0, plain.length);
    return plain;
}
 
开发者ID:giuseppeurso-eu,项目名称:java-security,代码行数:27,代码来源:PasswordBasedEncryption.java

示例6: operate

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
private static byte[] operate(Encryption encryptionAlgorithm, boolean doEncrypt, byte[] subject, byte[] key, byte[] iv) throws CryptoException
{
    // set up padded buffered cipher
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
            CipherBuilder.buildCipher(encryptionAlgorithm, doEncrypt, key, iv), new PKCS7Padding()
    );

    // init with key and if
    cipher.init(doEncrypt, new ParametersWithIV(new KeyParameter(key), iv));

    // construct output buffer
    byte[] output = new byte[cipher.getOutputSize(subject.length)];

    // process all da bytes
    int cursor = cipher.processBytes(subject, 0, subject.length, output, 0);

    // process the last bytes from the buffer
    cipher.doFinal(output, cursor);
    return output;
}
 
开发者ID:AstromechZA,项目名称:bunkr,代码行数:21,代码来源:SimpleBlockCipher.java

示例7: encryptedOutputStream

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
@Override
public OutputStream encryptedOutputStream(final Path path, final String password) throws IOException,
        EncryptionFailedException {
    try {
        final byte[] salt = generateSalt();
        final byte[] key = generateKey(password, salt);
        final byte[] iv = generateIV();
        final byte[] fileInitBlock = generateOutputInitBlock(salt, iv);

        final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                new CBCBlockCipher(new AESEngine()), new PKCS7Padding());

        final KeyParameter keyParam = new KeyParameter(key);
        final CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(true, params);

        final BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(path));
        out.write(fileInitBlock);

        return new CipherOutputStream(out, cipher);
    } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
        throw new EncryptionFailedException(e);
    }
}
 
开发者ID:flbaue,项目名称:jCryptTool,代码行数:25,代码来源:AesEncryptionService.java

示例8: decryptedInputStream

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
@Override
public InputStream decryptedInputStream(final Path path, final String password) throws IOException,
        DecryptionFailedException {
    try {
        InputStream in = new BufferedInputStream(Files.newInputStream(path));
        byte[] initBlock = readInitBlock(in);
        byte[] salt = extractSalt(initBlock);
        byte[] iv = extractIV(initBlock);
        byte[] key = generateKey(password, salt);

        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
                new PKCS7Padding());
        KeyParameter keyParam = new KeyParameter(key);
        CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(false, params);

        return new CipherInputStream(in, cipher);
    } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
        throw new DecryptionFailedException(e);
    }
}
 
开发者ID:flbaue,项目名称:jCryptTool,代码行数:22,代码来源:AesEncryptionService.java

示例9: decryptAESCBC

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
public static byte[] decryptAESCBC(byte[] key, byte[] iv, byte[] data) {
    // AES CBC PKCS7 decrypt
    try {
        CipherParameters cipherParameters = new ParametersWithIV(new KeyParameter(key), iv);
        PaddedBufferedBlockCipher cipher
                = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding());
        cipher.init(false, cipherParameters);

        byte[] buffer = new byte[cipher.getOutputSize(data.length)];

        int pos = cipher.processBytes(data, 0, data.length, buffer, 0);
        pos += cipher.doFinal(buffer, pos);

        return Arrays.copyOf(buffer, pos);

    } catch (DataLengthException | IllegalStateException | InvalidCipherTextException ex) {
        throw new IllegalArgumentException("decrypt failed", ex);
    }
}
 
开发者ID:horrorho,项目名称:InflatableDonkey,代码行数:20,代码来源:AESCBC.java

示例10: crypt

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
private static byte[] crypt(final boolean encrypt, final byte[] bytes, final String password, final byte[] salt) throws InvalidCipherTextException {
    final PBEParametersGenerator keyGenerator = new PKCS12ParametersGenerator(new SHA256Digest());
    keyGenerator.init(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password.toCharArray()), salt, 20);
    final CipherParameters keyParams = keyGenerator.generateDerivedParameters(256, 128);

    final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
    cipher.init(encrypt, keyParams);

    final byte[] processed = new byte[cipher.getOutputSize(bytes.length)];
    int outputLength = cipher.processBytes(bytes, 0, bytes.length, processed, 0);
    outputLength += cipher.doFinal(processed, outputLength);

    final byte[] results = new byte[outputLength];
    System.arraycopy(processed, 0, results, 0, outputLength);
    return results;
}
 
开发者ID:Kloudtek,项目名称:kloudmake,代码行数:17,代码来源:AESHelper.java

示例11: getHashedPassword

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
/**
 * @return AES(BCrypt(clear_password, 10), SHA256(master_password_key))
 */
public byte[] getHashedPassword(String clear_password) throws SecurityException {
	String tested_password = testIfPasswordIsStrong(clear_password);
	try {
		byte[] hashed = (BCrypt.hashpw(tested_password, BCrypt.gensalt(10))).getBytes("UTF-8");
		
		BlockCipherPadding padding = new PKCS7Padding();
		BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);
		cipher.reset();
		cipher.init(true, params);
		
		byte[] buf = new byte[cipher.getOutputSize(hashed.length)];
		int len = cipher.processBytes(hashed, 0, hashed.length, buf, 0);
		len += cipher.doFinal(buf, len);
		
		byte[] out = new byte[len];
		System.arraycopy(buf, 0, out, 0, len);
		
		return out;
	} catch (Exception e) {
		Loggers.Auth.error("Can't prepare password", e);
	}
	return null;
}
 
开发者ID:hdsdi3g,项目名称:MyDMAM,代码行数:27,代码来源:Password.java

示例12: checkPassword

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
public boolean checkPassword(String candidate_password, byte[] raw_password) {
	try {
		BlockCipherPadding padding = new PKCS7Padding();
		BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);
		cipher.reset();
		cipher.init(false, params);
		
		byte[] buf = new byte[cipher.getOutputSize(raw_password.length)];
		int len = cipher.processBytes(raw_password, 0, raw_password.length, buf, 0);
		len += cipher.doFinal(buf, len);
		
		return BCrypt.checkpw(candidate_password, new String(buf, 0, len));
	} catch (Exception e) {
		Loggers.Auth.error("Can't extract hashed password", e);
	}
	return false;
}
 
开发者ID:hdsdi3g,项目名称:MyDMAM,代码行数:18,代码来源:Password.java

示例13: setParameters

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
public void setParameters(int KeySize, int alg) throws NoSuchAlgorithmException, InvalidKeySpecException
{
	switch (alg)
	{
	case 7:
	case 1:
		this.blockSize = KeySize;
		break;
		
	case 8:
		this.blockSize = 256;
		break;
		
	default:
		this.blockSize = 128;
		break;
	}
	
	switch (KeySize)
	{
	case 128:
	case 192:
		Algs[1] = new RijndaelEngine(KeySize);
		break;
	
	case 256:
		Algs[1] = new RijndaelEngine(KeySize);
		Algs[7] = new ThreefishEngine(KeySize);
		break;
	
	default:
		Algs[7] = new ThreefishEngine(KeySize);
		break;
	}
	
	padding = new PKCS7Padding();		
}
 
开发者ID:MonroCoury,项目名称:CryptoKnight,代码行数:38,代码来源:Encryptor.java

示例14: testOutputSizes

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
private void testOutputSizes()
{
    PaddedBufferedBlockCipher bc = new PaddedBufferedBlockCipher(new DESEngine(), new PKCS7Padding());
    KeyParameter key = new KeyParameter(Hex.decode("0011223344556677"));

    for (int i = 0; i < bc.getBlockSize() * 2; i++)
    {
        bc.init(true, key);
        if (bc.getUpdateOutputSize(i) < 0)
        {
            fail("Padded cipher encrypt negative update output size for input size " + i);
        }
        if (bc.getOutputSize(i) < 0)
        {
            fail("Padded cipher encrypt negative output size for input size " + i);
        }

        bc.init(false, key);
        if (bc.getUpdateOutputSize(i) < 0)
        {
            fail("Padded cipher decrypt negative update output size for input size " + i);
        }
        if (bc.getOutputSize(i) < 0)
        {
            fail("Padded cipher decrypt negative output size for input size " + i);
        }

    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:30,代码来源:PaddingTest.java

示例15: testModes

import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入依赖的package包/类
private void testModes(BlockCipher cipher1, BlockCipher cipher2, int keySize)
    throws Exception
{
    final KeyParameter key = new KeyParameter(new byte[keySize]);
    final int blockSize = getBlockSize(cipher1);
    final CipherParameters withIv = new ParametersWithIV(key, new byte[blockSize]);

    if (blockSize > 1)
    {
        testMode(new PaddedBufferedBlockCipher(cipher1, new PKCS7Padding()), key);

        testMode(new PaddedBufferedBlockCipher(new CBCBlockCipher(cipher1), new PKCS7Padding()), withIv);

        testMode(new BufferedBlockCipher(new OFBBlockCipher(cipher1, blockSize)), withIv);
        testMode(new BufferedBlockCipher(new CFBBlockCipher(cipher1, blockSize)), withIv);
        testMode(new BufferedBlockCipher(new SICBlockCipher(cipher1)), withIv);
    }
    // CTS requires at least one block
    if (blockSize <= 16 && streamSize >= blockSize)
    {
        testMode(new CTSBlockCipher(cipher1), key);
    }
    if (blockSize <= 16 && streamSize >= blockSize)
    {
        testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS1, cipher1), key);
        testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS2, cipher1), key);
        testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS3, cipher1), key);
    }
    if (blockSize == 8 || blockSize == 16)
    {
        testMode(new EAXBlockCipher(cipher1), withIv);
    }
    if (blockSize == 16)
    {
        testMode(new CCMBlockCipher(cipher1), new ParametersWithIV(key, new byte[7]));
        testMode(new GCMBlockCipher(cipher1), withIv);
        testMode(new OCBBlockCipher(cipher1, cipher2), new ParametersWithIV(key, new byte[15]));
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:40,代码来源:CipherStreamTest.java


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