コード例 #1
0
        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
            });
        }
コード例 #2
0
        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;
            }
        }
コード例 #3
0
 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;
     }
 }
コード例 #4
0
        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
                );
        }
コード例 #5
0
 public byte[] GetOriginData(
     byte[] pass,
     CryptConfigFileHelperScryptParameters scryptParameters = null)
 {
     return(CryptConfigFileHelper.Decrypt(
                EncryptedData,
                pass,
                Salt,
                scryptParameters
                ));
 }
コード例 #6
0
 public static ScryptPassEncryptedData FromValue <T1>(
     T1 value,
     byte[] pass,
     CryptConfigFileHelperScryptParameters scryptParameters = null
     )
 {
     return(new ScryptPassEncryptedData(
                Encoding.UTF8.GetBytes(value.WriteObjectToJson()),
                pass,
                null,
                scryptParameters
                ));
 }
コード例 #7
0
        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));
        }
コード例 #8
0
 public bool CheckPass(
     byte[] pass,
     CryptConfigFileHelperScryptParameters scryptParameters = null
     )
 {
     try
     {
         CryptConfigFileHelper.Decrypt(
             EncryptedData,
             pass,
             Salt,
             scryptParameters
             );
         return(true);
     }
     catch
     {
         return(false);
     }
 }
コード例 #9
0
 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
         );
 }