- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
public string Encrypt(string Code)
{
string result = string.Empty;
byte[] encryptResult = null;
var CodeInByte = Encoding.ASCII.GetBytes(Code);
try
{
using (MemoryStream memo = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = KeySize;
AES.BlockSize = BlockSize;
var key = new Rfc2898DeriveBytes(CodeInByte, salt, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var encrypt = new CryptoStream(memo, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
encrypt.Write(CodeInByte, 0, CodeInByte.Length);
encrypt.Close();
}
encryptResult = memo.ToArray();
}
}
result = Convert.ToBase64String(encryptResult);
return result;
}
catch (Exception err)
{
MsgCode = 99;
MsgDesc = err.Message;
return string.Empty;
}
}
只是一个简单的字符串AES加密方法
我想问的一点,在生成 key 的时候,在
var key = new Rfc2898DeriveBytes(CodeInByte, salt, 1000);
key 是由输入的字符串生成的,还是只是一个随机生成的字节数组?
并且,盐是否需要是静态的
最佳答案
Rfc2898DeriveBytes takes a password, a salt, and an iteration count, and then generates keys through calls to the GetBytes method.
换句话说,它将使用您提供的输入参数派生字节。如果你给它不同的参数,派生的 key 就会不同。如果您给它相同的参数,它将生成相同的字节。
对称加密算法(例如 AES)需要固定长度的 key - 在本例中为 16 字节的 AES128。但是,您不想强制密码长度固定,因为这会使它们更容易受到攻击。您可能还需要比可行密码更长的 key - 例如,AES256 需要 32 字节的 key 。最后,密码往往是字母数字并且可能有一些符号,而加密 key 由范围为 0x00-0xFF 的字节组成,如果您将加密 key 设置为 32 个字符的 ASCII 密码,那么您将大大缩小范围因为可打印的 ASCII 字符范围远小于 0x00-0xFF。
出于这个原因,您希望从给定的密码中导出加密 key ,这样您就可以获得所需长度的强 key 。这就是 Rfc2898DeriveBytes 的用武之地。
关于c# - Rfc2898DeriveBytes 的输出取决于什么,应该如何处理盐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43252590/
我正在使用一个以明文形式存储密码的旧应用程序。我已将应用程序移植到 spring 3 mvc + security。我还成功地让 spring security 使用 sha256 + 基于用户名的盐
我试图按照 here. 中的代码获取哈希密码从中,我目前只使用 salt 的代码。方法,hash方法和isExpectedPassword方法。 我从文本字段获取密码: char[] passChar
我已经阅读了有关此问题的许多问题,但许多答案相互矛盾或我不明白。 您应该始终将密码存储为哈希值,而不是纯文本。但是您是否应该将盐(对于每个用户来说都是唯一的)存储在数据库中的散列密码+盐旁边。这对我来
我有一些 php 脚本,使我的用户能够使用电子邮件创建帐户。然后,他设置了一个经过哈希处理的密码,生成了一个盐并将其存储在我的数据库中。现在我正在重置密码,工作正常,但新密码(更改密码,即使其正确的密
我在申请中使用 shiro 进行身份验证。我使用加盐的散列密码,并将它们存储在我的数据库中,如下所示: private User createUserWithHashedPassword(St
我正在尝试找出在桌面应用程序中应该将应用程序 secret 和 key 存储在何处或如何存储。 例如 facebook 应用程序 key 或 dropbox key 和 secret 。 所以我读到我
ASP.NET 成员资格如何生成他们的 salt key ,然后他们如何对其进行编码(即是 salt + 密码还是密码 + salt)? 我正在使用 SHA-1使用我的成员(member)资格,但我想
我正在设置一个cookie。像这样的东西: $_COOKIE['test'] = SHA1('124'.'mysalt'); 现在 124 是我想要的 ID。所以在我的 MySQL 表中,我尝试运行如
我的应用需要加密一些数据(用户 session token )。我看到的大多数示例都有一种使用密码和盐生成 key 的方法,例如: public static Key generateKey(char
我想在数据库中的密码旁边存储一个(随机)盐。现在,问题是: 我应该将其存储为哈希值还是纯文本?有什么区别吗(更安全,更快?)?我应该付出多少努力来创建随机字符串? 示例代码: //Creati
当我收到此错误时,我正要在 Windows Azure 上部署我的服务: Error: The provided configuration file contains XML that could
如何在 C# 中创建以下 PHP 代码? PHP代码: 我将不胜感激任何形式的帮助。一直在测试以下内容: https://stackoverflow.com/a/1300927/7312781 但它
谁能推荐一个使用 javascript 创建 sha1 salt 的好方法? 最佳答案 首先,阅读盐的用途以确保您理解它。 This和 this会让您入门,但您应该阅读更多内容。 基本上,任何适当大小
我知道这可能是一个常见问题,网上有很多关于为密码哈希生成安全盐的文章。到目前为止,我已经了解到mcrypt_create_iv通常用于加密的初始化向量,它可用于安全地创建盐,特别是在针对MCRYPT_
我知道这个话题已经被讨论了一百万次。但这对我来说是新的,我读得越多,就越不了解实际发生或应该发生的事情。 我在用户密码的散列存储中添加了每个用户盐,因此存储的密码散列如下所示:hash(passwor
我正在学习本教程 ( http://kowsercse.com/2011/09/04/kohana-tutorial-beginners/ ) 并遇到此错误消息: Kohana_Exception [
我是一名优秀的程序员,十分优秀!