/// <summary> /// Casually encrypts the old and new password to be used to during /// a change password operation. /// </summary> /// <param name="originalPassword">The original password.</param> /// <param name="newPassword">The new password.</param> /// <returns>The encrypted password change parameters.</returns> /// <remarks> /// <note> /// This method is designed to provide a low level of security during development /// and testing, when the overhead of configuring SSL certificates is not worth /// the trouble. Do not rely on this method as your only mechanism for securing /// credentials during transmission in production environments. /// </note> /// </remarks> public static byte[] EncryptPasswordChange(string originalPassword, string newPassword) { using (var ms = new EnhancedMemoryStream(256)) { ms.WriteInt32(CredentialMagic); ms.WriteString16(originalPassword); ms.WriteString16(newPassword); return(EncryptWithSalt8(ms.ToArray(), credentialsKey)); } }
/// <summary> /// Casually encrypts authentication <see cref="Credentials" /> into a byte /// array using using the <see cref="CredentialsKey" />. /// </summary> /// <param name="credentials">The <see cref="Credentials" />.</param> /// <returns>The encrypted credentials.</returns> /// <remarks> /// <note> /// This method is designed to provide a low level of security during development /// and testing, when the overhead of configuring SSL certificates is not worth /// the trouble. Do not rely on this method as your only mechanism for securing /// credentials during transmission in production environments. /// </note> /// </remarks> public static byte[] EncryptCredentials(Credentials credentials) { using (var ms = new EnhancedMemoryStream(256)) { ms.WriteInt32(CredentialMagic); ms.WriteString16(credentials.Realm); ms.WriteString16(credentials.Account); ms.WriteString16(credentials.Password); return(EncryptWithSalt8(ms.ToArray(), credentialsKey)); } }
/// <summary> /// Encrypts authentication <see cref="Credentials" /> into a byte /// array using the specified public asymmetric public key and /// algorithm. /// </summary> /// <param name="credentials">The <see cref="Credentials" />.</param> /// <param name="algorithm">The encryption algorithm.</param> /// <param name="key">The public key.</param> /// <returns>The encrypted credentials.</returns> /// <remarks> /// The current implementation supports only the "RSA" provider. /// </remarks> public static byte[] EncryptCredentials(Credentials credentials, string algorithm, string key) { using (var ms = new EnhancedMemoryStream(256)) { ms.WriteInt32(Crypto.CredentialMagic); ms.WriteString16(credentials.Realm); ms.WriteString16(credentials.Account); ms.WriteString16(credentials.Password); ms.WriteBytesNoLen(Crypto.GetSalt4()); return(Encrypt(algorithm, key, ms.ToArray())); } }
public void EnhancedMemoryStream_String() { var es = new EnhancedMemoryStream(); es.WriteString16(null); es.WriteString16("Hello World!"); es.WriteString16(new String('a', 45000)); es.Seek(0, SeekOrigin.Begin); Assert.IsNull(es.ReadString16()); Assert.AreEqual("Hello World!", es.ReadString16()); Assert.AreEqual(new String('a', 45000), es.ReadString16()); }
/// <summary> /// Encrypts the keys using the symmetric key passed. /// </summary> /// <returns>The encrypted key chain.</returns> public byte[] Encrypt(SymmetricKey key) { using (var ms = new EnhancedMemoryStream()) { ms.WriteInt32(Magic); ms.WriteInt32(keys.Count); foreach (string privateKey in keys.Values) { ms.WriteString16(privateKey); } ms.WriteBytesNoLen(Crypto.GetSalt8()); return(Crypto.Encrypt(ms.ToArray(), key)); } }
/// <summary> /// Serializes and encrypts the message into a byte array. /// </summary> /// <param name="sharedKey">The shared encryption key.</param> /// <returns>The serialized message.</returns> public byte[] ToArray(SymmetricKey sharedKey) { if (this.HostEntry == null) { throw new InvalidOperationException("HostEntry property cannot be null."); } using (var ms = new EnhancedMemoryStream(2048)) { ms.WriteInt32Le(Magic); ms.WriteByte((byte)FormatVer); ms.WriteInt64Le(TimeStampUtc.Ticks); ms.WriteInt32Le((int)Flags); ms.WriteString16(this.HostEntry.ToString()); ms.WriteBytesNoLen(Crypto.GetSalt4()); return(Crypto.Encrypt(ms.ToArray(), sharedKey)); } }
/// <summary> /// Attempts to acquire the global application lock. /// </summary> /// <remarks> /// <note> /// <see cref="Release" /> should be called promptly when the /// application terminates to release the lock. /// </note> /// </remarks> /// <exception cref="GlobalLockException">Thrown when there's a problem acquiring the lock.</exception> public unsafe void Lock() { EnhancedMemoryStream ms; byte *pMem; byte[] buf; Assembly assembly; string path; lock (syncLock) { if (initLock != null) { cLock++; // The lock is already acquired return; } try { // Use a global shared memory block to enforce the lock. assembly = Assembly.GetEntryAssembly(); if (assembly == null) { assembly = Assembly.GetCallingAssembly(); } path = Helper.StripFileScheme(assembly.CodeBase); ms = new EnhancedMemoryStream(4096); ms.WriteString16(path); initLock = new SharedMem(); initLock.Open("LT.Lock." + appName, 4096, SharedMem.OpenMode.CREATE_OPEN); pMem = initLock.Lock(); if (pMem[0] != 0 || pMem[1] != 0) { buf = new byte[initLock.Size]; for (int i = 0; i < buf.Length; i++) { buf[i] = pMem[i]; } ms = new EnhancedMemoryStream(buf); initLock.Unlock(); initLock.Close(); initLock = null; throw new GlobalLockException("Global lock is already acquired by [{0}].", ms.ReadString16()); } buf = ms.ToArray(); for (int i = 0; i < buf.Length; i++) { pMem[i] = buf[i]; } initLock.Unlock(); } catch (Exception e) { throw new GlobalLockException(e); } } cLock++; }
/// <summary> /// Encrypts a byte array using a combination of an asymmetric RSA key and the /// specified symmetric encryption algorithm and a one-time key generated by /// the method. /// </summary> /// <param name="rsaKey">The encrypting RSA key as XML or as a secure key container name.</param> /// <param name="plainText">The data to be encrypted.</param> /// <param name="algorithm">The symmetric encryption algorithm name.</param> /// <param name="keySize">The one-time symmetric key size to generate in bits.</param> /// <param name="paddedSize">Specifies the minimum padded size of the encrypted content.</param> /// <param name="symmetricKey">Returns as the symmetric encryption algorithm arguments.</param> /// <returns>The encrypted result.</returns> /// <remarks> /// <para> /// Note that applications should take some care to ensure that the <paramref name="symmetricKey" /> /// value return is disposed so that the symmetric encryption key will be cleared. /// </para> /// <para> /// The current supported cross platform encryption algorithms /// are: "DES", "RC2", "TripleDES", and "AES" (Rijndael). /// </para> /// </remarks> /// <exception cref="ArgumentException">Thrown if the requested encryption algorithm is unknown.</exception> public static byte[] Encrypt(string rsaKey, byte[] plainText, string algorithm, int keySize, int paddedSize, out SymmetricKey symmetricKey) { EnhancedMemoryStream output = new EnhancedMemoryStream(Math.Max(plainText.Length, paddedSize) + 512); EnhancedMemoryStream ms = new EnhancedMemoryStream(512); BlockEncryptor encryptor = null; byte[] symKey; byte[] symIV; Crypto.GenerateSymmetricKey(algorithm, keySize, out symKey, out symIV); encryptor = new BlockEncryptor(algorithm, symKey, symIV); symmetricKey = new SymmetricKey(algorithm, (byte[])symKey.Clone(), (byte[])symIV.Clone()); try { // Write header fields output.WriteInt32(Magic); output.WriteInt32(0); // Write encryption Info ms.WriteString16(algorithm); ms.WriteBytes16(symKey); ms.WriteBytes16(symIV); ms.WriteBytesNoLen(Crypto.GetSalt8()); output.WriteBytes16(AsymmetricCrypto.Encrypt(CryptoAlgorithm.RSA, rsaKey, ms.ToArray())); // Write encrypted contents ms.SetLength(0); ms.WriteInt32(Magic); ms.WriteBytesNoLen(Crypto.GetSalt8()); ms.WriteBytes32(plainText); for (int i = plainText.Length; i < paddedSize; i++) { ms.WriteByte((byte)i); // Padding bytes } output.WriteBytes32(encryptor.Encrypt(ms.ToArray())); // That's it, we're done. return(output.ToArray()); } finally { if (symKey != null) { Array.Clear(symKey, 0, symKey.Length); } if (symIV != null) { Array.Clear(symIV, 0, symIV.Length); } if (encryptor != null) { encryptor.Dispose(); } output.Close(); ms.Close(); } }