use of org.bouncycastle.crypto.engines.RSAEngine in project faf-java-server by FAForever.
the class UniqueIdServiceTest method rsaEncrypt.
private static byte[] rsaEncrypt(byte[] encryptedData, CipherParameters privateKey) throws IOException, InvalidCipherTextException {
AsymmetricBlockCipher engine = new PKCS1Encoding(new RSAEngine());
engine.init(true, privateKey);
return engine.processBlock(encryptedData, 0, encryptedData.length);
}
use of org.bouncycastle.crypto.engines.RSAEngine in project cloudbreak by hortonworks.
the class PkiUtil method generateSignature.
public static String generateSignature(String privateKeyPem, byte[] data) {
RSAKeyParameters rsaKeyParameters = CACHE.get(privateKeyPem);
if (rsaKeyParameters == null) {
try (PEMParser pEMParser = new PEMParser(new StringReader(clarifyPemKey(privateKeyPem)))) {
PEMKeyPair pemKeyPair = (PEMKeyPair) pEMParser.readObject();
KeyFactory factory = KeyFactory.getInstance("RSA");
KeySpec publicKeySpec = new X509EncodedKeySpec(pemKeyPair.getPublicKeyInfo().getEncoded());
PublicKey publicKey = factory.generatePublic(publicKeySpec);
KeySpec privateKeySpec = new PKCS8EncodedKeySpec(pemKeyPair.getPrivateKeyInfo().getEncoded());
PrivateKey privateKey = factory.generatePrivate(privateKeySpec);
KeyPair kp = new KeyPair(publicKey, privateKey);
RSAPrivateKeySpec privKeySpec = factory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
rsaKeyParameters = new RSAKeyParameters(true, privKeySpec.getModulus(), privKeySpec.getPrivateExponent());
CACHE.put(privateKeyPem, rsaKeyParameters);
} catch (NoSuchAlgorithmException | IOException | InvalidKeySpecException e) {
throw new SecurityException(e);
}
}
Signer signer = new PSSSigner(new RSAEngine(), new SHA256Digest(), SALT_LENGTH);
signer.init(true, rsaKeyParameters);
signer.update(data, 0, data.length);
try {
byte[] signature = signer.generateSignature();
return BaseEncoding.base64().encode(signature);
} catch (CryptoException e) {
throw new SecurityException(e);
}
}
use of org.bouncycastle.crypto.engines.RSAEngine in project 360-Engine-for-Android by 360.
the class RSAEncryptionUtils method rsa.
/**
* Encrypts or Decrypts bytes with the given RSA Public or Private Key.
*
* @param encrypt true for encrypt, false for decrypt.
* @param key the RSA Public or Private Key.
* @param data the data to encrypt or decrypt.
* @return the encrypted or decrypted data.
*/
private static byte[] rsa(final boolean encrypt, final RSAKeyParameters key, final byte[] data) throws InvalidCipherTextException {
final byte[] dataAligned = new byte[roundUp(data.length, ROUND_UP_VALUE)];
System.arraycopy(data, 0, dataAligned, 0, data.length);
final RSAEngine rsa = new RSAEngine();
final AsymmetricBlockCipher pkcs1 = new PKCS1Encoding(rsa);
pkcs1.init(encrypt, key);
if (encrypt)
return pkcs1.processBlock(dataAligned, 0, dataAligned.length);
return trimZeros(pkcs1.processBlock(dataAligned, 0, dataAligned.length));
}
Aggregations