public GenericKey generateUnwrappedKey(AlgorithmIdentifier encryptedKeyAlgorithm, byte[] encryptedKey) throws OperatorException { AsymmetricBlockCipher keyCipher = createAsymmetricUnwrapper(this.getAlgorithmIdentifier().getAlgorithm()); keyCipher.init(false, privateKey); try { byte[] key = keyCipher.processBlock(encryptedKey, 0, encryptedKey.length); if (encryptedKeyAlgorithm.getAlgorithm().equals(PKCSObjectIdentifiers.des_EDE3_CBC)) { return new GenericKey(encryptedKeyAlgorithm, key); } else { return new GenericKey(encryptedKeyAlgorithm, key); } } catch (InvalidCipherTextException e) { throw new OperatorException("unable to recover secret key: " + e.getMessage(), e); } }
public OAEPEncoding( AsymmetricBlockCipher cipher, Digest hash, Digest mgf1Hash, byte[] encodingParams) { this.engine = cipher; this.mgf1Hash = mgf1Hash; this.defHash = new byte[hash.getDigestSize()]; hash.reset(); if (encodingParams != null) { hash.update(encodingParams, 0, encodingParams.length); } hash.doFinal(defHash, 0); }
public PSSSigner( AsymmetricBlockCipher cipher, Digest contentDigest, Digest mgfDigest, int sLen, byte trailer) { this.cipher = cipher; this.contentDigest = contentDigest; this.mgfDigest = mgfDigest; this.hLen = contentDigest.getDigestSize(); this.mgfhLen = mgfDigest.getDigestSize(); this.sLen = sLen; this.salt = new byte[sLen]; this.mDash = new byte[8 + sLen + hLen]; this.trailer = trailer; }
protected PSSSignatureSpi( AsymmetricBlockCipher signer, PSSParameterSpec baseParamSpec, boolean isRaw) { this.signer = signer; this.originalSpec = baseParamSpec; if (baseParamSpec == null) { this.paramSpec = PSSParameterSpec.DEFAULT; } else { this.paramSpec = baseParamSpec; } this.mgfDigest = DigestFactory.getDigest(paramSpec.getDigestAlgorithm()); this.saltLength = paramSpec.getSaltLength(); this.trailer = getTrailer(paramSpec.getTrailerField()); this.isRaw = isRaw; setupContentDigest(); }
public OAEPEncoding( AsymmetricBlockCipher cipher, Digest hash, byte[] encodingParams) { this.engine = cipher; this.hash = hash; this.defHash = new byte[hash.getDigestSize()]; if (encodingParams != null) { hash.update(encodingParams, 0, encodingParams.length); } hash.doFinal(defHash, 0); }
public PSSSigner( AsymmetricBlockCipher cipher, Digest contentDigest, Digest mgfDigest, int sLen, byte trailer) { this.cipher = cipher; this.contentDigest = contentDigest; this.mgfDigest = mgfDigest; this.hLen = contentDigest.getDigestSize(); this.mgfhLen = mgfDigest.getDigestSize(); this.sSet = false; this.sLen = sLen; this.salt = new byte[sLen]; this.mDash = new byte[8 + sLen + hLen]; this.trailer = trailer; }
public PSSSigner( AsymmetricBlockCipher cipher, Digest contentDigest, Digest mgfDigest, byte[] salt, byte trailer) { this.cipher = cipher; this.contentDigest = contentDigest; this.mgfDigest = mgfDigest; this.hLen = contentDigest.getDigestSize(); this.mgfhLen = mgfDigest.getDigestSize(); this.sSet = true; this.sLen = salt.length; this.salt = salt; this.mDash = new byte[8 + sLen + hLen]; this.trailer = trailer; }
private Cipher createCipherForAsymmetricBlockCipherMode(final String transformation, final AsymmetricBlockCipher modeWithEngine, final String engineName, final String modeName, final String paddingName) throws NoSuchPaddingException { AsymmetricBlockCipher padding; if (paddingName.isEmpty() || "NOPADDING".equals(paddingName)) padding = modeWithEngine; else { padding = createAsymmetricBlockCipherPadding(paddingName, modeWithEngine); if (padding == null) throw new NoSuchPaddingException("There is no asymmetric-block-cipher-padding registered with name \"" + paddingName + "\"!"); } return new AsymmetricBlockCipherImpl( transformation, new BufferedAsymmetricBlockCipher(padding) ); }
/** * @return an RSA decryption cipher */ protected synchronized AsymmetricBlockCipher getRSADecryptCipher() { if (decodeCipher == null) { try { byte[] bytes = getEncoder().decode(privateKey); EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(bytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); PrivateKey key = keyFactory.generatePrivate(privateKeySpec); this.decodeCipher = new PKCS1Encoding(new RSABlindedEngine()); decodeCipher.init(false, generatePrivateKeyParameter((RSAPrivateKey) key)); } catch (Exception e) { throw new RuntimeException("Error constructing Cipher: ", e); } } return decodeCipher; }
/** * @return */ protected synchronized AsymmetricBlockCipher getRSAEncryptCipher() { if (encodeCipher == null) { try { byte[] bytes = getEncoder().decode(publicKey); EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(bytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); PublicKey key = keyFactory.generatePublic(publicKeySpec); this.encodeCipher = new PKCS1Encoding(new RSABlindedEngine()); encodeCipher.init(true, generatePublicKeyParameter((RSAPublicKey) key)); } catch (Exception e) { throw new RuntimeException("Error constructing Cipher: ", e); } } return encodeCipher; }
protected byte[] encryptSessionInfo(PGPPublicKey pubKey, byte[] sessionInfo) throws PGPException { try { AsymmetricBlockCipher c = BcImplProvider.createPublicKeyCipher(pubKey.getAlgorithm()); AsymmetricKeyParameter key = keyConverter.getPublicKey(pubKey); if (random == null) { random = new SecureRandom(); } c.init(true, new ParametersWithRandom(key, random)); return c.processBlock(sessionInfo, 0, sessionInfo.length); } catch (InvalidCipherTextException e) { throw new PGPException("exception encrypting session info: " + e.getMessage(), e); } }
private byte[] doOperation(byte[] input, boolean isEncrypt) { AsymmetricBlockCipher cipher = new OAEPEncoding(new RSAEngine(), new SHA256Digest(), new SHA1Digest(), null); RSAKeyParameters key = isEncrypt ? publicKey : privateKey; cipher.init(isEncrypt, key); try { return cipher.processBlock(input, 0, input.length); } catch (InvalidCipherTextException e) { throw new EncryptionException("Encryption fails", e); } }
public byte[] generateWrappedKey(GenericKey encryptionKey) throws OperatorException { AsymmetricBlockCipher keyEncryptionCipher = createAsymmetricWrapper(getAlgorithmIdentifier().getAlgorithm()); CipherParameters params = publicKey; if (random != null) { params = new ParametersWithRandom(params, random); } try { byte[] keyEnc = OperatorUtils.getKeyBytes(encryptionKey); keyEncryptionCipher.init(true, publicKey); return keyEncryptionCipher.processBlock(keyEnc, 0, keyEnc.length); } catch (InvalidCipherTextException e) { throw new OperatorException("unable to encrypt contents key", e); } }
protected PSSSignatureSpi( String name, AsymmetricBlockCipher signer, Digest digest) { super(name); this.signer = signer; this.mgfDigest = digest; if (digest != null) { this.saltLength = digest.getDigestSize(); } else { this.saltLength = 20; } this.isRaw = false; setupContentDigest(); }
protected PSSSignatureSpi( String name, AsymmetricBlockCipher signer, Digest digest, boolean isRaw) { super(name); this.signer = signer; this.mgfDigest = digest; if (digest != null) { this.saltLength = digest.getDigestSize(); } else { this.saltLength = 20; } this.isRaw = isRaw; setupContentDigest(); }