List of usage examples for org.bouncycastle.crypto.engines RSABlindedEngine RSABlindedEngine
RSABlindedEngine
From source file:bluecrystal.service.service.SignVerifyService.java
License:Open Source License
public boolean verify(int hashId, byte[] contentHash, byte[] sigBytes, X509Certificate cert) throws Exception { RSAPublicKey pubK = (RSAPublicKey) cert.getPublicKey(); CipherParameters param = new RSAKeyParameters(false, pubK.getModulus(), pubK.getPublicExponent()); RSABlindedEngine cipher2 = new RSABlindedEngine(); cipher2.init(false, param);//from w w w. ja va 2 s . c o m AsymmetricBlockCipher cipher = new PKCS1Encoding(cipher2); byte[] sig = cipher.processBlock(sigBytes, 0, sigBytes.length); AlgorithmIdentifier algId = createAlgorithm(hashId); byte[] expected = derEncode(contentHash, algId); LOG.debug("Sig:(" + sigBytes.length + ")" + Utils.conv(sigBytes)); LOG.debug("Has:(" + contentHash.length + ")" + Utils.conv(contentHash)); LOG.debug("Sig:(" + sig.length + ")" + Utils.conv(sig)); LOG.debug("Exp:(" + expected.length + ")" + Utils.conv(expected)); if (sig.length == expected.length) { for (int i = 0; i < sig.length; i++) { if (sig[i] != expected[i]) { return false; } } } else if (sig.length == expected.length - 2) // NULL left out { int sigOffset = sig.length - contentHash.length - 2; int expectedOffset = expected.length - contentHash.length - 2; expected[1] -= 2; // adjust lengths expected[3] -= 2; for (int i = 0; i < contentHash.length; i++) { if (sig[sigOffset + i] != expected[expectedOffset + i]) // check hash { return false; } } for (int i = 0; i < sigOffset; i++) { if (sig[i] != expected[i]) // check header less NULL { return false; } } } else { return false; } return true; }
From source file:com.geoxp.oss.CryptoHelper.java
License:Apache License
/** * Encrypt data using RSA.//from w w w. java 2 s.com * CAUTION: this can take a while on large data * * @param key RSA key to use for encryption * @param data Cleartext data * @return The ciphertext data or null if an error occured */ public static byte[] encryptRSA(Key key, byte[] data) { // // Get an RSA Cipher instance // //Cipher rsa = null; try { /* The following commented code can be used the BouncyCastle * JCE provider signature is intact, which is not the * case when BC has been repackaged using jarjar rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); rsa.init (Cipher.ENCRYPT_MODE, key, CryptoHelper.sr); return rsa.doFinal(data); */ AsymmetricBlockCipher c = new PKCS1Encoding(new RSABlindedEngine()); if (key instanceof RSAPublicKey) { c.init(true, new RSAKeyParameters(true, ((RSAPublicKey) key).getModulus(), ((RSAPublicKey) key).getPublicExponent())); } else if (key instanceof RSAPrivateKey) { c.init(true, new RSAKeyParameters(true, ((RSAPrivateKey) key).getModulus(), ((RSAPrivateKey) key).getPrivateExponent())); } else { return null; } int insize = c.getInputBlockSize(); int offset = 0; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while (offset < data.length) { int len = Math.min(insize, data.length - offset); baos.write(c.processBlock(data, offset, len)); offset += len; } return baos.toByteArray(); /* } catch (NoSuchProviderException nspe) { return null; } catch (NoSuchPaddingException nspe) { return null; } catch (NoSuchAlgorithmException nsae) { return null; } catch (InvalidKeyException ike) { return null; } catch (BadPaddingException bpe) { return null; } catch (IllegalBlockSizeException ibse) { return null; } */ } catch (InvalidCipherTextException icte) { return null; } catch (IOException ioe) { return null; } }
From source file:com.geoxp.oss.CryptoHelper.java
License:Apache License
/** * Decrypt data previously encrypted with RSA * @param key RSA key to use for decryption * @param data Ciphertext data/*from w w w.j av a 2 s.c o m*/ * @return The cleartext data or null if an error occurred */ public static byte[] decryptRSA(Key key, byte[] data) { // // Get an RSA Cipher instance // //Cipher rsa = null; try { /* The following commented code can be used the BouncyCastle * JCE provider signature is intact, which is not the * case when BC has been repackaged using jarjar rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); rsa.init (Cipher.DECRYPT_MODE, key, CryptoHelper.sr); return rsa.doFinal(data); */ AsymmetricBlockCipher c = new PKCS1Encoding(new RSABlindedEngine()); if (key instanceof RSAPublicKey) { c.init(false, new RSAKeyParameters(true, ((RSAPublicKey) key).getModulus(), ((RSAPublicKey) key).getPublicExponent())); } else if (key instanceof RSAPrivateKey) { c.init(false, new RSAKeyParameters(true, ((RSAPrivateKey) key).getModulus(), ((RSAPrivateKey) key).getPrivateExponent())); } else { return null; } int insize = c.getInputBlockSize(); int offset = 0; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while (offset < data.length) { int len = Math.min(insize, data.length - offset); baos.write(c.processBlock(data, offset, len)); offset += len; } return baos.toByteArray(); /* } catch (NoSuchProviderException nspe) { return null; } catch (NoSuchPaddingException nspe) { return null; } catch (NoSuchAlgorithmException nsae) { return null; } catch (InvalidKeyException ike) { return null; } catch (BadPaddingException bpe) { return null; } catch (IllegalBlockSizeException ibse) { return null; } */ } catch (InvalidCipherTextException icte) { return null; } catch (IOException ioe) { return null; } }
From source file:com.github.chuckbuckethead.cypher.keys.RSAKey.java
License:Open Source License
/** * @return an RSA decryption cipher//from w ww . j av a 2 s. c o m */ 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; }
From source file:com.github.chuckbuckethead.cypher.keys.RSAKey.java
License:Open Source License
/** * @return//from www. ja va2 s . com */ 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; }
From source file:edu.biu.scapi.midLayer.asymmetricCrypto.digitalSignature.BcRSAPss.java
License:Open Source License
private void createBCSigner(String hashName, SecureRandom random) throws FactoriesException { //Creates BC digest with the given name. digest = BCFactory.getInstance().getDigest(hashName); this.random = random; RSABlindedEngine rsa = new RSABlindedEngine(); signer = new PSSSigner(rsa, digest, digest.getDigestSize()); }
From source file:edu.biu.scapi.midLayer.asymmetricCrypto.encryption.BcRSAOaep.java
License:Open Source License
/** * Constructor that lets the user choose the source of randomness. * @param random source of randomness.// w w w . j a va 2 s.com */ public BcRSAOaep(SecureRandom random) { this.random = random; //Creates the OAEP encoding with RSABlindedEngine of BC. this.bcBlockCipher = new OAEPEncoding(new RSABlindedEngine()); }
From source file:org.fnppl.opensdx.security.PrivateKey.java
License:Open Source License
public byte[] decrypt(byte[] data) throws Exception { RSABlindedEngine rsae = new RSABlindedEngine(); OAEPEncoding oaep = new OAEPEncoding(rsae); oaep.init(false, //fr encrypt: true // bp priv);/*from w ww. j a v a 2s . c om*/ if (data.length > rsae.getInputBlockSize()) { throw new RuntimeException("PrivateKey.encrypt::data.length(" + data.length + ") too long - max is: " + rsae.getInputBlockSize()); } return oaep.processBlock(data, 0, data.length); }
From source file:org.fnppl.opensdx.security.PrivateKey.java
License:Open Source License
public byte[] encrypt(byte[] data) throws Exception { RSABlindedEngine rsae = new RSABlindedEngine(); // RSABlindingEngine rsae = new RSABlindingEngine(); // //from www .j a va 2 s.co m // RSABlindingParameters bp = new RSABlindingParameters( // priv, // PublicKey.generateBlindingFactor(pubkey) // ); OAEPEncoding oaep = new OAEPEncoding(rsae); oaep.init(true, //fr encrypt: true // bp priv); if (data.length > rsae.getInputBlockSize()) { throw new RuntimeException("PrivateKey.encrypt::data.length(" + data.length + ") too long - max is: " + rsae.getInputBlockSize()); } return oaep.processBlock(data, 0, data.length); }
From source file:org.fnppl.opensdx.security.PublicKey.java
License:Open Source License
public byte[] encrypt(byte[] data) throws Exception { // RSABlindingEngine rsae = new RSABlindingEngine(); RSABlindedEngine rsab = new RSABlindedEngine(); // RSABlindingParameters bp = new RSABlindingParameters( // pub, // generateBlindingFactor(pub) // ); // OAEPEncoding oaep = new OAEPEncoding(rsae); OAEPEncoding oaep = new OAEPEncoding(rsab); oaep.init(true, //fr encrypt: true pub//from w w w . java 2 s. c om // bp ); if (data.length > rsab.getInputBlockSize()) { throw new RuntimeException("PublicKey.encrypt::data.length(" + data.length + ") too long - max is: " + oaep.getInputBlockSize()); } return oaep.processBlock(data, 0, data.length); }