Files
duplicati/Duplicati/Library/Encryption/AESEncryption.cs
T

96 lines
3.4 KiB
C#
Raw Normal View History

2009-01-14 17:29:30 +00:00
#region Disclaimer / License
2011-05-09 19:52:28 +00:00
// Copyright (C) 2011, Kenneth Skovhede
2009-01-14 17:29:30 +00:00
// http://www.hexad.dk, opensource@hexad.dk
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
2010-06-19 21:08:21 +00:00
using Duplicati.Library.Interface;
using System.IO;
using System.Security.Cryptography;
2009-01-14 17:29:30 +00:00
namespace Duplicati.Library.Encryption
{
/// <summary>
/// Implements AES encryption
/// </summary>
2013-05-05 17:07:24 +02:00
public class AESEncryption : EncryptionBase
2009-01-14 17:29:30 +00:00
{
2010-06-19 21:08:21 +00:00
/// <summary>
/// The key used to encrypt the data
/// </summary>
private string m_key;
/// <summary>
/// Default constructor, used to read file extension and supported commands
/// </summary>
public AESEncryption()
{
}
2009-01-14 17:29:30 +00:00
/// <summary>
/// Constructs a new AES encryption/decyption instance
/// </summary>
/// <param name="key">The key used for encryption. The key gets stretched through SHA hashing to fit the key size requirements</param>
2010-06-19 21:08:21 +00:00
public AESEncryption(string passphrase, Dictionary<string, string> options)
2009-01-14 17:29:30 +00:00
{
if(string.IsNullOrEmpty(passphrase))
throw new ArgumentException(Strings.AESEncryption.EmptyKeyError, "passphrase");
2010-06-19 21:08:21 +00:00
m_key = passphrase;
}
2009-01-14 17:29:30 +00:00
#region IEncryption Members
2009-01-25 19:33:36 +00:00
public override string FilenameExtension { get { return "aes"; } }
2011-01-12 18:59:01 +00:00
public override string Description { get { return string.Format(Strings.AESEncryption.Description_v2); } }
2010-06-19 21:08:21 +00:00
public override string DisplayName { get { return Strings.AESEncryption.DisplayName; } }
protected override void Dispose(bool disposing) { m_key = null; }
public override long SizeOverhead(long filesize)
{
//If we use 1, we trigger the blocksize.
//As the AES algorithm does not alter the size,
// the results are the same as for the real size,
// but a single byte encryption is much faster.
return base.SizeOverhead(1);
}
public override Stream Encrypt(Stream input)
{
2011-01-12 18:59:01 +00:00
return new SharpAESCrypt.SharpAESCrypt(m_key, input, SharpAESCrypt.OperationMode.Encrypt);
2010-06-19 21:08:21 +00:00
}
2009-01-25 19:33:36 +00:00
2010-06-19 21:08:21 +00:00
public override Stream Decrypt(Stream input)
2009-01-14 17:29:30 +00:00
{
2013-05-05 17:57:10 +02:00
return new SharpAESCrypt.SharpAESCrypt(m_key, input, SharpAESCrypt.OperationMode.Decrypt);
2010-06-19 21:08:21 +00:00
}
2009-01-14 17:29:30 +00:00
2010-06-19 21:08:21 +00:00
public override IList<ICommandLineArgument> SupportedCommands
{
get
2010-06-19 21:08:21 +00:00
{
return new List<ICommandLineArgument>(new ICommandLineArgument[] {
});
}
2009-01-14 17:29:30 +00:00
}
2010-06-19 21:08:21 +00:00
#endregion
2009-01-14 17:29:30 +00:00
}
}