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);
}
}
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);
}
}
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);
}
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();
}
}
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();
}
}
Aggregations