use of com.tencentcloudapi.kms.v20190118.models.EncryptRequest in project cos-java-sdk-v5 by tencentyun.
the class ContentCryptoMaterial method encryptIV.
public static byte[] encryptIV(byte[] iv, EncryptionMaterials materials, COSKeyWrapScheme kwScheme, SecureRandom srand, Provider p, QCLOUDKMS kms, CosServiceRequest req) {
if (materials.isKMSEnabled()) {
Map<String, String> matdesc = mergeMaterialDescriptions(materials, req);
EncryptRequest encryptRequest = new EncryptRequest();
try {
ObjectMapper mapper = new ObjectMapper();
encryptRequest.setEncryptionContext(mapper.writeValueAsString(matdesc));
} catch (JsonProcessingException e) {
throw new CosClientException("encrypt request set encryption context got json processing exception", e);
}
encryptRequest.setKeyId(materials.getCustomerMasterKeyId());
encryptRequest.setPlaintext(Base64.encodeAsString(iv));
EncryptResponse encryptResponse = kms.encrypt(encryptRequest);
String cipherIV = encryptResponse.getCiphertextBlob();
return cipherIV.getBytes(Charset.forName("UTF-8"));
}
Key kek;
if (materials.getKeyPair() != null) {
// Do envelope encryption with public key from key pair
kek = materials.getKeyPair().getPublic();
} else {
// Do envelope encryption with symmetric key
kek = materials.getSymmetricKey();
}
String keyWrapAlgo = kwScheme.getKeyWrapAlgorithm(kek);
try {
Cipher cipher = p == null ? Cipher.getInstance(keyWrapAlgo) : Cipher.getInstance(keyWrapAlgo, p);
cipher.init(Cipher.ENCRYPT_MODE, kek, srand);
return cipher.doFinal(iv);
} catch (Exception e) {
throw new CosClientException("Unable to encrypt IV", e);
}
}
use of com.tencentcloudapi.kms.v20190118.models.EncryptRequest in project cos-java-sdk-v5 by tencentyun.
the class ContentCryptoMaterial method secureCEK.
/**
* Secure the given CEK. Note network calls are involved if the CEK is to be protected by KMS.
*
* @param cek content encrypting key to be secured
* @param materials used to provide the key-encryption-key (KEK); or if it is KMS-enabled, the
* customer master key id and material description.
* @param contentCryptoScheme the content crypto scheme
* @param p optional security provider; can be null if the default is used.
* @return a secured CEK in the form of ciphertext or ciphertext blob.
*/
private static SecuredCEK secureCEK(SecretKey cek, EncryptionMaterials materials, COSKeyWrapScheme kwScheme, SecureRandom srand, Provider p, QCLOUDKMS kms, CosServiceRequest req) {
final Map<String, String> matdesc;
if (materials.isKMSEnabled()) {
matdesc = mergeMaterialDescriptions(materials, req);
EncryptRequest encryptRequest = new EncryptRequest();
try {
ObjectMapper mapper = new ObjectMapper();
encryptRequest.setEncryptionContext(mapper.writeValueAsString(matdesc));
} catch (JsonProcessingException e) {
throw new CosClientException("encrypt request set encryption context got json processing exception", e);
}
encryptRequest.setKeyId(materials.getCustomerMasterKeyId());
encryptRequest.setPlaintext(cek.getEncoded().toString());
EncryptResponse encryptResponse = kms.encrypt(encryptRequest);
byte[] keyBlob = encryptResponse.getCiphertextBlob().getBytes();
return new KMSSecuredCEK(keyBlob, matdesc);
} else {
matdesc = materials.getMaterialsDescription();
}
Key kek;
if (materials.getKeyPair() != null) {
// Do envelope encryption with public key from key pair
kek = materials.getKeyPair().getPublic();
} else {
// Do envelope encryption with symmetric key
kek = materials.getSymmetricKey();
}
String keyWrapAlgo = kwScheme.getKeyWrapAlgorithm(kek);
try {
Cipher cipher = p == null ? Cipher.getInstance(keyWrapAlgo) : Cipher.getInstance(keyWrapAlgo, p);
cipher.init(Cipher.WRAP_MODE, kek, srand);
return new SecuredCEK(cipher.wrap(cek), keyWrapAlgo, matdesc);
} catch (Exception e) {
throw new CosClientException("Unable to encrypt symmetric key", e);
}
}
Aggregations