public SaltedBytesCache( SecureHead secureHead, SecurePassword securePassword) { if (secureHead == null) { throw new ArgumentException("secureHead should NOT be null."); } if (securePassword == null) { throw new ArgumentException("securePassword should NOT be null."); } byte[] saltedPasswordBytes = GetSaltedPassowrdBytes(secureHead, securePassword); if (BytesHelper.IsNullOrEmptyArray(saltedPasswordBytes) == true) { throw new ArgumentException("saltedPasswordBytes should NOT be null."); } byte[] saltedInitialVectorBytes = GetSaltedInitialVectorBytes(secureHead); if (BytesHelper.IsNullOrEmptyArray(saltedInitialVectorBytes) == true) { throw new ArgumentException("saltedInitialVectorBytes should NOT be null."); } SecureHead = secureHead; SaltedPasswordBytes = saltedPasswordBytes; SaltedInitiaVectorBytes = saltedInitialVectorBytes; }
public byte[] Encrypt(byte[] plainBytes, uint offset, uint count) { byte[] bytes = null; try { byte[] secureBytes = EncryptPlainBytes(plainBytes, offset, count); SecureHead secureHead = new SecureHead( Config.Salt, Config.InitiaVector, Config.SaltIterations, Config.SaltedInitialVectorBytesCount, Config.SaltedPasswordBytesCount, (uint)secureBytes.Length); byte[] secureHeadBytes = secureHead.Encode(); bytes = new byte[secureHeadBytes.Length + secureBytes.Length]; Array.Copy(secureHeadBytes, 0, bytes, 0, secureHeadBytes.Length); Array.Copy(secureBytes, 0, bytes, secureHeadBytes.Length, secureBytes.Length); } catch (Exception e) { //ignore all exceptions according to requirements #if DEBUG Env.Instance.ShowMessage($"encrypt exception desc={e.Message} stack trace={e.StackTrace}"); #endif } return(bytes); }
public string Decrypt(string secureString) { string result = null; try { byte[] secureBytes = Convert.FromBase64String(secureString); uint saltBytesCount = 0; uint initiaVectorBytesCount = 0; SecureHead.DecodeByteCounts(secureBytes, 0, out saltBytesCount, out initiaVectorBytesCount); SecureHead secureHead = SecureHead.Decode( secureBytes, 2 * sizeof(uint), saltBytesCount, initiaVectorBytesCount); byte[] plainBytes = Decrypt(secureBytes, secureHead.TotalBytesCount, secureHead.SecureBytesCount, secureHead); if (plainBytes != null) { result = BytesHelper.GetUTF8String(plainBytes, 0, (uint)plainBytes.Length); } } catch (Exception e) { //ignore all exceptions according to requirements } return(result); }
private byte[] GetSaltedInitialVectorBytes(SecureHead secureHead) { return(SaltString( secureHead.InitiaVector, secureHead.Salt, secureHead.SaltIterations, secureHead.SaltedInitialVectorBytesCount)); }
public byte[] Decrypt(byte[] secureBytes, uint offset, uint count, SecureHead secureHead) { string error = null; byte[] bytes = null; if (isDisposed == true) { error = "security manager has been disposed."; } if (BytesHelper.IsNullOrEmptyArray(secureBytes) == true) { error = "secureBytes array should NOT be null or empty."; } if (Config.IsValid == false) { error = "Config is not valid."; } if (secureHead == null) { error = "secureHead should NOT be null."; } if (error == null) { try { SaltedBytesCache decryptionCache = GetDecryptionSaltedBytesCache(secureHead); bytes = AesDecrypt( secureBytes, offset, count, decryptionCache.SaltedPasswordBytes, decryptionCache.SaltedInitiaVectorBytes); } catch (Exception e) { #if DEBUG Env.Instance.ShowMessage($"decrypt exception desc={e.Message} stack trace={e.StackTrace}"); #endif } } else { #if DEBUG Env.Instance.ShowMessage(error); #endif } return(bytes); }
//instance private byte[] GetSaltedPassowrdBytes(SecureHead secureHead, SecurePassword securePassword) { string plainPassword = securePassword.GetString(); byte[] saltedPasswordBytes = SaltString( plainPassword, secureHead.Salt, secureHead.SaltIterations, secureHead.SaltedPasswordBytesCount); plainPassword = SecurePassword.DestroyString(plainPassword); return(saltedPasswordBytes); }
private SaltedBytesCache GetDecryptionSaltedBytesCache(SecureHead secureHead) { if (decryptionCaches == null) { decryptionCaches = new Dictionary <string, SaltedBytesCache>(); } string cacheKey = secureHead.ToString(); if (decryptionCaches.ContainsKey(cacheKey) == false) { decryptionCaches.Add( cacheKey, new SaltedBytesCache( secureHead, Config.Password)); } return(decryptionCaches[cacheKey]); }
public byte[] Decrypt(byte[] secureBytes, SecureHead secureHead) { return(Decrypt(secureBytes, 0, (uint)secureBytes.Length, secureHead)); }
public override string ToString() { return(SecureHead.ToString()); }