Search in sources :

Example 11 with WeGooCryptoException

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);
    }
}
Also used : WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) DigestInfo(com.github.zhenwei.core.asn1.x509.DigestInfo) SecureRandom(java.security.SecureRandom) MessageDigest(java.security.MessageDigest) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Aggregations

WeGooCryptoException (com.github.zhenwei.core.exception.WeGooCryptoException)11 BaseWeGooException (com.github.zhenwei.core.exception.BaseWeGooException)4 WeGooProvider (com.github.zhenwei.provider.jce.provider.WeGooProvider)4 WeGooKeyException (com.github.zhenwei.core.exception.WeGooKeyException)3 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)2 KeyPairAlgEnum (com.github.zhenwei.core.enums.KeyPairAlgEnum)2 BigInteger (java.math.BigInteger)2 KeyStore (java.security.KeyStore)2 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 ASN1Set (com.github.zhenwei.core.asn1.ASN1Set)1 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 DLSequence (com.github.zhenwei.core.asn1.DLSequence)1 ContentInfo (com.github.zhenwei.core.asn1.pkcs.ContentInfo)1 SignedData (com.github.zhenwei.core.asn1.pkcs.SignedData)1 X500Name (com.github.zhenwei.core.asn1.x500.X500Name)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 DigestInfo (com.github.zhenwei.core.asn1.x509.DigestInfo)1 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)1 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)1