Refine search
AlgorithmParameterSpec aps; if (cipherAlgorithm == CipherAlgorithm.rc2) { aps = new RC2ParameterSpec(key.getEncoded().length*8, vec); } else { aps = new IvParameterSpec(vec);
protected void engineInit( AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (paramSpec instanceof IvParameterSpec) { this.iv = ((IvParameterSpec)paramSpec).getIV(); } else if (paramSpec instanceof RC2ParameterSpec) { int effKeyBits = ((RC2ParameterSpec)paramSpec).getEffectiveKeyBits(); if (effKeyBits != -1) { if (effKeyBits < 256) { parameterVersion = table[effKeyBits]; } else { parameterVersion = effKeyBits; } } this.iv = ((RC2ParameterSpec)paramSpec).getIV(); } else { throw new InvalidParameterSpecException("IvParameterSpec or RC2ParameterSpec required to initialise a RC2 parameters algorithm parameters object"); } }
/** {@inheritDoc} */ @Override protected AlgorithmParameterSpec getAlgorithmParameterSpec() { final AlgorithmParameterSpec spec; if (paramSpec != null) { spec = paramSpec; } else { // Use the number of bits in the key as effective bits // if it has not been explicitly set final int effective = effectiveKeyBits < 0 ? this.key.getEncoded().length * 8 : effectiveKeyBits; if (iv != null) { spec = new RC2ParameterSpec(effective, iv); } else { spec = new RC2ParameterSpec(effective); } } return spec; } }
protected void engineInit( AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (paramSpec instanceof IvParameterSpec) { this.iv = ((IvParameterSpec)paramSpec).getIV(); } else if (paramSpec instanceof RC2ParameterSpec) { int effKeyBits = ((RC2ParameterSpec)paramSpec).getEffectiveKeyBits(); if (effKeyBits != -1) { if (effKeyBits < 256) { parameterVersion = table[effKeyBits]; } else { parameterVersion = effKeyBits; } } this.iv = ((RC2ParameterSpec)paramSpec).getIV(); } else { throw new InvalidParameterSpecException("IvParameterSpec or RC2ParameterSpec required to initialise a RC2 parameters algorithm parameters object"); } }
byte[] iv = "87654321".getBytes("US-ASCII"); byte[] key = "SampleKey".getBytes("US-ASCII"); byte[] data = new byte[30]; // for example SecretKeySpec skeySpec = new SecretKeySpec(key, "RC2"); RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.length * 8, iv); Cipher cipher = Cipher.getInstance("RC2/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec); byte[] encrypted = cipher.doFinal(data); // ....
AlgorithmParameterSpec aps; if (cipherAlgorithm == CipherAlgorithm.rc2) { aps = new RC2ParameterSpec(key.getEncoded().length*8, vec); } else { aps = new IvParameterSpec(vec);
protected void engineInit( AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (paramSpec instanceof IvParameterSpec) { this.iv = ((IvParameterSpec)paramSpec).getIV(); } else if (paramSpec instanceof RC2ParameterSpec) { int effKeyBits = ((RC2ParameterSpec)paramSpec).getEffectiveKeyBits(); if (effKeyBits != -1) { if (effKeyBits < 256) { parameterVersion = table[effKeyBits]; } else { parameterVersion = effKeyBits; } } this.iv = ((RC2ParameterSpec)paramSpec).getIV(); } else { throw new InvalidParameterSpecException("IvParameterSpec or RC2ParameterSpec required to initialise a RC2 parameters algorithm parameters object"); } }
public static String encrypt(byte[] key, byte[] iv, String unencrypted) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{ RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.length*8, iv); Cipher cipher = Cipher.getInstance("RC2/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "RC2"), ivSpec); byte[] encrypted = cipher.doFinal(unencrypted.getBytes()); return DatatypeConverter.printBase64Binary(encrypted); } public static String decrypt(byte[] key, byte[] iv, String encrypted) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{ RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.length*8, iv); Cipher cipher = Cipher.getInstance("RC2/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "RC2"), ivSpec); byte[] decrypted = cipher.doFinal(DatatypeConverter.parseBase64Binary(encrypted)); return new String(decrypted); }
protected static Cipher initCipherForBlock(Cipher existing, int block, boolean lastChunk, EncryptionInfo encryptionInfo, SecretKey skey, int encryptionMode) throws GeneralSecurityException { EncryptionHeader header = encryptionInfo.getHeader(); String padding = (lastChunk ? "PKCS5Padding" : "NoPadding"); if (existing == null || !existing.getAlgorithm().endsWith(padding)) { existing = getCipher(skey, header.getCipherAlgorithm(), header.getChainingMode(), header.getKeySalt(), encryptionMode, padding); } byte[] blockKey = new byte[4]; LittleEndian.putInt(blockKey, 0, block); byte[] iv = generateIv(header.getHashAlgorithm(), header.getKeySalt(), blockKey, header.getBlockSize()); AlgorithmParameterSpec aps; if (header.getCipherAlgorithm() == CipherAlgorithm.rc2) { aps = new RC2ParameterSpec(skey.getEncoded().length*8, iv); } else { aps = new IvParameterSpec(iv); } existing.init(encryptionMode, skey, aps); return existing; }
protected void engineInit( AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (paramSpec instanceof IvParameterSpec) { this.iv = ((IvParameterSpec)paramSpec).getIV(); } else if (paramSpec instanceof RC2ParameterSpec) { int effKeyBits = ((RC2ParameterSpec)paramSpec).getEffectiveKeyBits(); if (effKeyBits != -1) { if (effKeyBits < 256) { parameterVersion = table[effKeyBits]; } else { parameterVersion = effKeyBits; } } this.iv = ((RC2ParameterSpec)paramSpec).getIV(); } else { throw new InvalidParameterSpecException("IvParameterSpec or RC2ParameterSpec required to initialise a RC2 parameters algorithm parameters object"); } }
public static String encryptRC2(String key, String initVector, String value) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.getBytes(UTF_8).length, initVector.getBytes(UTF_8)); Cipher cipher = Cipher.getInstance(CIPHER_RC2_INSTANCE); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(UTF_8), RC2), ivSpec); byte[] encrypted = cipher.doFinal(value.getBytes(UTF_8)); return Base64.getEncoder().encodeToString(encrypted); }
protected static Cipher initCipherForBlock(Cipher existing, int block, boolean lastChunk, EncryptionInfoBuilder builder, SecretKey skey, int encryptionMode) throws GeneralSecurityException { EncryptionHeader header = builder.getHeader(); if (existing == null || lastChunk) { String padding = (lastChunk ? "PKCS5Padding" : "NoPadding"); existing = getCipher(skey, header.getCipherAlgorithm(), header.getChainingMode(), header.getKeySalt(), encryptionMode, padding); } byte[] blockKey = new byte[4]; LittleEndian.putInt(blockKey, 0, block); byte[] iv = generateIv(header.getHashAlgorithmEx(), header.getKeySalt(), blockKey, header.getBlockSize()); AlgorithmParameterSpec aps; if (header.getCipherAlgorithm() == CipherAlgorithm.rc2) { aps = new RC2ParameterSpec(skey.getEncoded().length*8, iv); } else { aps = new IvParameterSpec(iv); } existing.init(encryptionMode, skey, aps); return existing; }
protected void engineInit( AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (paramSpec instanceof IvParameterSpec) { this.iv = ((IvParameterSpec)paramSpec).getIV(); } else if (paramSpec instanceof RC2ParameterSpec) { int effKeyBits = ((RC2ParameterSpec)paramSpec).getEffectiveKeyBits(); if (effKeyBits != -1) { if (effKeyBits < 256) { parameterVersion = table[effKeyBits]; } else { parameterVersion = effKeyBits; } } this.iv = ((RC2ParameterSpec)paramSpec).getIV(); } else { throw new InvalidParameterSpecException("IvParameterSpec or RC2ParameterSpec required to initialise a RC2 parameters algorithm parameters object"); } }
public static String decryptRC2(String key, String initVector, String encrypted) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.getBytes(UTF_8).length, initVector.getBytes(UTF_8)); Cipher cipher = Cipher.getInstance(CIPHER_RC2_INSTANCE); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(UTF_8), RC2), ivSpec); byte[] original = cipher.doFinal(Base64.getDecoder().decode(encrypted)); return new String(original); }
protected AlgorithmParameterSpec localEngineGetParameterSpec( Class paramSpec) throws InvalidParameterSpecException { if (paramSpec == RC2ParameterSpec.class) { if (parameterVersion != -1) { if (parameterVersion < 256) { return new RC2ParameterSpec(ekb[parameterVersion], iv); } else { return new RC2ParameterSpec(parameterVersion, iv); } } } if (paramSpec == IvParameterSpec.class) { return new IvParameterSpec(iv); } throw new InvalidParameterSpecException("unknown parameter spec passed to RC2 parameters object."); }
public AlgorithmIdentifier getAlgorithmIdentifier(ASN1ObjectIdentifier macOID, AlgorithmParameterSpec paramSpec) { if (paramSpec instanceof IvParameterSpec) { return new AlgorithmIdentifier(macOID, new DEROctetString(((IvParameterSpec)paramSpec).getIV())); } if (paramSpec instanceof RC2ParameterSpec) { RC2ParameterSpec rc2Spec = (RC2ParameterSpec)paramSpec; int effKeyBits = ((RC2ParameterSpec)paramSpec).getEffectiveKeyBits(); if (effKeyBits != -1) { int parameterVersion; if (effKeyBits < 256) { parameterVersion = rc2Table[effKeyBits]; } else { parameterVersion = effKeyBits; } return new AlgorithmIdentifier(macOID, new RC2CBCParameter(parameterVersion, rc2Spec.getIV())); } return new AlgorithmIdentifier(macOID, new RC2CBCParameter(rc2Spec.getIV())); } throw new IllegalStateException("unknown parameter spec: " + paramSpec); }
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; } } }
protected AlgorithmParameterSpec localEngineGetParameterSpec( Class paramSpec) throws InvalidParameterSpecException { if (paramSpec == RC2ParameterSpec.class || paramSpec == AlgorithmParameterSpec.class) { if (parameterVersion != -1) { if (parameterVersion < 256) { return new RC2ParameterSpec(ekb[parameterVersion], iv); } else { return new RC2ParameterSpec(parameterVersion, iv); } } } if (paramSpec == IvParameterSpec.class || paramSpec == AlgorithmParameterSpec.class) { return new IvParameterSpec(iv); } throw new InvalidParameterSpecException("unknown parameter spec passed to RC2 parameters object."); }
public AlgorithmIdentifier getAlgorithmIdentifier(ASN1ObjectIdentifier macOID, AlgorithmParameterSpec paramSpec) { if (paramSpec instanceof IvParameterSpec) { return new AlgorithmIdentifier(macOID, new DEROctetString(((IvParameterSpec)paramSpec).getIV())); } if (paramSpec instanceof RC2ParameterSpec) { RC2ParameterSpec rc2Spec = (RC2ParameterSpec)paramSpec; int effKeyBits = ((RC2ParameterSpec)paramSpec).getEffectiveKeyBits(); if (effKeyBits != -1) { int parameterVersion; if (effKeyBits < 256) { parameterVersion = rc2Table[effKeyBits]; } else { parameterVersion = effKeyBits; } return new AlgorithmIdentifier(macOID, new RC2CBCParameter(parameterVersion, rc2Spec.getIV())); } return new AlgorithmIdentifier(macOID, new RC2CBCParameter(rc2Spec.getIV())); } throw new IllegalStateException("unknown parameter spec: " + paramSpec); }
AlgorithmParameters generateParameters(ASN1ObjectIdentifier encryptionOID, SecretKey encKey, SecureRandom rand) throws CRMFException { try { AlgorithmParameterGenerator pGen = createAlgorithmParameterGenerator(encryptionOID); if (encryptionOID.equals(CMSAlgorithm.RC2_CBC)) { byte[] iv = new byte[8]; rand.nextBytes(iv); try { pGen.init(new RC2ParameterSpec(encKey.getEncoded().length * 8, iv), rand); } catch (InvalidAlgorithmParameterException e) { throw new CRMFException("parameters generation error: " + e, e); } } return pGen.generateParameters(); } catch (NoSuchAlgorithmException e) { return null; } catch (GeneralSecurityException e) { throw new CRMFException("exception creating algorithm parameter generator: " + e, e); } }