use of com.github.zhenwei.core.exception.WeGooCryptoException in project LinLong-Java by zhenwei1108.
the class Pkcs1PaddingBuilder method encodePkcs1Padding.
/**
* 若为私钥,标志位01,中间填充FF,保证每次填充一致,签名结果的一致 若为公钥,标志位02,则中间填充使用随机数 00 01/02 || PS(随机数/OxFF) || 00 ||
* T(数据摘要)
*/
public static byte[] encodePkcs1Padding(byte[] data, boolean isPrivate, int modulusLength, DigestAlgEnum digestAlg) throws WeGooCryptoException {
try {
if (modulusLength % 1024 == 0) {
modulusLength = modulusLength / 8;
}
if (modulusLength % 128 != 0) {
throw new WeGooCryptoException(CryptoExceptionMassageEnum.params_err);
}
ASN1ObjectIdentifier hashOid = digestAlg.getOid();
// 组装摘要值
MessageDigest digest = MessageDigest.getInstance(digestAlg.name());
digest.update(data);
byte[] hash = digest.digest();
int T = hash.length;
int emLen = modulusLength - 1;
if (emLen < (T + 10)) {
/*intended encoded message length too short*/
throw new WeGooCryptoException(CryptoExceptionMassageEnum.params_short_err);
}
int psLength = Math.max(emLen - T - 2, 8);
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(hashOid, DERNull.INSTANCE);
DigestInfo dInfo = new DigestInfo(algorithmIdentifier, hash);
byte[] in = dInfo.getEncoded(ASN1Encoding.DER);
SecureRandom random = CryptoServicesRegistrar.getSecureRandom();
byte[] block = new byte[3 + psLength + T];
int i;
int inLen = in.length;
if (isPrivate) {
block[1] = 1;
for (i = 2; i != block.length - inLen; ++i) {
block[i] = -1;
}
} else {
random.nextBytes(block);
block[0] = 0;
block[1] = 2;
for (i = 2; i != block.length - inLen; ++i) {
while (block[i] == 0) {
block[i] = (byte) random.nextInt();
}
}
}
block[block.length - inLen - 1] = 0;
System.arraycopy(in, 0, block, block.length - inLen, inLen);
return block;
} catch (Exception e) {
throw new WeGooCryptoException(e);
}
}
Aggregations