/// <summary> /// Gets the length that an Iv value must have for the encryption algorithm specified by the method's argument. /// </summary> /// <param name="alg">Type of encryption algorithm.</param> /// <returns>Int value representing string length.</returns> public static int GetEncryptionKeyIvStringLength(pfEncryptionAlgorithm alg) { int stringLength = 16; switch (alg) { case pfEncryptionAlgorithm.AES: stringLength = 16; break; case pfEncryptionAlgorithm.DES: stringLength = 8; break; case pfEncryptionAlgorithm.TripleDES: stringLength = 16; break; default: //default to AES stringLength = 16; break; } return(stringLength); }
private int _statusReportIntervalSeconds = 5; //in seconds //constructor /// <summary> /// Constructor initializes the Key and Iv values to defaults upon object creation. /// Use Key and Iv properties to customize values to your application's requirements. /// </summary> /// <param name="alg">Encryption algorithm to use for this instance.</param> public PFFileEncryptor(pfEncryptionAlgorithm alg) { switch (alg) { case pfEncryptionAlgorithm.AES: _cryptoProvider = new AesCryptoServiceProvider(); break; case pfEncryptionAlgorithm.DES: _cryptoProvider = new DESCryptoServiceProvider(); break; case pfEncryptionAlgorithm.TripleDES: _cryptoProvider = new TripleDESCryptoServiceProvider(); break; default: _msg.Length = 0; _msg.Append("Invalid or unexpected encryption algorithm: "); _msg.Append(alg.ToString()); throw new System.Exception(_msg.ToString()); } _encryptionAlgorithm = alg; Initialize(); }
/// <summary> /// Translates text description to a pfEncryptionAlgorithm enum value. /// </summary> /// <param name="typeDescription">String specifying the name of the algorithm.</param> /// <returns>pfEncryptionAlgorithm value.</returns> public static pfEncryptionAlgorithm GetEncryptionAlgorithm(string typeDescription) { pfEncryptionAlgorithm ret = pfEncryptionAlgorithm.NotSpecified; switch (typeDescription) { case "AES": ret = pfEncryptionAlgorithm.AES; break; case "DES": ret = pfEncryptionAlgorithm.DES; break; case "TripleDES": ret = pfEncryptionAlgorithm.TripleDES; break; default: ret = pfEncryptionAlgorithm.Invalid; break; } return(ret); }
/// <summary> /// Generate randomized Key and Iv values for symmetric encryption processing. /// </summary> /// <param name="typeEncryption">See <see cref="pfEncryptionAlgorithm"/> for list of valid values.</param> /// <returns>pfKeyIvPair value.</returns> public static pfKeyIvPair GenerateKeyIvPair(pfEncryptionAlgorithm typeEncryption) { int keyLength = 8; int ivLength = 8; pfKeyIvPair keyIvPair = new pfKeyIvPair(); RandomString randstr = new RandomString(); switch (typeEncryption) { case pfEncryptionAlgorithm.AES: keyLength = 16; ivLength = 16; break; case pfEncryptionAlgorithm.DES: keyLength = 8; ivLength = 8; break; case pfEncryptionAlgorithm.TripleDES: keyLength = 16; ivLength = 8; break; default: //default to AES keyLength = 16; ivLength = 16; break; } keyIvPair.key = randstr.GetStringNoSpaces(keyLength); keyIvPair.IV = randstr.GetStringNoSpaces(ivLength); return(keyIvPair); }
/// <summary> /// Factory method for creating instances of file encryption objects. /// </summary> /// <param name="alg">Type of encryption algorithm.</param> /// <returns>IFileEncryptor interface for the chosen file encryptor.</returns> public static IFileEncryptor GetFileEncryptor(pfEncryptionAlgorithm alg) { IFileEncryptor enc; switch (alg) { case pfEncryptionAlgorithm.AES: enc = new FileEncryptorAES(); break; case pfEncryptionAlgorithm.DES: enc = new FileEncryptorDES(); break; case pfEncryptionAlgorithm.TripleDES: enc = new FileEncryptorTripleDES(); break; default: enc = new FileEncryptorAES(); break; } return(enc); }
/// <summary> /// Saves the public property values contained in the current instance to an encrypted file. Key and IV parameters are used /// to specify an AES encryption. Serialization is used to get the object values into an XML string that will be encrypted and /// written to the output file. /// </summary> /// <param name="filePath">Full path for output file.</param> /// <param name="key">AES encryption key.</param> /// <param name="iv">AES IV value.</param> /// <remarks>Only AES encryption is supported by this routine.</remarks> public void SaveToXmlFile(string filePath, string key, string iv) { pfEncryptionAlgorithm alg = pfEncryptionAlgorithm.AES; IStringEncryptor enc2 = PFEncryption.GetStringEncryptor(alg); //store xml data in a string string xmldata = this.ToXmlString(); //encrypt the xml data string enc2.Key = key; enc2.IV = iv; string encryptedXML = enc2.Encrypt(xmldata); //save encrypted xml to file File.WriteAllText(filePath, encryptedXML); }
/// <summary> /// Creates and initializes an instance of the class by loading a serialized version of the instance stored in an encrypted file. /// The file is first decrypted into an XML string and the XML string is then used to create an instance of the class. /// </summary> /// <param name="filePath">Full path for the input file.</param> /// <param name="key">AES encryption key.</param> /// <param name="iv">AES IV value.</param> /// <returns>An instance of PFKeyValueListEx.</returns> /// <remarks>Only AES encryption is supported by this routine.</remarks> public static PFKeyValueListExSorted <K, V> LoadFromXmlFile(string filePath, string key, string iv) { pfEncryptionAlgorithm alg = pfEncryptionAlgorithm.AES; IStringEncryptor enc2 = PFEncryption.GetStringEncryptor(alg); PFKeyValueListExSorted <K, V> listElements; //first: load the encrypted data from the specified file string encryptedXML = File.ReadAllText(filePath); //next: decrypt the encrypted data enc2.Key = key; enc2.IV = iv; string xmldata = enc2.Decrypt(encryptedXML); //step 3: create a PFLIcense object from the decrypted string listElements = LoadFromXmlString(xmldata); //finally: return to caller return(listElements); }
public void SaveKeyIVPair(pfEncryptionAlgorithm alg, pfKeyIvPair kvp, string filePath) { try { PFKeyIVValues kvv = new PFKeyIVValues(kvp.key, kvp.IV); kvv.Algorithm = alg; kvv.SaveToXmlFile(filePath); } catch (System.Exception ex) { _msg.Length = 0; _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog); } finally { ; } }
/// <summary> /// Constructor. /// </summary> public PFKeyIVValues(pfEncryptionAlgorithm alg, string key, string iv) { _alg = alg; _key = key; _iv = iv; }
public pfKeyIvPair GenerateKeyIVPair(pfEncryptionAlgorithm alg) { pfKeyIvPair ki = PFEncryption.GenerateKeyIvPair(alg); return(ki); }