private void checkCryptoPerm(CipherSpi checkSpi, Key key) throws InvalidKeyException { if (cryptoPerm == CryptoAllPermission.INSTANCE) { return; } // Check if key size and default parameters are within legal limits AlgorithmParameterSpec params; try { params = getAlgorithmParameterSpec(checkSpi.engineGetParameters()); } catch (InvalidParameterSpecException ipse) { throw new InvalidKeyException ("Unsupported default algorithm parameters"); } if (!passCryptoPermCheck(checkSpi, key, params)) { throw new InvalidKeyException( "Illegal key size or default parameters"); } }
/** * DES算法,加密 * * @param data 待加密字符串 * @param key 加密私钥,长度不能够小于8位 * @return 加密后的字节数组,一般结合Base64编码使用 * @throws InvalidAlgorithmParameterException * @throws Exception */ public static String encode(String key,String data) { if(data == null) return null; try{ DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //key的长度不能够小于8位字节 Key secretKey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(ALGORITHM_DES); IvParameterSpec iv = new IvParameterSpec(key.getBytes()); AlgorithmParameterSpec paramSpec = iv; cipher.init(Cipher.ENCRYPT_MODE, secretKey,paramSpec); byte[] bytes = cipher.doFinal(data.getBytes()); return byte2hex(bytes); }catch(Exception e){ e.printStackTrace(); return data; } }
protected void engineInit(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (params instanceof TlsPrfParameterSpec == false) { throw new InvalidAlgorithmParameterException(MSG); } this.spec = (TlsPrfParameterSpec)params; SecretKey key = spec.getSecret(); if (key == null) { key = NULL_KEY; } try { p11Key = P11SecretKeyFactory.convertKey(token, key, null); } catch (InvalidKeyException e) { throw new InvalidAlgorithmParameterException("init() failed", e); } }
private void checkCryptoPerm(CipherSpi checkSpi, Key key, AlgorithmParameters params) throws InvalidKeyException, InvalidAlgorithmParameterException { if (cryptoPerm == CryptoAllPermission.INSTANCE) { return; } // Convert the specified parameters into specs and then delegate. AlgorithmParameterSpec pSpec; try { pSpec = getAlgorithmParameterSpec(params); } catch (InvalidParameterSpecException ipse) { throw new InvalidAlgorithmParameterException ("Failed to retrieve algorithm parameter specification"); } checkCryptoPerm(checkSpi, key, pSpec); }
private AlgorithmParameterSpec getAlgorithmParameterSpec( AlgorithmParameters params) throws InvalidParameterSpecException { if (params == null) { return null; } String alg = params.getAlgorithm().toUpperCase(Locale.ENGLISH); if (alg.equalsIgnoreCase("RC2")) { return params.getParameterSpec(RC2ParameterSpec.class); } if (alg.equalsIgnoreCase("RC5")) { return params.getParameterSpec(RC5ParameterSpec.class); } if (alg.startsWith("PBE")) { return params.getParameterSpec(PBEParameterSpec.class); } if (alg.startsWith("DES")) { return params.getParameterSpec(IvParameterSpec.class); } return null; }
@Override protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException { int ret = 0; byte[] encodedKey; /* key must be of type SecretKey */ if (!(key instanceof SecretKey)) throw new InvalidKeyException("Key is not of type SecretKey"); /* get encoded key */ encodedKey = key.getEncoded(); if (encodedKey == null) throw new InvalidKeyException("Key does not support encoding"); this.hmac.setKey(nativeHmacType, encodedKey); if (debug.DEBUG) log("init with key and spec"); }
/** * Initializes this key pair generator for the specified parameter * set and source of randomness. * * <p>The given parameter set contains the prime modulus, the base * generator, and optionally the requested size in bits of the random * exponent (private value). * * @param algParams the parameter set used to generate the key pair * @param random the source of randomness * * @exception InvalidAlgorithmParameterException if the given parameters * are inappropriate for this key pair generator */ public void initialize(AlgorithmParameterSpec algParams, SecureRandom random) throws InvalidAlgorithmParameterException { if (!(algParams instanceof DHParameterSpec)){ throw new InvalidAlgorithmParameterException ("Inappropriate parameter type"); } params = (DHParameterSpec)algParams; pSize = params.getP().bitLength(); try { checkKeySize(pSize); } catch (InvalidParameterException ipe) { throw new InvalidAlgorithmParameterException(ipe.getMessage()); } // exponent size is optional, could be 0 lSize = params.getL(); // Require exponentSize < primeSize if ((lSize != 0) && (lSize > pSize)) { throw new InvalidAlgorithmParameterException ("Exponent size must not be larger than modulus size"); } this.random = random; }
protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (!(paramSpec instanceof RC2ParameterSpec)) { throw new InvalidParameterSpecException ("Inappropriate parameter specification"); } RC2ParameterSpec rps = (RC2ParameterSpec) paramSpec; // check effective key size (a value of 0 means it is unspecified) effectiveKeySize = rps.getEffectiveKeyBits(); if (effectiveKeySize != 0) { if (effectiveKeySize < 1 || effectiveKeySize > 1024) { throw new InvalidParameterSpecException("RC2 effective key " + "size must be between 1 and 1024 bits"); } if (effectiveKeySize < 256) { version = EKB_TABLE[effectiveKeySize]; } else { version = effectiveKeySize; } } this.iv = rps.getIV(); }
public void engineInit( int opmode, Key key, SecureRandom random) throws InvalidKeyException { try { engineInit(opmode, key, (AlgorithmParameterSpec)null, random); } catch (InvalidAlgorithmParameterException e) { throw new IllegalArgumentException("can't handle supplied parameter spec"); } }
/** * Initializes this parameter generator with a set of * algorithm-specific parameter generation values. * * @param genParamSpec the set of algorithm-specific parameter * generation values * @param random the source of randomness * * @exception InvalidAlgorithmParameterException if the given parameter * generation values are inappropriate for this parameter generator */ @Override protected void engineInit(AlgorithmParameterSpec genParamSpec, SecureRandom random) throws InvalidAlgorithmParameterException { if (!(genParamSpec instanceof DSAGenParameterSpec)) { throw new InvalidAlgorithmParameterException("Invalid parameter"); } DSAGenParameterSpec dsaGenParams = (DSAGenParameterSpec)genParamSpec; // directly initialize using the already validated values this.valueL = dsaGenParams.getPrimePLength(); this.valueN = dsaGenParams.getSubprimeQLength(); this.seedLen = dsaGenParams.getSeedLength(); this.random = random; }
private Crypto2(String passPhrase) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, UnsupportedEncodingException { /* Ciphering options: Mode = CipherMode.CBC,-( Cipher-block chaining) Padding = PaddingMode.PKCS7 or PKCS5, KeySize = 128, BlockSize = 128, Key = keyBytes - password, IV = keyBytes - password */ // Create the key byte[] bytesOfMessage = passPhrase.getBytes("UTF-8"); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] bytesPassphrase = md.digest(bytesOfMessage); SecretKeySpec key = new SecretKeySpec(bytesPassphrase, "AES"); // Parameter specific algorithm AlgorithmParameterSpec paramSpec = new IvParameterSpec(bytesPassphrase); ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); }
@SuppressWarnings("deprecation") protected void engineInit(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (params instanceof TlsMasterSecretParameterSpec == false) { throw new InvalidAlgorithmParameterException(MSG); } this.spec = (TlsMasterSecretParameterSpec)params; if ("RAW".equals(spec.getPremasterSecret().getFormat()) == false) { throw new InvalidAlgorithmParameterException( "Key format must be RAW"); } protocolVersion = (spec.getMajorVersion() << 8) | spec.getMinorVersion(); if ((protocolVersion < 0x0300) || (protocolVersion > 0x0303)) { throw new InvalidAlgorithmParameterException( "Only SSL 3.0, TLS 1.0/1.1/1.2 supported"); } }
protected void engineInit(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (params instanceof TlsKeyMaterialParameterSpec == false) { throw new InvalidAlgorithmParameterException(MSG); } this.spec = (TlsKeyMaterialParameterSpec)params; if ("RAW".equals(spec.getMasterSecret().getFormat()) == false) { throw new InvalidAlgorithmParameterException( "Key format must be RAW"); } protocolVersion = (spec.getMajorVersion() << 8) | spec.getMinorVersion(); if ((protocolVersion < 0x0300) || (protocolVersion > 0x0303)) { throw new InvalidAlgorithmParameterException( "Only SSL 3.0, TLS 1.0/1.1/1.2 supported"); } }
@Override protected void engineInit( AlgorithmParameterSpec genParamSpec, SecureRandom random) throws InvalidAlgorithmParameterException { throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for IDEA parameter generation."); }
@Override SecretKey intSecurityKey(AlgorithmParameterSpec[] spec) throws InvalidKeySpecException { byte[] salt = new byte[8]; int iterCnt = 6; new Random().nextBytes(salt); spec[0] = new PBEParameterSpec(salt, iterCnt); PBEKeySpec pbeKS = new PBEKeySpec( new String("So far so good").toCharArray()); SecretKey key1 = new MyOwnSecKey(pbeKS); return key1; }
protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { if (params != null) { throw new InvalidAlgorithmParameterException ("Parameters not supported"); } engineInit(key, random); }
protected <T extends AlgorithmParameterSpec> T engineGetParameterSpec(Class<T> paramSpec) throws InvalidParameterSpecException { if (GCMParameterSpec.class.isAssignableFrom(paramSpec)) { return paramSpec.cast(new GCMParameterSpec(tLen * 8, iv)); } else { throw new InvalidParameterSpecException ("Inappropriate parameter specification"); } }
protected void engineInit(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) { throw new InvalidAlgorithmParameterException(MSG); } this.spec = (TlsRsaPremasterSecretParameterSpec)params; this.random = random; }
protected void engineInit( int opmode, Key key, SecureRandom random) throws InvalidKeyException { try { engineInit(opmode, key, (AlgorithmParameterSpec)null, random); } catch (InvalidAlgorithmParameterException e) { throw new IllegalArgumentException(e.getMessage()); } }
protected void engineInit( int opmode, Key key, SecureRandom random) throws InvalidKeyException { try { engineInit(opmode, key, (AlgorithmParameterSpec)null, random); } catch (InvalidAlgorithmParameterException e) { throw new InvalidKeyException(e.getMessage()); } }
protected void engineInit(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) { throw new InvalidAlgorithmParameterException(MSG); } this.spec = (TlsRsaPremasterSecretParameterSpec)params; }
/** * Creates a <code>DOMSignatureMethod</code>. * * @param params the algorithm-specific params (may be <code>null</code>) * @throws InvalidAlgorithmParameterException if the parameters are not * appropriate for this signature method */ DOMSignatureMethod(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { if (params != null && !(params instanceof SignatureMethodParameterSpec)) { throw new InvalidAlgorithmParameterException ("params must be of type SignatureMethodParameterSpec"); } checkParams((SignatureMethodParameterSpec)params); this.params = (SignatureMethodParameterSpec)params; }
public static String a(String str, String str2) { AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(a); Key secretKeySpec = new SecretKeySpec(str.getBytes(), "DES"); Cipher instance = Cipher.getInstance(DEs.ALGORITHM_DES); instance.init(1, secretKeySpec, ivParameterSpec); return cn.com.iresearch.vvtracker.a.a.a.a(instance.doFinal(str2.getBytes())); }
@Override public int hashCode() { int result = 17; result = 31 * result + getAlgorithm().hashCode(); AlgorithmParameterSpec spec = getParameterSpec(); if (spec != null) { result = 31 * result + spec.hashCode(); } return result; }
public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (params instanceof RSAKeyGenParameterSpec == false) { throw new InvalidAlgorithmParameterException ("Params must be instance of RSAKeyGenParameterSpec"); } RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params; int tmpKeySize = rsaSpec.getKeysize(); BigInteger tmpPublicExponent = rsaSpec.getPublicExponent(); if (tmpPublicExponent == null) { tmpPublicExponent = RSAKeyGenParameterSpec.F4; } else { if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) { throw new InvalidAlgorithmParameterException ("Public exponent must be 3 or larger"); } if (tmpPublicExponent.bitLength() > tmpKeySize) { throw new InvalidAlgorithmParameterException ("Public exponent must be smaller than key size"); } } // do not allow unreasonably large key sizes, probably user error try { RSAKeyFactory.checkKeyLengths(tmpKeySize, tmpPublicExponent, 512, 64 * 1024); } catch (InvalidKeyException e) { throw new InvalidAlgorithmParameterException( "Invalid key sizes", e); } this.keySize = tmpKeySize; this.publicExponent = tmpPublicExponent; this.random = random; }
public void initialize(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { kpg = new McElieceCCA2KeyPairGenerator(); super.initialize(params); ECCKeyGenParameterSpec ecc = (ECCKeyGenParameterSpec)params; McElieceCCA2KeyGenerationParameters mccca2KGParams = new McElieceCCA2KeyGenerationParameters(new SecureRandom(), new McElieceCCA2Parameters(ecc.getM(), ecc.getT())); kpg.init(mccca2KGParams); }
void init(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (!(paramSpec instanceof IvParameterSpec)) { throw new InvalidParameterSpecException ("Inappropriate parameter specification"); } byte[] tmpIv = ((IvParameterSpec)paramSpec).getIV(); if (tmpIv.length != block_size) { throw new InvalidParameterSpecException("IV not " + block_size + " bytes long"); } iv = tmpIv.clone(); }
public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (serviceIterator == null) { spi.initialize(params, random); return; } Exception failure = null; KeyPairGeneratorSpi mySpi = spi; do { try { mySpi.initialize(params, random); initType = I_PARAMS; initKeySize = 0; initParams = params; initRandom = random; return; } catch (Exception e) { if (failure == null) { failure = e; } mySpi = nextSpi(mySpi, false); } } while (mySpi != null); if (failure instanceof RuntimeException) { throw (RuntimeException)failure; } // must be an InvalidAlgorithmParameterException throw (InvalidAlgorithmParameterException)failure; }
protected void engineInit( AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (!(paramSpec instanceof IESParameterSpec)) { throw new InvalidParameterSpecException("IESParameterSpec required to initialise a IES algorithm parameters object"); } this.currentSpec = (IESParameterSpec)paramSpec; }
private AlgorithmParameterSpec getAlgorithmParameterSpec(AliceContext.Mode mode, byte[] initializationVector) { switch (mode) { case CBC: case CTR: return new IvParameterSpec(initializationVector); case GCM: return new GCMParameterSpec(context.getGcmTagLength().bits(), initializationVector); } throw new IllegalArgumentException("Unknown mode"); }
@Override protected void engineSetParameter(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { if (params instanceof XiSM2ParameterSpec) { paramSpec = (XiSM2ParameterSpec)params; } else { throw new InvalidAlgorithmParameterException("only XiSM2ParameterSpec supported"); } }
protected void engineInit(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (params instanceof TlsPrfParameterSpec == false) { throw new InvalidAlgorithmParameterException(MSG); } this.spec = (TlsPrfParameterSpec)params; SecretKey key = spec.getSecret(); if ((key != null) && ("RAW".equals(key.getFormat()) == false)) { throw new InvalidAlgorithmParameterException( "Key encoding format must be RAW"); } }
protected void engineInit( AlgorithmParameterSpec genParamSpec, SecureRandom random) throws InvalidAlgorithmParameterException { throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for DES parameter generation."); }
protected AlgorithmParameterSpec localEngineGetParameterSpec( Class paramSpec) throws InvalidParameterSpecException { if (paramSpec == PBEParameterSpec.class) { return new PBEParameterSpec(params.getIV(), params.getIterations().intValue()); } throw new InvalidParameterSpecException("unknown parameter spec passed to PKCS12 PBE parameters object."); }
protected void engineInit( AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (!(paramSpec instanceof PBEParameterSpec)) { throw new InvalidParameterSpecException("PBEParameterSpec required to initialise a PKCS12 PBE parameters algorithm parameters object"); } PBEParameterSpec pbeSpec = (PBEParameterSpec)paramSpec; this.params = new PKCS12PBEParams(pbeSpec.getSalt(), pbeSpec.getIterationCount()); }
protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (!(paramSpec instanceof DSAParameterSpec)) { throw new InvalidParameterSpecException ("Inappropriate parameter specification"); } this.p = ((DSAParameterSpec)paramSpec).getP(); this.q = ((DSAParameterSpec)paramSpec).getQ(); this.g = ((DSAParameterSpec)paramSpec).getG(); }
protected void engineInit( AlgorithmParameterSpec genParamSpec, SecureRandom random) throws InvalidAlgorithmParameterException { throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for CAST5 parameter generation."); }