AnonymizeAES.cxx
#include "gdcmAES.h"
#include "gdcmReader.h"
#include "gdcmImage.h"
#include "gdcmWriter.h"
#include "gdcmAttribute.h"
#include "gdcmImageWriter.h"
#include "gdcmSequenceOfItems.h"
#include "gdcmAnonymizer.h"
#include "gdcmExplicitDataElement.h"
#include "gdcmSwapper.h"
#include "gdcmUIDGenerator.h"
#include "gdcmFileExplicitFilter.h"
#include <iostream>
#include <fstream>
int main(int argc, char *argv[])
{
if( argc < 3 )
{
std::cerr << argv[0] << " input.dcm output.dcm" << std::endl;
return 1;
}
const char *filename = argv[1];
const char *outfilename = argv[2];
gdcm::Reader reader;
reader.SetFileName( filename );
if( !reader.Read() )
{
std::cerr << "Could not read: " << filename << std::endl;
return 1;
}
gdcm::File &file = reader.GetFile();
gdcm::DataSet &ds = file.GetDataSet();
gdcm::Tag tag1(0x10,0x10);
gdcm::Tag tag2(0x10,0x20);
gdcm::SmartPointer<gdcm::SequenceOfItems> sq = new gdcm::SequenceOfItems();
sq->SetLengthToUndefined();
gdcm::Item it;
it.SetVLToUndefined();
gdcm::DataSet &encryptedds = it.GetNestedDataSet();
encryptedds.Insert( ds.GetDataElement( tag1 ) );
encryptedds.Insert( ds.GetDataElement( tag2 ) );
sq->AddItem(it);
gdcm::DataElement des( gdcm::Tag(0x0400,0x0550) );
des.SetVR(gdcm::VR::SQ);
des.SetValue(*sq);
des.SetVLToUndefined();
std::ostringstream os;
std::cout << des << std::endl;
des.Write<gdcm::ExplicitDataElement,gdcm::SwapperNoOp>(os);
std::string encrypted_str = os.str();
std::cout << "Size:" << encrypted_str.size() << std::endl;
size_t encrypted_len = encrypted_str.size();
if( encrypted_len % 16 != 0 )
{
encrypted_len = ((encrypted_len / 16) + 1) * 16;
}
char *buf = new char[ encrypted_len ];
memset( buf, 0, encrypted_len );
memcpy( buf, encrypted_str.c_str(), encrypted_str.size() );
char key[32] = {};
gdcm::AES aes;
if( !aes.SetkeyEnc( key, 128 ) ) return 1;
char iv[16] = {};
aes.CryptCbc( gdcm::AES::ENCRYPT, encrypted_len, iv, buf, buf );
{
gdcm::SmartPointer<gdcm::SequenceOfItems> sq = new gdcm::SequenceOfItems();
sq->SetLengthToUndefined();
gdcm::TransferSyntax encrypted_ts = gdcm::TransferSyntax::ExplicitVRLittleEndian;
gdcm::DataElement encrypted_ts_de( gdcm::Tag(0x400,0x510) );
encrypted_ts_de.SetByteValue( encrypted_ts.GetString(), strlen(encrypted_ts.GetString()) );
gdcm::DataElement encrypted_de( gdcm::Tag(0x400,0x520) );
encrypted_de.SetByteValue( (char*)buf, encrypted_len );
delete[] buf;
gdcm::Item it;
it.SetVLToUndefined();
gdcm::DataSet &nds = it.GetNestedDataSet();
nds.Insert(encrypted_ts_de);
nds.Insert(encrypted_de);
sq->AddItem(it);
gdcm::DataElement des( gdcm::Tag(0x0400,0x0500) );
des.SetVR(gdcm::VR::SQ);
des.SetValue(*sq);
des.SetVLToUndefined();
ds.Insert(des);
}
gdcm::Anonymizer ano;
ano.SetFile( reader.GetFile() );
ano.Replace( tag1, "D'oh! Patient Name is in Encrypted Content");
ano.Replace( tag2, "-1" );
ano.RemoveGroupLength();
ano.RemovePrivateTags();
ano.Replace( gdcm::Tag(0x0012,0x0062), "YES");
ano.Replace( gdcm::Tag(0x0012,0x0063), "BASIC APPLICATION LEVEL CONFIDENTIALITY PROFILE");
gdcm::FileMetaInformation::SetSourceApplicationEntityTitle( "AnonymizeAES" );
gdcm::FileMetaInformation &fmi = file.GetHeader();
fmi.Remove( gdcm::Tag(0x0002,0x0012) );
fmi.Remove( gdcm::Tag(0x0002,0x0013) );
{
gdcm::DataElement de( gdcm::Tag(0x008,0x0018) );
gdcm::UIDGenerator uid;
std::string instance_uid = uid.Generate();
de.SetByteValue( instance_uid.c_str(), instance_uid.size() );
de.SetVR( gdcm::Attribute<0x008, 0x0018>::GetVR() );
ds.Replace( de );
}
gdcm::FileExplicitFilter fef;
fef.SetFile( reader.GetFile() );
if(!fef.Change())
{
std::cerr << "Failed to change: " << filename << std::endl;
return 1;
}
gdcm::Writer writer;
writer.SetFile( reader.GetFile() );
writer.SetFileName( outfilename );
if( !writer.Write() )
{
return 1;
}
return 0;
}