Skip to content

Commit

Permalink
EncryptFile and DecryptFile
Browse files Browse the repository at this point in the history
  • Loading branch information
budgetpreneur committed Sep 19, 2023
1 parent 44e75fd commit 52b0488
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Api/PubnubApi/Security/Crypto/CryptoModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public byte[] Encrypt(byte[] data)
{
return _cryptor.Encrypt(data);
}
public void EncryptFile(string sourceFile, string destinationFile)
{
_cryptor.EncryptFile(sourceFile, destinationFile);
}
public string Decrypt(string data)
{
if (data == null)
Expand Down Expand Up @@ -77,5 +81,27 @@ public byte[] Decrypt(byte[] data)
}
return _cryptor?.Decrypt(data);
}
public void DecryptFile(string sourceFile, string destinationFile)
{
if (string.IsNullOrEmpty(sourceFile) || sourceFile.Length < 1)
{
throw new ArgumentException("sourceFile is not valid");
}
if (string.IsNullOrEmpty(destinationFile) || destinationFile.Length < 1)
{
throw new ArgumentException("destinationFile is not valid");
}
CryptorHeader header = CryptorHeader.FromFile(sourceFile);
if (header == null)
{
_fallbackCryptor?.DecryptFile(sourceFile, destinationFile);
return;
}
if (!header.Identifier.SequenceEqual(_cryptor?.Identifier))
{
throw new PNException("CryptorHeader mismatch");
}
_cryptor?.DecryptFile(sourceFile, destinationFile);
}
}
}
8 changes: 8 additions & 0 deletions src/Api/PubnubApi/Security/Crypto/Cryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public byte[] Encrypt(byte[] data)
{
return _cryptor.Encrypt(data);
}
public void EncryptFile(string sourceFile, string destinationFile)
{
_cryptor.EncryptFile(sourceFile, destinationFile);
}

public string Decrypt(string data)
{
Expand All @@ -39,5 +43,9 @@ public byte[] Decrypt(byte[] data)
{
return _cryptor.Decrypt(data);
}
public void DecryptFile(string sourceFile, string destinationFile)
{
_cryptor.DecryptFile(sourceFile, destinationFile);
}
}
}
52 changes: 52 additions & 0 deletions src/Api/PubnubApi/Security/Crypto/Cryptors/AesCbcCryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,57 @@ public override byte[] Decrypt(byte[] encryptedData)
throw new PNException("Decrypt Error", ex);
}
}

public override void EncryptFile(string sourceFile, string destinationFile)
{
if (string.IsNullOrEmpty(sourceFile) || sourceFile.Length < 1)
{
throw new ArgumentException("sourceFile is not valid");
}
#if !NETSTANDARD10 && !NETSTANDARD11
bool validSource = System.IO.File.Exists(sourceFile);
if (!validSource)
{
throw new ArgumentException("sourceFile is not valid");
}
string destDirectory = System.IO.Path.GetDirectoryName(destinationFile);
bool validDest = System.IO.Directory.Exists(destDirectory);
if (!string.IsNullOrEmpty(destDirectory) && !validDest)
{
throw new ArgumentException("destination path is not valid");
}
byte[] inputBytes = System.IO.File.ReadAllBytes(sourceFile);
byte[] outputBytes = Encrypt(inputBytes);
System.IO.File.WriteAllBytes(destinationFile, outputBytes);
#else
throw new NotSupportedException("FileSystem not supported in NetStandard 1.0/1.1. Consider higher version of .NetStandard.");
#endif
}

public override void DecryptFile(string sourceFile, string destinationFile)
{
if (string.IsNullOrEmpty(sourceFile) || sourceFile.Length < 1)
{
throw new ArgumentException("sourceFile is not valid");
}
#if !NETSTANDARD10 && !NETSTANDARD11
bool validSource = System.IO.File.Exists(sourceFile);
if (!validSource)
{
throw new ArgumentException("sourceFile is not valid");
}
string destDirectory = System.IO.Path.GetDirectoryName(destinationFile);
bool validDest = System.IO.Directory.Exists(destDirectory);
if (!string.IsNullOrEmpty(destDirectory) && !validDest)
{
throw new ArgumentException("destination path is not valid");
}
byte[] inputBytes = System.IO.File.ReadAllBytes(sourceFile);
byte[] outputBytes = Decrypt(inputBytes);
System.IO.File.WriteAllBytes(destinationFile, outputBytes);
#else
throw new NotSupportedException("FileSystem not supported in NetStandard 1.0/1.1. Consider higher version of .NetStandard.");
#endif
}
}
}
4 changes: 4 additions & 0 deletions src/Api/PubnubApi/Security/Crypto/Cryptors/CryptorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,9 @@ protected byte[] InternalDecrypt(byte[] dataBytes, byte[] ivBytes, byte[] keyByt
public abstract byte[] Encrypt(byte[] data);
public abstract string Decrypt(string encryptedData);
public abstract byte[] Decrypt(byte[] encryptedData);

public abstract void EncryptFile(string sourceFile, string destinationFile);

public abstract void DecryptFile(string sourceFile, string destinationFile);
}
}
9 changes: 9 additions & 0 deletions src/Api/PubnubApi/Security/Crypto/Cryptors/CryptorHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public static CryptorHeader FromBytes(byte[] data)
return new CryptorHeader(identifier, dataSize);
}

public static CryptorHeader FromFile(string sourceFile)
{
using (System.IO.FileStream fs = new System.IO.FileStream(sourceFile, System.IO.FileMode.Open, System.IO.FileAccess.Read))

Check failure on line 76 in src/Api/PubnubApi/Security/Crypto/Cryptors/CryptorHeader.cs

View workflow job for this annotation

GitHub Actions / Integration and Unit tests

The type or namespace name 'FileStream' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)

Check failure on line 76 in src/Api/PubnubApi/Security/Crypto/Cryptors/CryptorHeader.cs

View workflow job for this annotation

GitHub Actions / Integration and Unit tests

The type or namespace name 'FileStream' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)

Check failure on line 76 in src/Api/PubnubApi/Security/Crypto/Cryptors/CryptorHeader.cs

View workflow job for this annotation

GitHub Actions / Integration and Unit tests

The type or namespace name 'FileMode' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)

Check failure on line 76 in src/Api/PubnubApi/Security/Crypto/Cryptors/CryptorHeader.cs

View workflow job for this annotation

GitHub Actions / Integration and Unit tests

The type or namespace name 'FileAccess' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)

Check failure on line 76 in src/Api/PubnubApi/Security/Crypto/Cryptors/CryptorHeader.cs

View workflow job for this annotation

GitHub Actions / Integration and Unit tests

The type or namespace name 'FileStream' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)

Check failure on line 76 in src/Api/PubnubApi/Security/Crypto/Cryptors/CryptorHeader.cs

View workflow job for this annotation

GitHub Actions / Integration and Unit tests

The type or namespace name 'FileStream' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)

Check failure on line 76 in src/Api/PubnubApi/Security/Crypto/Cryptors/CryptorHeader.cs

View workflow job for this annotation

GitHub Actions / Integration and Unit tests

The type or namespace name 'FileMode' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)

Check failure on line 76 in src/Api/PubnubApi/Security/Crypto/Cryptors/CryptorHeader.cs

View workflow job for this annotation

GitHub Actions / Integration and Unit tests

The type or namespace name 'FileAccess' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)
{
byte[] headerBytes = new byte[5 + IDENTIFIER_LENGTH + 3];
fs.Read(headerBytes, 0, headerBytes.Length);
return FromBytes(headerBytes);
}
}
public byte[] ToBytes()
{
List<byte> bytes = new List<byte>();
Expand Down
52 changes: 52 additions & 0 deletions src/Api/PubnubApi/Security/Crypto/Cryptors/LegacyCryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,57 @@ public override byte[] Decrypt(byte[] encryptedData)
throw new PNException("Decrypt Error", ex);
}
}

public override void EncryptFile(string sourceFile, string destinationFile)
{
if (string.IsNullOrEmpty(sourceFile) || sourceFile.Length < 1)
{
throw new ArgumentException("sourceFile is not valid");
}
#if !NETSTANDARD10 && !NETSTANDARD11
bool validSource = System.IO.File.Exists(sourceFile);
if (!validSource)
{
throw new ArgumentException("sourceFile is not valid");
}
string destDirectory = System.IO.Path.GetDirectoryName(destinationFile);
bool validDest = System.IO.Directory.Exists(destDirectory);
if (!string.IsNullOrEmpty(destDirectory) && !validDest)
{
throw new ArgumentException("destination path is not valid");
}
byte[] inputBytes = System.IO.File.ReadAllBytes(sourceFile);
byte[] outputBytes = Encrypt(inputBytes);
System.IO.File.WriteAllBytes(destinationFile, outputBytes);
#else
throw new NotSupportedException("FileSystem not supported in NetStandard 1.0/1.1. Consider higher version of .NetStandard.");
#endif
}

public override void DecryptFile(string sourceFile, string destinationFile)
{
if (string.IsNullOrEmpty(sourceFile) || sourceFile.Length < 1)
{
throw new ArgumentException("sourceFile is not valid");
}
#if !NETSTANDARD10 && !NETSTANDARD11
bool validSource = System.IO.File.Exists(sourceFile);
if (!validSource)
{
throw new ArgumentException("sourceFile is not valid");
}
string destDirectory = System.IO.Path.GetDirectoryName(destinationFile);
bool validDest = System.IO.Directory.Exists(destDirectory);
if (!string.IsNullOrEmpty(destDirectory) && !validDest)
{
throw new ArgumentException("destination path is not valid");
}
byte[] inputBytes = System.IO.File.ReadAllBytes(sourceFile);
byte[] outputBytes = Decrypt(inputBytes);
System.IO.File.WriteAllBytes(destinationFile, outputBytes);
#else
throw new NotSupportedException("FileSystem not supported in NetStandard 1.0/1.1. Consider higher version of .NetStandard.");
#endif
}
}
}
2 changes: 2 additions & 0 deletions src/Api/PubnubApi/Security/Crypto/ICryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public interface ICryptor

string Encrypt(string data);
byte[] Encrypt(byte[] data);
void EncryptFile(string sourceFile, string destinationFile);

string Decrypt(string encryptedData);
byte[] Decrypt(byte[] encryptedData);
void DecryptFile(string sourceFile, string destinationFile);
}
}
33 changes: 33 additions & 0 deletions src/UnitTests/PubnubApi.Tests/CryptorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,38 @@ public void TestYayByteArrayAesCbcCryptoDecryptionModule()
byte[] expectedBytes = Encoding.UTF8.GetBytes(message);
Assert.AreEqual(expectedBytes, decryptedBytes);
}

[Test]
public void TestLocalFileEncryptionModuleFromPath()
{
IPubnubUnitTest pubnubUnitTest = new PubnubUnitTest();
pubnubUnitTest.IV = new byte[16] { 133, 126, 158, 123, 43, 95, 96, 90, 215, 178, 17, 73, 166, 130, 79, 156 };
string sourceFile = "fileupload.txt";
string destFile = "fileupload_encrypted.txt";
if (System.IO.File.Exists(destFile))
{
System.IO.File.Delete(destFile);
}
CryptoModule module = new CryptoModule(new AesCbcCryptor("enigma"), new LegacyCryptor("enigma"));
module.EncryptFile(sourceFile, destFile);
Assert.IsTrue(System.IO.File.Exists(destFile));

}

[Test]
public void TestLocalFileDecryptionModuleFromPath()
{
string sourceFile = "fileupload_enc.txt";
string destFile = "fileupload_enc_decrypted_to_original.txt";
if (System.IO.File.Exists(destFile))
{
System.IO.File.Delete(destFile);
}
CryptoModule module = new CryptoModule(new AesCbcCryptor("enigma"), new LegacyCryptor("enigma"));
module.DecryptFile(sourceFile, destFile);
Assert.IsTrue(System.IO.File.Exists(destFile));

}

}
}

0 comments on commit 52b0488

Please sign in to comment.