public static BlobEncryptionData deserialize(String inputData) throws JsonProcessingException, IOException { JsonParser parser = Utility.getJsonParser(inputData); BlobEncryptionData data = new BlobEncryptionData(); try { if (!parser.hasCurrentToken()) { data.setEncryptionMode(parser.getValueAsString()); data.setWrappedContentKey(WrappedContentKey.deserialize(parser)); data.setEncryptionAgent(EncryptionAgent.deserialize(parser)); data.setContentEncryptionIV(parser.getBinaryValue()); data.setKeyWrappingMetadata(deserializeKeyWrappingMetadata(parser)); consumeJsonObject(parser);
myAes.init(Cipher.ENCRYPT_MODE, aesKey); BlobEncryptionData encryptionData = new BlobEncryptionData(); if (encryptionData.getKeyWrappingMetadata() == null) { encryptionData.setKeyWrappingMetadata(new HashMap<String, String>()); encryptionData.getKeyWrappingMetadata().put(Constants.EncryptionConstants.ENCRYPTION_LIBRARY, "Java " + Constants.HeaderConstants.USER_AGENT_VERSION); encryptionData.setEncryptionAgent(new EncryptionAgent(Constants.EncryptionConstants.ENCRYPTION_PROTOCOL_V1, EncryptionAlgorithm.AES_CBC_256)); encryptionData.setWrappedContentKey(new WrappedContentKey(this.keyWrapper.getKid(), encryptedKey.getKey(), encryptedKey.getValue())); encryptionData.setContentEncryptionIV(myAes.getIV()); metadata.put(Constants.EncryptionConstants.BLOB_ENCRYPTION_DATA, encryptionData.serialize()); return myAes;
BlobEncryptionData encryptionData = BlobEncryptionData.deserialize(encryptionDataString); Utility.assertNotNull("contentEncryptionIV", encryptionData.getContentEncryptionIV()); Utility.assertNotNull("encryptedKey", encryptionData.getWrappedContentKey().getEncryptedKey()); if (!Constants.EncryptionConstants.ENCRYPTION_PROTOCOL_V1.equals(encryptionData.getEncryptionAgent() .getProtocol())) { throw new StorageException(StorageErrorCodeStrings.DECRYPTION_ERROR, IKey keyEncryptionKey = this.keyResolver.resolveKeyAsync(encryptionData.getWrappedContentKey() .getKeyId()).get(); encryptionData.getWrappedContentKey().getEncryptedKey(), encryptionData.getWrappedContentKey().getAlgorithm()).get(); if (encryptionData.getWrappedContentKey().getKeyId().equals(this.keyWrapper.getKid())) { contentEncryptionKey = this.keyWrapper.unwrapKeyAsync( encryptionData.getWrappedContentKey().getEncryptedKey(), encryptionData.getWrappedContentKey().getAlgorithm()).get(); switch (encryptionData.getEncryptionAgent().getEncryptionAlgorithm()) { case AES_CBC_256: : encryptionData.getContentEncryptionIV()); SecretKey keySpec = new SecretKeySpec(contentEncryptionKey, 0, contentEncryptionKey.length, "AES");
public String serialize() throws IOException { final StringWriter strWriter = new StringWriter(); JsonGenerator generator = Utility.getJsonGenerator(strWriter); try { // start object generator.writeStartObject(); // write the encryption mode generator.writeStringField(Constants.EncryptionConstants.ENCRYPTION_MODE, Constants.EncryptionConstants.FULL_BLOB); // write the encryption data this.serialize(generator); // end object generator.writeEndObject(); } finally { generator.close(); } return strWriter.toString(); }