Bläddra i källkod

Modified stored filename to not include full path

tags/2.0.3
Teknikode 4 år sedan
förälder
incheckning
167c16b574

+ 7
- 4
Teknik/Areas/Upload/Controllers/UploadController.cs Visa fil

@@ -109,6 +109,7 @@ namespace Teknik.Areas.Upload.Controllers
upload.Downloads += 1;
db.Entry(upload).State = EntityState.Modified;
db.SaveChanges();

// We don't have the key, so we need to decrypt it client side
if (string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV))
{
@@ -122,11 +123,12 @@ namespace Teknik.Areas.Upload.Controllers
}
else // We have the key, so that means server side decryption
{
if (System.IO.File.Exists(upload.FileName))
string subDir = upload.FileName[0].ToString();
string filePath = Path.Combine(Config.UploadConfig.UploadDirectory, subDir, upload.FileName);
if (System.IO.File.Exists(filePath))
{
// Read in the file
byte[] data = System.IO.File.ReadAllBytes(upload.FileName);

byte[] data = System.IO.File.ReadAllBytes(filePath);
// If the IV is set, and Key is set, then decrypt it
if (!string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV))
{
@@ -162,7 +164,8 @@ namespace Teknik.Areas.Upload.Controllers
Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault();
if (upload != null)
{
string filePath = Path.Combine(Config.UploadConfig.UploadDirectory, upload.FileName);
string subDir = upload.FileName[0].ToString();
string filePath = Path.Combine(Config.UploadConfig.UploadDirectory, subDir, upload.FileName);
if (System.IO.File.Exists(filePath))
{
byte[] buffer;

+ 3
- 2
Teknik/Areas/Upload/Uploader.cs Visa fil

@@ -40,10 +40,11 @@ namespace Teknik.Areas.Upload
}

// Generate a unique file name that does not currently exist
string fileName = Utility.GenerateUniqueFileName(config.UploadConfig.UploadDirectory, config.UploadConfig.FileExtension, 10);
string filePath = Utility.GenerateUniqueFileName(config.UploadConfig.UploadDirectory, config.UploadConfig.FileExtension, 10);
string fileName = Path.GetFileName(filePath);

// once we have the filename, lets save the file
File.WriteAllBytes(fileName, file);
File.WriteAllBytes(filePath, file);

// Generate a unique url
string extension = (config.UploadConfig.IncludeExtension) ? Utility.GetDefaultExtension(contentType, defaultExtension) : string.Empty;

+ 27
- 6
Teknik/Helpers/Crypto.cs Visa fil

@@ -63,30 +63,51 @@ namespace Teknik.Helpers

public class AES
{
public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
{
return Decrypt(data, key, iv, "CTR", "NoPadding");
}
public static byte[] Decrypt(byte[] data, string key, string iv)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
return Decrypt(data, keyBytes, ivBytes);
return Decrypt(data, keyBytes, ivBytes, "CTR", "NoPadding");
}
public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
public static byte[] DecryptCBC(byte[] data, string key, string iv)
{
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
return Decrypt(data, keyBytes, ivBytes, "CBC", "PKCS5PADDING");
}
public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv, string mode, string padding)
{
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);

cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));

return cipher.DoFinal(data);
}


public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
{
return Encrypt(data, key, iv, "CTR", "NoPadding");
}
public static byte[] Encrypt(byte[] data, string key, string iv)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
return Encrypt(data, keyBytes, ivBytes);
return Encrypt(data, keyBytes, ivBytes, "CTR", "NoPadding");
}
public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
public static byte[] EncryptCBC(byte[] data, string key, string iv)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
return Encrypt(data, keyBytes, ivBytes, "CBC", "PKCS5PADDING");
}
public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv, string mode, string padding)
{
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);

cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));


Laddar…
Avbryt
Spara