在C#中满足以下内容的最现代(最佳)方式是什么?
string encryptedString = SomeStaticClass.Encrypt(sourceString);
string decryptedString = SomeStaticClass.Decrypt(encryptedString);
Run Code Online (Sandbox Code Playgroud)
但最小的涉及盐,键,与字节[]等混乱的大惊小怪等.
谷歌搜索和混淆我发现的东西(你可以看到类似的SO Q列表,看这是一个欺骗性的问题).
我正在使用Asp.Net-Identity-2,我正在尝试使用以下方法验证电子邮件验证码.但我收到"无效令牌"错误消息.
我的应用程序的用户管理器是这样的:
public class AppUserManager : UserManager<AppUser>
{
public AppUserManager(IUserStore<AppUser> store) : base(store) { }
public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options, IOwinContext context)
{
AppIdentityDbContext db = context.Get<AppIdentityDbContext>();
AppUserManager manager = new AppUserManager(new UserStore<AppUser>(db));
manager.PasswordValidator = new PasswordValidator {
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = true,
RequireUppercase = true
};
manager.UserValidator = new UserValidator<AppUser>(manager)
{
AllowOnlyAlphanumericUserNames = true,
RequireUniqueEmail = true
};
var dataProtectionProvider = options.DataProtectionProvider;
//token life span is 3 hours …Run Code Online (Sandbox Code Playgroud)我使用rsa密钥加密一个长字符串,我将发送到我的服务器(将使用服务器的公钥和我的私钥加密它)但它抛出一个异常,就像javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes
我觉得我还没有理解rsa的工作到现在为止(使用内置库是导致此问题的原因).
有人可以解释为什么抛出这个异常.是不是可以发送长字符串加密?
我需要一些简单的字符串加密,所以我写了下面的代码(从这里有很多"灵感" ):
// create and initialize a crypto algorithm
private static SymmetricAlgorithm getAlgorithm(string password) {
SymmetricAlgorithm algorithm = Rijndael.Create();
Rfc2898DeriveBytes rdb = new Rfc2898DeriveBytes(
password, new byte[] {
0x53,0x6f,0x64,0x69,0x75,0x6d,0x20, // salty goodness
0x43,0x68,0x6c,0x6f,0x72,0x69,0x64,0x65
}
);
algorithm.Padding = PaddingMode.ISO10126;
algorithm.Key = rdb.GetBytes(32);
algorithm.IV = rdb.GetBytes(16);
return algorithm;
}
/*
* encryptString
* provides simple encryption of a string, with a given password
*/
public static string encryptString(string clearText, string password) {
SymmetricAlgorithm algorithm = getAlgorithm(password);
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText); …Run Code Online (Sandbox Code Playgroud) 我有一个程序可以保存一个带有高分的.txt文件:
// Create a file to write to.
string createHighscore = _higscore + Environment.NewLine;
File.WriteAllText(path, createText);
// Open the file to read from.
string createHighscore = File.ReadAllText(path);
Run Code Online (Sandbox Code Playgroud)
问题是用户可以使用texteditor尽可能简单地编辑文件.所以我想让文件不可读/不可编辑或加密它.
我的想法是我可以将数据保存在资源文件中,但是我可以在资源文件中写入吗?或者将其保存为.dll,加密/解密或查找MD5-sum/hash.
我希望在C#中简单加密和解密密码.如何在数据库中以加密格式保存密码并通过解密检索原始格式?
我想加密连接字符串中的密码.当我连接到DB时,连接字符串公开存储在App.config中,我需要找到一种方法来保持只加密密码.
在使用对称加密算法(在本例中为AES)加密和解密数据时,我试图理解如何处理和管理启动向量和盐(适用时).
我从不同的SO线程和各种其他网站推断出,IV或盐都不需要保密,只是为了防御密码分析攻击,例如蛮力攻击.考虑到这一点,我认为将伪随机IV与加密数据一起存储是可行的.我问的是我使用的方法是否合适,而且,我是否应该以同样的方式处理我目前的硬编码盐?那就是将它写在IV旁边的内存流中
我的代码:
private const ushort ITERATIONS = 300;
private static readonly byte[] SALT = new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c };
private static byte[] CreateKey(string password, int keySize)
{
DeriveBytes derivedKey = new Rfc2898DeriveBytes(password, SALT, ITERATIONS);
return derivedKey.GetBytes(keySize >> 3);
}
public static byte[] Encrypt(byte[] data, string password)
{
byte[] encryptedData = null;
using (AesCryptoServiceProvider provider = new AesCryptoServiceProvider())
{
provider.GenerateIV();
provider.Key = CreateKey(password, provider.KeySize);
provider.Mode …Run Code Online (Sandbox Code Playgroud) 我正在编写一个C#应用程序,需要在启动时读取大约130,000(String,Int32)对到Dictionary.这些对存储在.txt文件中,因此很容易被任何人修改,这在上下文中是危险的.我想问一下是否有办法保存这个词典,以便可以合理安全地存储信息,而不会在启动时失去性能.我尝试过使用BinaryFormatter,但问题是原始程序在启动时需要125ms到250ms来读取txt中的信息并构建字典,反序列化生成的二进制文件需要2s,这本身并不太多但与原始性能相比,速度降低了8-16倍.
注意:加密很重要,但最重要的应该是从磁盘保存和读取字典的方法 - 可能来自二进制文件 - 而不必在每一行上使用Convert.ToInt32,从而提高性能.
c# ×9
encryption ×6
.net ×2
cryptography ×2
ado.net ×1
asp.net ×1
binary ×1
exception ×1
java ×1
md5sum ×1
obfuscation ×1
performance ×1
rsa ×1