Search in sources :

Example 26 with CipherParameters

use of org.bouncycastle.crypto.CipherParameters in project elastic-core-maven by OrdinaryDude.

the class Crypto method aesDecrypt.

public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] key) {
    try {
        if (ivCiphertext.length < 16 || ivCiphertext.length % 16 != 0) {
            throw new InvalidCipherTextException("invalid ivCiphertext length");
        }
        byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16);
        byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(false, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(ciphertext.length)];
        int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0);
        plaintextLength += aes.doFinal(output, plaintextLength);
        byte[] result = new byte[plaintextLength];
        System.arraycopy(output, 0, result, 0, result.length);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Example 27 with CipherParameters

use of org.bouncycastle.crypto.CipherParameters in project web3sdk by FISCO-BCOS.

the class SM2Signer method initWithCache.

/**
 * The same as init method with better performance by adding the cache for the z value
 * corresponding to the privateKey value
 *
 * @param forSigning
 * @param param
 */
public void initWithCache(boolean forSigning, CipherParameters param) {
    CipherParameters baseParam;
    byte[] userID;
    if (param instanceof ParametersWithID) {
        baseParam = ((ParametersWithID) param).getParameters();
        userID = ((ParametersWithID) param).getID();
    } else {
        baseParam = param;
        // the default value
        userID = Hex.decode("31323334353637383132333435363738");
    }
    if (forSigning) {
        if (baseParam instanceof ParametersWithRandom) {
            ParametersWithRandom rParam = (ParametersWithRandom) baseParam;
            ecKey = (ECKeyParameters) rParam.getParameters();
            ecParams = ecKey.getParameters();
            kCalculator.init(ecParams.getN(), rParam.getRandom());
        } else {
            ecKey = (ECKeyParameters) baseParam;
            ecParams = ecKey.getParameters();
            kCalculator.init(ecParams.getN(), CryptoServicesRegistrar.getSecureRandom());
        }
        BigInteger privateKey = ((ECPrivateKeyParameters) ecKey).getD();
        /**
         * First find z value from zValueCache
         */
        z = zValueCache.get(privateKey);
        if (Objects.isNull(z)) {
            // z value of privateKey not exist, calculate it and set it to the cache
            pubPoint = createBasePointMultiplier().multiply(ecParams.getG(), ((ECPrivateKeyParameters) ecKey).getD()).normalize();
            z = getZ(userID);
            zValueCache.put(privateKey, z);
            logger.info(" privateKey: {} z value not exist, caculate z: {}", privateKey, Hex.toHexString(z));
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug(" privateKey: {} z value, z: {}", privateKey, Hex.toHexString(z));
            }
        }
        digest.update(z, 0, z.length);
    } else {
        ecKey = (ECKeyParameters) baseParam;
        ecParams = ecKey.getParameters();
        pubPoint = ((ECPublicKeyParameters) ecKey).getQ();
        z = getZ(userID);
        digest.update(z, 0, z.length);
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ParametersWithID(org.bouncycastle.crypto.params.ParametersWithID) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) BigInteger(java.math.BigInteger)

Example 28 with CipherParameters

use of org.bouncycastle.crypto.CipherParameters in project web3sdk by FISCO-BCOS.

the class SM2Signer method init.

@Override
public void init(boolean forSigning, CipherParameters param) {
    CipherParameters baseParam;
    byte[] userID;
    if (param instanceof ParametersWithID) {
        baseParam = ((ParametersWithID) param).getParameters();
        userID = ((ParametersWithID) param).getID();
    } else {
        baseParam = param;
        // the default value
        userID = Hex.decode("31323334353637383132333435363738");
    }
    if (forSigning) {
        if (baseParam instanceof ParametersWithRandom) {
            ParametersWithRandom rParam = (ParametersWithRandom) baseParam;
            ecKey = (ECKeyParameters) rParam.getParameters();
            ecParams = ecKey.getParameters();
            kCalculator.init(ecParams.getN(), rParam.getRandom());
        } else {
            ecKey = (ECKeyParameters) baseParam;
            ecParams = ecKey.getParameters();
            kCalculator.init(ecParams.getN(), CryptoServicesRegistrar.getSecureRandom());
        }
        pubPoint = createBasePointMultiplier().multiply(ecParams.getG(), ((ECPrivateKeyParameters) ecKey).getD()).normalize();
    } else {
        ecKey = (ECKeyParameters) baseParam;
        ecParams = ecKey.getParameters();
        pubPoint = ((ECPublicKeyParameters) ecKey).getQ();
    }
    z = getZ(userID);
    digest.update(z, 0, z.length);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithID(org.bouncycastle.crypto.params.ParametersWithID) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom)

Example 29 with CipherParameters

use of org.bouncycastle.crypto.CipherParameters in project hutool by looly.

the class SM2 method sign.

/**
 * 用私钥对信息生成数字签名,签名格式为ASN1<br>
 * 在硬件签名中,返回结果为R+S,可以通过调用{@link cn.hutool.crypto.SmUtil#rsAsn1ToPlain(byte[])}方法转换之。
 *
 * @param data 被签名的数据数据
 * @param id   可以为null,若为null,则默认withId为字节数组:"1234567812345678".getBytes()
 * @return 签名
 */
public byte[] sign(byte[] data, byte[] id) {
    lock.lock();
    final SM2Signer signer = getSigner();
    try {
        CipherParameters param = new ParametersWithRandom(getCipherParameters(KeyType.PrivateKey));
        if (id != null) {
            param = new ParametersWithID(param, id);
        }
        signer.init(true, param);
        signer.update(data, 0, data.length);
        return signer.generateSignature();
    } catch (org.bouncycastle.crypto.CryptoException e) {
        throw new CryptoException(e);
    } finally {
        lock.unlock();
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithID(org.bouncycastle.crypto.params.ParametersWithID) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) CryptoException(cn.hutool.crypto.CryptoException) SM2Signer(org.bouncycastle.crypto.signers.SM2Signer)

Example 30 with CipherParameters

use of org.bouncycastle.crypto.CipherParameters in project hutool by looly.

the class SM2 method verify.

/**
 * 用公钥检验数字签名的合法性
 *
 * @param data 数据签名后的数据
 * @param sign 签名
 * @param id   可以为null,若为null,则默认withId为字节数组:"1234567812345678".getBytes()
 * @return 是否验证通过
 */
public boolean verify(byte[] data, byte[] sign, byte[] id) {
    lock.lock();
    final SM2Signer signer = getSigner();
    try {
        CipherParameters param = getCipherParameters(KeyType.PublicKey);
        if (id != null) {
            param = new ParametersWithID(param, id);
        }
        signer.init(false, param);
        signer.update(data, 0, data.length);
        return signer.verifySignature(sign);
    } finally {
        lock.unlock();
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithID(org.bouncycastle.crypto.params.ParametersWithID) SM2Signer(org.bouncycastle.crypto.signers.SM2Signer)

Aggregations

CipherParameters (org.bouncycastle.crypto.CipherParameters)60 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)35 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)24 InvalidKeyException (java.security.InvalidKeyException)21 AESEngine (org.bouncycastle.crypto.engines.AESEngine)16 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)14 IvParameterSpec (javax.crypto.spec.IvParameterSpec)14 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)14 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)12 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)12 SecureRandom (java.security.SecureRandom)11 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)11 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)9 BlockCipher (org.bouncycastle.crypto.BlockCipher)8 SecretKey (javax.crypto.SecretKey)7 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)7 PBEParametersGenerator (org.bouncycastle.crypto.PBEParametersGenerator)7 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)7 IOException (java.io.IOException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5