我不是在寻找两个返回bool的结构的比较,我想知道是否有办法获得两个结构的哪些字段(相同的结构,但可能是不同的值)是不同的.基本上我想要一个更简单的方法来执行以下操作:
public class Diff
{
public String VarName;
public object Val1;
public object Val2;
public Diff(String varName, object val1, object val2)
{
VarName = varName;
Val1 = val1;
Val2 = val2;
}
public override string ToString()
{
return VarName + " differs with values " + Val1 + " and " + Val2;
}
}
public struct TestStruct
{
public int ValueOne;
public int ValueTwo;
public int ValueThree;
public List Compare(TestStruct inTestStruct)
{
List diffs = new List();
if (ValueOne …Run Code Online (Sandbox Code Playgroud) 加密是在java中:
String salt = "DC14DBE5F917C7D03C02CD5ADB88FA41";
String password = "25623F17-0027-3B82-BB4B-B7DD60DCDC9B";
char[] passwordChars = new char[password.length()];
password.getChars(0,password.length(), passwordChars, 0);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passwordChars, salt.getBytes(), 2, 256);
SecretKey sKey = factory.generateSecret(spec);
byte[] raw = _sKey.getEncoded();
String toEncrypt = "The text to be encrypted.";
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, skey);
AlgorithmParameters params = cipher.getParameters();
byte[] initVector = params.getParameterSpec(IvParameterSpec.class).getIV();
byte[] encryptedBytes = cipher.doFinal(toEncrypt.getBytes());
Run Code Online (Sandbox Code Playgroud)
虽然解密是在c#中:
string hashAlgorithm = "SHA1";
int passwordIterations = 2;
int keySize = 256;
byte[] saltValueBytes = …Run Code Online (Sandbox Code Playgroud) 我想这样实现:
namespace PIMP.Web.TestForum.Models
{
public class ThreadModel : PagedList<T>
{
Run Code Online (Sandbox Code Playgroud)
但我得到ErrorMessage:
找不到类型或命名空间名称"T"(您是否缺少using指令或程序集引用?)
我该怎么做才能避免它?
我需要在VB6中加密一个字节数组并在C#(NET 2.0)中解密它.反之亦然(C#到VB6).
在C#中我使用了RijndaelManaged类.在VB6中,我使用了来自Internet的免费部分.最好的似乎是http://www.frez.co.uk/freecode.htm#rijndael 但这两个实现从相同的输入开始生成不同的输出:(
也许这是RijndaelManaged中IV矢量的问题......我不明白......
在VB6和NET之间使用Rijndael/AES的任何解决方案/经验?或TripleDes ....
谢谢
更新:重要:运行vb6 app的机器,没有.NET框架.所以我不能使用Interop和/或作为COM公开的.NET包装类.:(
我在这里使用AES方法:http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx
我想要一个字符串值,我将转换为字节数组并将其传递给AES加密方法.字符串应该有多少个字符来生成方法所需的正确字节数组大小?
static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the stream used to encrypt to an in memory
// array of bytes.
MemoryStream msEncrypt = null;
// Declare the RijndaelManaged object
// used to encrypt the data. …Run Code Online (Sandbox Code Playgroud) 我正在尝试从结构中获取数组值的字段信息。到目前为止,我有以下内容,但我不知道如何获得我想要的信息。
[StructLayout(LayoutKind.Sequential)]
public struct Test
{
public byte Byte1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
public Test2[] Test1;
}
BindingFlags struct_field_flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
FieldInfo[] all_struct_fields = typeof(Test).GetFields(struct_field_flags);
foreach (FieldInfo struct_field in all_struct_fields)
{
if(struct_field.FieldType.IsArray)
{
// Get FieldInfo for each value in the Test1 array within Test structure
}
}
Run Code Online (Sandbox Code Playgroud)
所以如果我这样做:
Type array_type = struct_field.FieldType.GetElementType();
Run Code Online (Sandbox Code Playgroud)
这将返回 Test2 类型,但我不想要数组的类型,我想要该结构的 FieldInfo 或 Fields,以便我可以从其中设置值。
我期待加密数据.我想基于密码生成加密密钥,以及一些移动值,如时间.目标是进行密钥更改,但让知道密码的任何人都能够解密.这发生在C#中.我使用以下代码来哈希密码.
private static string GetPasswordHash(string password)
{
TimeSpan span = (DateTime.UtcNow - new DateTime(1900, 1, 1));
string result = Convert.ToInt32(span.TotalHours).ToString();
result += password;
result += Convert.ToInt32(span.TotalDays).ToString();
result = Convert.ToBase64String(SHA256.Create().ComputeHash(Encoding.ASCII.GetBytes(result)));
return result;
}
Run Code Online (Sandbox Code Playgroud)
然后我使用该哈希,加上盐来生成密钥.
Rfc2898DeriveBytes rdb = new Rfc2898DeriveBytes(GetPasswordHash(password), salt);
rdb.IterationCount = 1000;
RijndaelManaged rm = new RijndaelManaged();
rm.KeySize = 256;
rm.Key = rdb.GetBytes(32);
rm.IV = rdb.GetBytes(16);
Run Code Online (Sandbox Code Playgroud)
我这样做的方式似乎有问题.有些计算机位于不同的时区,或者当我发送数据时小时计时,或者机器时间稍微偏离.有更好的建议吗?
我有 2 个网站:一个用经典 asp 编写,另一个用 ASP.NET(1.1 框架)编写。这两个应用程序都使用登录机制根据共享数据库表验证用户凭据。到目前为止,密码都存储在 1 向 MD5 哈希中,这意味着如果人们丢失旧密码,则必须为其提供一个新生成的密码。我现在想改变这一点并使密码可解密。
我发现这个 Rijndael 代码与经典的 asp 一起使用:http : //www.frez.co.uk/freecode.htm#rijndael
但是我找不到 ASP.NET 的相同解决方案。我试过这个,但它给了我经典asp和ASP.NET代码之间不同的加密和解密结果:
If Not String.IsNullOrEmpty(TextBox1.Text) And Not String.IsNullOrEmpty(TextBox2.Text) Then
Dim password = TextBox1.Text
Dim key = TextBox2.Text
Dim keyGenerator = New Rfc2898DeriveBytes(key, 8)
Dim r = New RijndaelManaged
r.Mode = CipherMode.CBC
r.Padding = PaddingMode.Zeros
r.BlockSize = 256
r.KeySize = 256
r.FeedbackSize = 256
r.IV = keyGenerator.GetBytes(CType(r.BlockSize / 8, Integer))
r.Key = keyGenerator.GetBytes(CType(r.KeySize / 8, Integer))
Dim transform As ICryptoTransform = …Run Code Online (Sandbox Code Playgroud) 问题描述:
在你回答之前:
1)是的,事情需要这样
2)以下代码示例:
public partial class Form1 : Form
{
List<UserControl2> list;
public Form1()
{
InitializeComponent();
list = new List<UserControl2>();
for (int i = 0; i < 20; i++)
{
UserControl2 c = new UserControl2();
list.Add(c);
}
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (UserControl2 c in list)
userControl11.Controls.Add(c);
}
private void button1_Click(object sender, EventArgs e)
{
int y …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像的课
public class MyClass
{
public string EmployerName;
public string EmployerSurname;
public string EmploeeName;
public string EmploeeSurname;
}
Run Code Online (Sandbox Code Playgroud)
我已经重构了上面的代码:
public class MyClass
{
public MyClass()
{
Employer = new PersonInfo();
Emploee = new PersonInfo();
}
public class PersonInfo
{
public string Name;
public string Surname;
}
public PersonInfo Emploee;
public PersonInfo Employer;
[Obsolete]
public string EmploeeName
{
get
{
return Emploee.Name;
}
set
{
Emploee.Name = value;
}
}
[Obsolete]
public string EmploeeSurname
{
get
{
return Emploee.Surname;
}
set …Run Code Online (Sandbox Code Playgroud) c# ×9
aes ×2
asp.net ×2
encryption ×2
rijndael ×2
struct ×2
asp-classic ×1
compare ×1
comparison ×1
cryptography ×1
fieldinfo ×1
java ×1
passwords ×1
position ×1
reflection ×1
scroll ×1
timezone ×1
vb6 ×1
winforms ×1
xml ×1