public string Upload(string path, string filename, string key, int alg) { try { byte[] f = File.ReadAllBytes(path); StoredFile sf = new StoredFile(); sf.username = Username; sf.filename = filename; sf.hash = MD5.HashString(f); switch (alg) { case 0: sf.data = DoubleTransposition.Encrypt(f, key, sf.hash); sf.size = sf.data.Length; break; case 1: sf.data = XTEA.Encrypt(f, key, sf.hash); sf.size = sf.data.Length; break; default: throw new Exception("You have not chosen an algorithm."); } svc.Upload(Username, sf, Token); Refresh(); return("File successfuly uploaded."); } catch (Exception e) { return(e.Message); } }
public string Download(string filename, string key, int alg, Download form) { try { StoredFile sf = svc.Download(Username, filename, Token); byte[] f; switch (alg) { case 0: f = DoubleTransposition.Decrypt(sf.data, key, sf.hash); break; case 1: f = XTEA.Decrypt(sf.data, key, sf.hash); break; default: throw new Exception("You have not chosen an algorithm."); } form.Save(filename, f); return("File successfuly downloaded."); } catch (Exception e) { return(e.Message); } }
private static void Initialize() { // Initializing objects used for encryption if (CypherDoubleTransposition == null) { CypherDoubleTransposition = new DoubleTransposition(); } if (CypherXTEA == null) { CypherXTEA = new XTEA(); } if (CypherKnapsack == null) { CypherKnapsack = new Knapsack(); } if (CypherMD5 == null) { CypherMD5 = new MD5(); } // Initializing algorithm dictionary if (EncryptionDictionary == null) { EncryptionDictionary = new Dictionary <int, Func <byte[], byte[]> >() { // Encryption methods { (int)Algorithm.DoubleTranposition, CypherDoubleTransposition.Crypt }, { (int)Algorithm.XTEA, CypherXTEA.Crypt }, { (int)Algorithm.Knapsack, CypherKnapsack.Crypt }, { (int)Algorithm.MD5, CypherMD5.Crypt }, // Decryption methods, enum value + number of algorithms { (int)Algorithm.DoubleTranposition + AlgoNumber, CypherDoubleTransposition.Decrypt }, { (int)Algorithm.XTEA + AlgoNumber, CypherXTEA.Decrypt }, { (int)Algorithm.Knapsack + AlgoNumber, CypherKnapsack.Decrypt }, { (int)Algorithm.MD5 + AlgoNumber, CypherKnapsack.Decrypt } } } ; // Initializing key and IV dictionary if (KeyIVDictionary == null) { KeyIVDictionary = new Dictionary <int, Func <byte[], bool> >() { // Set Key methods { (int)Algorithm.DoubleTranposition, CypherDoubleTransposition.SetKey }, { (int)Algorithm.XTEA, CypherXTEA.SetKey }, { (int)Algorithm.Knapsack, CypherKnapsack.SetKey }, { (int)Algorithm.MD5, CypherMD5.SetKey }, // Set IV methods, enum value + number of algorithms { (int)Algorithm.DoubleTranposition + AlgoNumber, CypherDoubleTransposition.SetIV }, { (int)Algorithm.XTEA + AlgoNumber, CypherXTEA.SetIV }, { (int)Algorithm.Knapsack + AlgoNumber, CypherKnapsack.SetIV }, { (int)Algorithm.MD5 + AlgoNumber, CypherMD5.SetIV } } } ; // Initializing property dictionary if (PropertyDictionary == null) { PropertyDictionary = new Dictionary <int, Func <IDictionary <string, byte[]>, bool> >() { // Set Property methods { (int)Algorithm.DoubleTranposition, CypherDoubleTransposition.SetAlgorithmProperties }, { (int)Algorithm.XTEA, CypherXTEA.SetAlgorithmProperties }, { (int)Algorithm.Knapsack, CypherKnapsack.SetAlgorithmProperties }, { (int)Algorithm.MD5, CypherMD5.SetAlgorithmProperties }, } } ; }