微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java.security.AlgorithmParameters的实例源码

项目:openjdk-jdk10    文件TextPKCS5PaddingTest.java   
public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SunJCE");
    if (provider == null) {
        throw new RuntimeException("SunJCE provider not exist");
    }
    // generate no-padding cipher with secret key
    Cipher c = Cipher.getInstance("DES/CBC/nopadding",provider);
    KeyGenerator kgen = KeyGenerator.getInstance("DES",provider);
    SecretKey skey = kgen.generateKey();
    // this is the improperly padded plaintext

    c.init(Cipher.ENCRYPT_MODE,skey);
    // encrypt plaintext
    byte[] cipher = c.doFinal(PLAIN_TEXT);
    AlgorithmParameters params = c.getParameters();
    // generate cipher that enforces PKCS5 padding
    c = Cipher.getInstance("DES/CBC/PKCS5Padding",provider);
    c.init(Cipher.DECRYPT_MODE,skey,params);
    try {
        c.doFinal(cipher);
        throw new RuntimeException(
                "ERROR: Expected BadPaddingException not thrown");
    } catch (BadPaddingException expected) {
        out.println("Expected BadPaddingException thrown");
    }

}
项目:jdk8u-jdk    文件SameBuffer.java   
private void runGCMWithSeparateArray(int mode,byte[] AAD,byte[] text,int txtOffset,int lenght,int offset,AlgorithmParameters params)
        throws Exception {
    // first,generate the cipher text at an allocated buffer
    Cipher cipher = createCipher(mode,params);
    cipher.updateAAD(AAD);
    byte[] outputText = cipher.doFinal(text,txtOffset,lenght);

    // new cipher for encrypt operation
    Cipher anotherCipher = createCipher(mode,params);
    anotherCipher.updateAAD(AAD);

    // next,generate cipher text again at the same buffer of plain text
    int myoff = offset;
    int off = anotherCipher.update(text,lenght,text,myoff);
    anotherCipher.doFinal(text,myoff + off);

    // check if two resutls are equal
    if (!isEqual(text,myoff,outputText,outputText.length)) {
        throw new RuntimeException("Two results not equal,mode:" + mode);
    }
}
项目:openjdk-jdk10    文件SameBuffer.java   
private void runGCMWithSeparateArray(int mode,mode:" + mode);
    }
}
项目:openjdk-jdk10    文件CipherInputStreamExceptions.java   
static void cbc_shortStream() throws Exception {
    Cipher c;
    AlgorithmParameters params;
    byte[] read = new byte[200];

    System.out.println("Running cbc_shortStream");

    // Encrypt 97 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC",97);
    // Create stream with only 96 bytes of encrypted data
    CipherInputStream in = getStream("CBC",ct,96);

    try {
        int size = in.read(read);
        in.close();
        if (size != 80) {
            throw new RuntimeException("Fail: CipherInputStream.read() " +
                    "returned " + size + ". Should have been 80");
        }
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
项目:openjdk-jdk10    文件PBEParametersTest.java   
public static void main(String[] args) throws Exception {
    PBEKeySpec ks = new PBEKeySpec(PASSWORD);
    for (int i = 0; i < PBE_ALGOS.length; i++) {
        String algo = PBE_ALGOS[i];
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algo);
        SecretKey key = skf.generateSecret(ks);
        Cipher c = Cipher.getInstance(algo,"SunJCE");
        c.init(Cipher.ENCRYPT_MODE,key);
        c.doFinal(new byte[10]); // force the generation of parameters
        AlgorithmParameters params = c.getParameters();
        if (!params.getAlgorithm().equalsIgnoreCase(algo)) {
            throw new Exception("expect: " + algo +
                                ",but got: " + params.getAlgorithm());
        }
        System.out.println(algo + "...done...");
    }
    System.out.println("Test Passed");
}
项目:openjdk-jdk10    文件SameBuffer.java   
/**
 * Run the test in case when AAD and text are placed in the same byte
 * array.
 */
private void doTestWithSameArrays(int offset,AlgorithmParameters params)
        throws Exception {
    // prepare buffers to test
    Cipher c = createCipher(Cipher.ENCRYPT_MODE,params);
    int outputLength = c.getoutputSize(textLength);
    int outputBufSize = AADLength + outputLength + offset * 2;

    byte[] AAD_and_text = Helper.generateBytes(outputBufSize);

    // do the test
    runGCMWithSameArray(Cipher.ENCRYPT_MODE,AAD_and_text,AADLength + offset,textLength,params);
    int tagLength = c.getParameters()
            .getParameterSpec(GCMParameterSpec.class).getTLen() / 8;
    runGCMWithSameArray(Cipher.DECRYPT_MODE,textLength + tagLength,params);
}
项目:openjdk-jdk10    文件SameBuffer.java   
private void doTestWithSameBuffer(int offset,AlgorithmParameters params)
        throws Exception {
    // calculate output length
    Cipher c = createCipher(Cipher.ENCRYPT_MODE,params);
    int outputLength = c.getoutputSize(textLength);

    // prepare byte buffer contained AAD and plain text
    int bufSize = AADLength + offset + outputLength;
    byte[] AAD_and_Text = Helper.generateBytes(bufSize);
    ByteBuffer AAD_and_Text_Buf = ByteBuffer.allocate(bufSize);
    AAD_and_Text_Buf.put(AAD_and_Text,AAD_and_Text.length);

    // do test
    runGCMWithSameBuffer(Cipher.ENCRYPT_MODE,AAD_and_Text_Buf,offset,params);
    int tagLength = c.getParameters()
            .getParameterSpec(GCMParameterSpec.class).getTLen() / 8;
    AAD_and_Text_Buf.limit(AADLength + offset + textLength + tagLength);
    runGCMWithSameBuffer(Cipher.DECRYPT_MODE,params);

}
项目:ipack    文件IESCipher.java   
public AlgorithmParameters engineGetParameters()
{
    if (engineParam == null && enginespec != null)
    {
        try
        {
            engineParam = AlgorithmParameters.getInstance("IES",BouncyCastleProvider.PROVIDER_NAME);
            engineParam.init(enginespec);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e.toString());
        }
    }

    return engineParam;
}
项目:ipack    文件CipherSpi.java   
protected AlgorithmParameters engineGetParameters()
{
    if (engineParams == null)
    {
        if (paramSpec != null)
        {
            try
            {
                engineParams = AlgorithmParameters.getInstance("OAEP",BouncyCastleProvider.PROVIDER_NAME);
                engineParams.init(paramSpec);
            }
            catch (Exception e)
            {
                throw new RuntimeException(e.toString());
            }
        }
    }

    return engineParams;
}
项目:jdk8u-jdk    文件SupportedEllipticCurvesExtension.java   
private static boolean isAvailableCurve(int curveId) {
    String oid = idToOidMap.get(curveId);
    if (oid != null) {
        AlgorithmParameters params = null;
        try {
            params = JsseJce.getAlgorithmParameters("EC");
            params.init(new ECGenParameterSpec(oid));
        } catch (Exception e) {
            return false;
        }

        // cache the parameters
        idToParams.put(curveId,params);

        return true;
    }

    return false;
}
项目:ipack    文件PSSSignatureSpi.java   
protected AlgorithmParameters engineGetParameters()
{
    if (engineParams == null)
    {
        if (paramSpec != null)
        {
            try
            {
                engineParams = AlgorithmParameters.getInstance("PSS",BouncyCastleProvider.PROVIDER_NAME);
                engineParams.init(paramSpec);
            }
            catch (Exception e)
            {
                throw new RuntimeException(e.toString());
            }
        }
    }

    return engineParams;
}
项目:ipack    文件IESCipher.java   
public void engineInit(
    int opmode,Key key,AlgorithmParameters params,SecureRandom random)
    throws InvalidKeyException,InvalidAlgorithmParameterException
{
    AlgorithmParameterSpec paramSpec = null;

    if (params != null)
    {
        try
        {
            paramSpec = params.getParameterSpec(IESParameterSpec.class);
        }
        catch (Exception e)
        {
            throw new InvalidAlgorithmParameterException("cannot recognise parameters: " + e.toString());
        }
    }

    engineParam = params;
    engineInit(opmode,key,paramSpec,random);

}
项目:jdk8u-jdk    文件PBEParametersTest.java   
public static void main(String[] args) throws Exception {
    PBEKeySpec ks = new PBEKeySpec(PASSWORD);
    for (int i = 0; i < PBE_ALGOS.length; i++) {
        String algo = PBE_ALGOS[i];
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algo);
        SecretKey key = skf.generateSecret(ks);
        Cipher c = Cipher.getInstance(algo,but got: " + params.getAlgorithm());
        }
        System.out.println(algo + "...done...");
    }
    System.out.println("Test Passed");
}
项目:openjdk-jdk10    文件disabledAlgorithmConstraints.java   
@Override
public boolean permits(AlgorithmParameters parameters) {
    String paramAlg = parameters.getAlgorithm();
    if (!algorithm.equalsIgnoreCase(parameters.getAlgorithm())) {
        // Consider the impact of the algorithm aliases.
        Collection<String> aliases =
                AlgorithmDecomposer.getAliases(algorithm);
        if (!aliases.contains(paramAlg)) {
            return true;
        }
    }

    int keySize = KeyUtil.getKeySize(parameters);
    if (keySize == 0) {
        return false;
    } else if (keySize > 0) {
        return !((keySize < minSize) || (keySize > maxSize) ||
            (prohibitedSize == keySize));
    }   // Otherwise,the key size is not accessible or determined.
        // Conservatively,please don't disable such keys.

    return true;
}
项目:jdk8u-jdk    文件SameBuffer.java   
private void runGCMWithSameArray(int mode,byte[] array,int length,AlgorithmParameters params) throws Exception {
    // first,generate cipher text at an allocated buffer
    Cipher cipher = createCipher(mode,params);
    cipher.updateAAD(array,AADLength);
    byte[] outputText = cipher.doFinal(array,length);

    // new cipher for encrypt operation
    Cipher anotherCipher = createCipher(mode,params);
    anotherCipher.updateAAD(array,AADLength);

    // next,generate cipher text again at the same buffer of plain text
    int off = anotherCipher.update(array,length,array,txtOffset);
    anotherCipher.doFinal(array,txtOffset + off);

    // check if two results are equal or not
    if (!isEqual(array,outputText.length)) {
        throw new RuntimeException(
                "Two results are not equal,mode:" + mode);
    }
}
项目:openjdk-jdk10    文件Encrypt.java   
private void combination_12(List<byte[]> results,int mode,byte[] plainText,AlgorithmParameters params) throws Exception {

    // prepare ByteBuffer to test
    ByteBuffer buf = ByteBuffer.allocate(AAD.length);
    buf.put(AAD);
    buf.position(0);
    buf.limit(AAD.length);
    Cipher ci = createCipher(mode,params);
    ci.updateAAD(buf);

    // prepare an empty ByteBuffer
    ByteBuffer emptyBuf = ByteBuffer.allocate(0);
    emptyBuf.put(new byte[0]);
    ci.updateAAD(emptyBuf);
    byte[] part12_1 = new byte[ci.getoutputSize(plainText.length)];
    int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
    int len12 = ci.update(plainText,plainText.length - offset,part12_1,0);
    int rest12 = ci.doFinal(plainText,len12);
    byte[] outputText12 = new byte[len12 + rest12];
    System.arraycopy(part12_1,outputText12,outputText12.length);
    results.add(outputText12);
}
项目:ipack    文件JceCMSMacCalculatorBuilder.java   
protected AlgorithmParameterSpec generateParameterSpec(ASN1ObjectIdentifier macOID,SecretKey encKey)
    throws CMSException
{
    try
    {
        if (macOID.equals(PKCSObjectIdentifiers.RC2_CBC))
        {
            byte[] iv = new byte[8];

            random.nextBytes(iv);

            return new RC2ParameterSpec(encKey.getEncoded().length * 8,iv);
        }

        AlgorithmParameterGenerator pGen = helper.createalgorithmParameterGenerator(macOID);

        AlgorithmParameters p = pGen.generateParameters();

        return p.getParameterSpec(IvParameterSpec.class);
    }
    catch (GeneralSecurityException e)
    {
        return null;
    }
}
项目:openjdk-jdk10    文件PKCS12KeyStore.java   
private AlgorithmParameters getAlgorithmParameters(String algorithm)
    throws IOException
{
    AlgorithmParameters algParams = null;

    // create PBE parameters from salt and iteration count
    PBEParameterSpec paramSpec =
            new PBEParameterSpec(getSalt(),iterationCount);
    try {
       algParams = AlgorithmParameters.getInstance(algorithm);
       algParams.init(paramSpec);
    } catch (Exception e) {
       throw new IOException("getAlgorithmParameters Failed: " +
                             e.getMessage(),e);
    }
    return algParams;
}
项目:openjdk-jdk10    文件Encrypt.java   
private void combination_9(List<byte[]> results,AlgorithmParameters params) throws Exception {

    // prepare ByteBuffer to test
    ByteBuffer buf = ByteBuffer.allocate(AAD.length);
    buf.put(AAD);
    buf.position(0);
    buf.limit(AAD.length);

    // Get Cipher object and do the combination
    Cipher c = createCipher(mode,params);
    c.updateAAD(buf);
    byte[] part91 = c.update(plainText,plainText.length);
    int part91_length = part91 == null ? 0 : part91.length;
    byte[] part92 = c.doFinal();
    byte[] outputText9 = new byte[part91_length + part92.length];

    // form result of the combination
    if (part91 != null) {
        System.arraycopy(part91,outputText9,part91_length);
    }
    System.arraycopy(part92,part91_length,part92.length);
    results.add(outputText9);
}
项目:jdk8u-jdk    文件DSAPublicKey.java   
/**
 * Returns the DSA parameters associated with this key,or null if the
 * parameters Could not be parsed.
 */
public DSAParams getParams() {
    try {
        if (algid instanceof DSAParams) {
            return (DSAParams)algid;
        } else {
            DSAParameterSpec paramSpec;
            AlgorithmParameters algParams = algid.getParameters();
            if (algParams == null) {
                return null;
            }
            paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);
            return (DSAParams)paramSpec;
        }
    } catch (InvalidParameterSpecException e) {
        return null;
    }
}
项目:RISE-V2G    文件SecurityUtils.java   
/**
 * Returns the EcpublicKey instance from its encoded raw bytes. 
 * The first byte has the fixed value 0x04 indicating the uncompressed form.
 * Therefore,the byte array must be of form: [0x04,x coord of point (32 bytes),y coord of point (32 bytes)]
 * 
 * @param publicKeyBytes The byte array representing the encoded raw bytes of the public key
 * @return The EcpublicKey instance
 */
public static EcpublicKey getPublicKey(byte[] publicKeyBytes) {
    // First we separate x and y of coordinates into separate variables
    byte[] x = new byte[32];
    byte[] y = new byte[32];
    System.arraycopy(publicKeyBytes,1,x,32);
    System.arraycopy(publicKeyBytes,33,y,32);

    try {
        KeyFactory kf = KeyFactory.getInstance("EC");

        AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
        parameters.init(new ECGenParameterSpec("secp256r1"));
        ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class);

        EcpublicKeySpec ecpublicKeySpec = new EcpublicKeySpec(new ECPoint(new BigInteger(x),new BigInteger(y)),ecParameterSpec);
        EcpublicKey ecpublicKey = (EcpublicKey) kf.generatePublic(ecpublicKeySpec);
        return ecpublicKey;
    } catch (NoSuchAlgorithmException | InvalidParameterSpecException | InvalidKeySpecException e) {
        getLogger().error(e.getClass().getSimpleName() + " occurred when trying to get public key from raw bytes",e);
        return null;
    }
}
项目:jdk8u-jdk    文件Encrypt.java   
private void combination_9(List<byte[]> results,part92.length);
    results.add(outputText9);
}
项目:jdk8u-jdk    文件Encrypt.java   
private void combination_2(List<byte[]> results,AlgorithmParameters params) throws Exception {
    Cipher c = createCipher(mode,params);
    c.updateAAD(AAD);
    int t = 0;
    int offset = 0;
    if (plainText.length > ARRAY_OFFSET) {
        t = plainText.length - ARRAY_OFFSET;
        offset = ARRAY_OFFSET;
    }
    byte[] part21 = c.update(plainText,t);
    byte[] part22 = new byte[c.getoutputSize(plainText.length)];
    int len2 = c.doFinal(plainText,t,part22,0);
    int part21Length = part21 != null ? part21.length : 0;
    byte[] outputText2 = new byte[part21Length + len2];
    if (part21 != null) {
        System.arraycopy(part21,outputText2,part21Length);
    }
    System.arraycopy(part22,part21Length,len2);
    results.add(outputText2);
}
项目:alfresco-core    文件DefaultEncryptionUtils.java   
/**
 * {@inheritDoc}
 */
@Override
public byte[] decryptBody(HttpServletRequest req) throws IOException
{
    if(req.getmethod().equals("POST"))
    {
        InputStream bodyStream = req.getInputStream();
        if(bodyStream != null)
        {
            // expect algorithParameters header
            AlgorithmParameters p = decodeAlgorithmParameters(req);

            // decrypt the body
            InputStream in = encryptor.decrypt(KeyProvider.ALIAS_SOLR,p,bodyStream);
            return IoUtils.toByteArray(in);
        }
        else
        {
            return null;
        }
    }
    else
    {
        return null;
    }
}
项目:Openjsharp    文件DSAPublicKey.java   
/**
 * Returns the DSA parameters associated with this key,or null if the
 * parameters Could not be parsed.
 */
public DSAParams getParams() {
    try {
        if (algid instanceof DSAParams) {
            return (DSAParams)algid;
        } else {
            DSAParameterSpec paramSpec;
            AlgorithmParameters algParams = algid.getParameters();
            if (algParams == null) {
                return null;
            }
            paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);
            return (DSAParams)paramSpec;
        }
    } catch (InvalidParameterSpecException e) {
        return null;
    }
}
项目:alfresco-core    文件DefaultEncryptor.java   
protected Cipher createCipher(int mode,String algorithm,String provider,AlgorithmParameters params)
throws NoSuchAlgorithmException,NoSuchPaddingException,NoSuchProviderException,InvalidKeyException,InvalidAlgorithmParameterException
{
    Cipher cipher = null;

    if (cipherProvider == null)
    {
        cipher = Cipher.getInstance(algorithm);
    }
    else
    {
        cipher = Cipher.getInstance(algorithm,provider);
    }
    cipher.init(mode,params);

    return cipher;
}
项目:Openjsharp    文件disabledAlgorithmConstraints.java   
@Override
final public boolean permits(Set<CryptoPrimitive> primitives,AlgorithmParameters parameters) {

    if (algorithm == null || algorithm.length() == 0) {
        throw new IllegalArgumentException("No algorithm name specified");
    }

    return checkConstraints(primitives,algorithm,parameters);
}
项目:keepass2android    文件JCEBlockCipher.java   
protected void engineInit(
    int                 opmode,Key                 key,SecureRandom        random) 
throws InvalidKeyException,InvalidAlgorithmParameterException
{
    AlgorithmParameterSpec  paramSpec = null;

    if (params != null)
    {
        for (int i = 0; i != availableSpecs.length; i++)
        {
            try
            {
                paramSpec = params.getParameterSpec(availableSpecs[i]);
                break;
            }
            catch (Exception e)
            {
                // try again if possible
            }
        }

        if (paramSpec == null)
        {
            throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
        }
    }

    engineInit(opmode,random);

    engineParams = params;
}
项目:openjdk-jdk10    文件PKCS12KeyStore.java   
private static String mapPBEParamsToAlgorithm(ObjectIdentifier algorithm,AlgorithmParameters algParams) throws NoSuchAlgorithmException {
    // Check for PBES2 algorithms
    if (algorithm.equals(pbes2_OID) && algParams != null) {
        return algParams.toString();
    }
    return algorithm.toString();
}
项目:keepass2android    文件JCEStreamCipher.java   
protected void engineInit(
    int                 opmode,SecureRandom        random) 
    throws InvalidKeyException,InvalidAlgorithmParameterException
{
    AlgorithmParameterSpec  paramSpec = null;

    if (params != null)
    {
        for (int i = 0; i != availableSpecs.length; i++)
        {
            try
            {
                paramSpec = params.getParameterSpec(availableSpecs[i]);
                break;
            }
            catch (Exception e)
            {
                continue;
            }
        }

        if (paramSpec == null)
        {
            throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
        }
    }

    engineInit(opmode,random);
    engineParams = params;
}
项目:openjdk-jdk10    文件Encrypt.java   
private void combination_4(List<byte[]> results,AlgorithmParameters params) throws Exception {
    Cipher ci = createCipher(mode,params);
    ci.updateAAD(AAD);
    byte[] part41 = new byte[ci.getoutputSize(plainText.length)];
    int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
    int len = ci.update(plainText,part41,0);
    int rest4 = ci.doFinal(plainText,len);
    byte[] outputText4 = new byte[len + rest4];
    System.arraycopy(part41,outputText4,outputText4.length);
    results.add(outputText4);
}
项目:PACE    文件AESValueEncryptor.java   
@Override
byte[] decrypt(byte[] key,byte[] data) {
  try {
    // Sun's impelemntation of GCM uses a custom algorithm name. We handle this odd (potentially incorrect) behavior here.
    AlgorithmParameters params = AlgorithmParameters.getInstance(isinstanceOfSunProvidedGCM ? "GCM" : "AES");

    // Read the Metadata.
    ByteArrayInputStream stream = new ByteArrayInputStream(data);
    DataInput in = new DataInputStream(stream);

    int MetadataLength = Writableutils.readVInt(in);
    byte[] Metadata = new byte[MetadataLength];
    in.readFully(Metadata);
    params.init(Metadata);

    // Decrypt the remaining data.
    SecretKeySpec keySpec = new SecretKeySpec(key,AES);
    cipher.init(Cipher.DECRYPT_MODE,keySpec,params);
    byte[] ciphertext = new byte[stream.available()];
    in.readFully(ciphertext);

    cipher.init(Cipher.DECRYPT_MODE,params);
    return cipher.doFinal(ciphertext);
  } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException
      | IOException e) {
    throw new EncryptionException(e);
  }
}
项目:ipack    文件JCEStreamCipher.java   
protected void engineInit(
    int                 opmode,random);
    engineParams = params;
}
项目:ipack    文件brokenJCEBlockCipher.java   
protected AlgorithmParameters engineGetParameters() 
{
    if (engineParams == null)
    {
        if (ivParam != null)
        {
            String  name = cipher.getUnderlyingCipher().getAlgorithmName();

            if (name.indexOf('/') >= 0)
            {
                name = name.substring(0,name.indexOf('/'));
            }

            try
            {
                engineParams = AlgorithmParameters.getInstance(name,BouncyCastleProvider.PROVIDER_NAME);
                engineParams.init(ivParam.getIV());
            }
            catch (Exception e)
            {
                throw new RuntimeException(e.toString());
            }
        }
    }

    return engineParams;
}
项目:Openjsharp    文件PKCS12KeyStore.java   
private AlgorithmParameters parseAlgParameters(ObjectIdentifier algorithm,DerInputStream in) throws IOException
{
    AlgorithmParameters algParams = null;
    try {
        DerValue params;
        if (in.available() == 0) {
            params = null;
        } else {
            params = in.getDerValue();
            if (params.tag == DerValue.tag_Null) {
               params = null;
            }
        }
        if (params != null) {
            if (algorithm.equals((Object)pbes2_OID)) {
                algParams = AlgorithmParameters.getInstance("PBES2");
            } else {
                algParams = AlgorithmParameters.getInstance("PBE");
            }
            algParams.init(params.toByteArray());
        }
    } catch (Exception e) {
       throw new IOException("parseAlgParameters Failed: " +
                             e.getMessage(),e);
    }
    return algParams;
}
项目:kafka-webview    文件SecretManager.java   
/**
 * Encrypt plaintext.
 * @param str Plaintext to encrypt
 * @return Cipher text
 */
public String encrypt(final String str) {
    if (str == null) {
        throw new NullPointerException("Argument cannot be null");
    }

    try {
        final SecureRandom random = new SecureRandom();
        final byte[] salt = new byte[16];
        random.nextBytes(salt);

        final SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        final KeySpec spec = new PBEKeySpec(passphrase.tochararray(),salt,65536,128);
        final SecretKey tmp = factory.generateSecret(spec);
        final SecretKey secret = new SecretKeySpec(tmp.getEncoded(),"AES");

        final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE,secret);

        final AlgorithmParameters params = cipher.getParameters();
        final byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
        final byte[] encryptedText = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8));

        // concatenate salt + iv + ciphertext
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(salt);
        outputStream.write(iv);
        outputStream.write(encryptedText);

        // properly encode the complete cipher text
        return DatatypeConverter.printBase64Binary(outputStream.toByteArray());
    } catch (final Exception exception) {
        throw new RuntimeException(exception.getMessage(),exception);
    }
}
项目:ipack    文件BaseWrapCipher.java   
protected void engineInit(
    int                 opmode,SecureRandom        random)
throws InvalidKeyException,InvalidAlgorithmParameterException
{
    AlgorithmParameterSpec  paramSpec = null;

    if (params != null)
    {
        for (int i = 0; i != availableSpecs.length; i++)
        {
            try
            {
                paramSpec = params.getParameterSpec(availableSpecs[i]);
                break;
            }
            catch (Exception e)
            {
                // try next spec
            }
        }

        if (paramSpec == null)
        {
            throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
        }
    }

    engineParams = params;
    engineInit(opmode,random);
}
项目:Openjsharp    文件AlgorithmChecker.java   
/**
 * Check the signature algorithm with the specified public key.
 *
 * @param key the public key to verify the CRL signature
 * @param crl the target CRL
 */
static void check(PublicKey key,AlgorithmId algorithmId)
                    throws CertPathValidatorException {
    String sigalgName = algorithmId.getName();
    AlgorithmParameters sigalgParams = algorithmId.getParameters();

    if (!certPathDefaultConstraints.permits(
            SIGNATURE_PRIMITIVE_SET,sigalgName,sigalgParams)) {
        throw new CertPathValidatorException(
            "algorithm check Failed: " + sigalgName + " is disabled",null,-1,BasicReason.ALGORITHM_CONSTRAINED);
    }
}
项目:ipack    文件AlgorithmParameterGeneratorSpi.java   
protected AlgorithmParameters engineGenerateParameters()
{
    GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();

    if (random != null)
    {
        pGen.init(strength,2,random);
    }
    else
    {
        pGen.init(strength,new SecureRandom());
    }

    GOST3410Parameters p = pGen.generateParameters();

    AlgorithmParameters params;

    try
    {
        params = AlgorithmParameters.getInstance("GOST3410",BouncyCastleProvider.PROVIDER_NAME);
        params.init(new GOST3410ParameterSpec(new GOST3410PublicKeyParameterSetSpec(p.getP(),p.getQ(),p.getA())));
    }
    catch (Exception e)
    {
        throw new RuntimeException(e.getMessage());
    }

    return params;
}
项目:openjdk-jdk10    文件NativeCipherWithJavaPadding.java   
@Override
protected synchronized void engineInit(int opmode,SecureRandom random)
        throws InvalidKeyException,InvalidAlgorithmParameterException {
    reset();
    nc.engineInit(opmode,params,random);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。