public static AesKeyIvPair GenKeyAndIv( byte[] pass, byte[] salt, CryptConfigFileHelperScryptParameters scryptParameters = null) { if (scryptParameters == null) { scryptParameters = new CryptConfigFileHelperScryptParameters(); } byte[] passHash; byte[] saltHash; using (var mySha256 = new SHA256Managed()) { passHash = mySha256.ComputeHash(pass); saltHash = mySha256.ComputeHash(salt); } var derivedKey = SCrypt.ComputeDerivedKey( passHash, saltHash, scryptParameters.Cost, scryptParameters.BlockSize, scryptParameters.Parallel, 1, 48 ); var initKey = new byte[32]; var iv = new byte[16]; Array.Copy(derivedKey, 0, initKey, 0, 32); Array.Copy(derivedKey, 32, iv, 0, 16); return(new AesKeyIvPair() { Iv = iv, Key = initKey }); }
public static byte[] Decrypt( byte[] encryptedData, byte[] pass, byte[] salt, CryptConfigFileHelperScryptParameters scryptParameters = null) { if (encryptedData == null || pass == null || salt == null) { throw new ArgumentNullException(); } var keyIvGenerated = GenKeyAndIv(pass, salt, scryptParameters); try { return(keyIvGenerated.DecryptData(encryptedData)); } catch (EnumException <AesKeyIvPair.EDecryptDataErrCodes> enumExc) { if (enumExc.ExceptionCode == AesKeyIvPair.EDecryptDataErrCodes.WrongKey) { throw EnumException.Create(EDecryptErrCodes.WrongPassword, innerException: enumExc); } throw; } }
public T1 GetValue <T1>( byte[] pass, CryptConfigFileHelperScryptParameters scryptParameters = null ) { try { return(Encoding.UTF8.GetString(CryptConfigFileHelper.Decrypt( EncryptedData, pass, Salt, scryptParameters )).ParseJsonToType <T1>()); } catch (EnumException <CryptConfigFileHelper.EDecryptErrCodes> enumExc) { if (enumExc.ExceptionCode == CryptConfigFileHelper.EDecryptErrCodes.WrongPassword) { throw EnumException.Create( EGetValueT1ErrCodes.WrongPassword, innerException: enumExc ); } throw; } }
public async Task ChangePass( byte[] oldPass, byte[] newPass, CryptConfigFileHelperScryptParameters scryptParameters = null ) { if (!CheckPass(oldPass)) { throw new ArgumentException( MyNameof.GetLocalVarName(() => oldPass) ); } var originData = GetOriginData( oldPass, scryptParameters ); Salt = new byte[32]; MiscFuncs.GetRandomBytes(Salt); EncryptedData = CryptConfigFileHelper.Encrypt( originData, newPass, Salt, scryptParameters ); }
public byte[] GetOriginData( byte[] pass, CryptConfigFileHelperScryptParameters scryptParameters = null) { return(CryptConfigFileHelper.Decrypt( EncryptedData, pass, Salt, scryptParameters )); }
public static ScryptPassEncryptedData FromValue <T1>( T1 value, byte[] pass, CryptConfigFileHelperScryptParameters scryptParameters = null ) { return(new ScryptPassEncryptedData( Encoding.UTF8.GetBytes(value.WriteObjectToJson()), pass, null, scryptParameters )); }
public static byte[] Encrypt( byte[] data, byte[] pass, byte[] salt, CryptConfigFileHelperScryptParameters scryptParameters = null) { if (data == null || pass == null || salt == null) { throw new ArgumentNullException(); } var keyIvGenerated = GenKeyAndIv(pass, salt, scryptParameters); return(keyIvGenerated.EncryptData(data)); }
public bool CheckPass( byte[] pass, CryptConfigFileHelperScryptParameters scryptParameters = null ) { try { CryptConfigFileHelper.Decrypt( EncryptedData, pass, Salt, scryptParameters ); return(true); } catch { return(false); } }
public ScryptPassEncryptedData( byte[] originData, byte[] pass, byte[] salt = null, CryptConfigFileHelperScryptParameters scryptParameters = null ) { if (salt == null) { salt = new byte[32]; MiscFuncs.GetRandomBytes(salt); } if (salt.Length != 32) { throw new Exception("salt.Length != 32"); } Salt = salt; EncryptedData = CryptConfigFileHelper.Encrypt( originData, pass, salt, scryptParameters ); }