本文将带您了解关于C#等效于AES的JavaSecretKeySpec的新内容,另外,我们还将为您提供关于#MyBatis:selectKey标签<selectKey>#keyProperty、key
本文将带您了解关于C#等效于AES的Java SecretKeySpec的新内容,另外,我们还将为您提供关于#MyBatis:selectKey标签 <selectKey> #keyProperty、keyColumn、order、resultType @FDDLC、AC#等效于C的读取文件I / O、C# 的 Java 代码的 SecretKeySpec AES 等效性、C#等效于Java的Arrays.fill()方法的实用信息。
本文目录一览:- C#等效于AES的Java SecretKeySpec
- #MyBatis:selectKey标签 <selectKey> #keyProperty、keyColumn、order、resultType @FDDLC
- AC#等效于C的读取文件I / O
- C# 的 Java 代码的 SecretKeySpec AES 等效性
- C#等效于Java的Arrays.fill()方法
C#等效于AES的Java SecretKeySpec
我有以下用Java编写的代码。我需要与此等效的C#。
Key key = new SecretKeySpec(keyValue, "AES");Cipher c = Cipher.getInstance("AES");c.init(1, key);byte[] encVal = c.doFinal(Data.getBytes());encryptedValue = new BASE64Encoder().encode(encVal);
答案1
小编典典这里的C#代码等效于Java。
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();AesManaged tdes = new AesManaged();tdes.Key = UTF8.GetBytes(keyValue);tdes.Mode = CipherMode.ECB;tdes.Padding = PaddingMode.PKCS7;ICryptoTransform crypt = tdes.CreateEncryptor();byte[] plain = Encoding.UTF8.GetBytes(text);byte[] cipher = crypt.TransformFinalBlock(plain, 0, plain.Length);String encryptedText = Convert.ToBase64String(cipher);
#MyBatis:selectKey标签 <selectKey> #keyProperty、keyColumn、order、resultType @FDDLC
如果把表的主键设为了自增长,在后面进行insert操作时,数据库自动帮我们生成了id。
比如:
insert into account (name, money) values (#{name}, #{money}) //假设name = "vip", money = "666"
执行完后,我们去看数据库,发现多了一条记录:
好,现在问题来了:看了数据库后我们知道自动生成的id是11,如果不看数据库,能不能知道这个id呢?
selectKey就能解决这个问题!
示例如下:
<insert id="insert" parameterType="cn.liuxingchang.domain.Account">
<selectKey keyProperty="id" keyColumn="id" order="AFTER" resultType="int">
select last_insert_id()
</selectKey>
insert into account (name, money) values (#{name}, #{money})
</insert>
说明:
1、keyColumn:数据表中那个自增长的id!
2、keyProperty:数据表的字段和实体类的属性是对应的,自增长的那玩意儿在实体类中对应的属性就是keyProperty!
3、order:本标签内的语句(这里是 select last_insert_id() )是先执行还是后执行!
这里order="AFTER",什么意思呢?
意思是:select last_insert_id()后执行,insert into account (name, money) values (#{name}, #{money})先执行!
AC#等效于C的读取文件I / O
谁能告诉我如何在C#.NET版本2中直接将字节数组放入结构中?像fread
在C语言中熟悉的一样,到目前为止,在读取字节流并自动填充结构方面并没有取得太大的成功。我已经看到了一些使用unsafe
关键字在托管代码中存在指针轨迹的实现。
看一下这个样本:
public unsafe struct foobarStruct{ /* fields here... */ public foobarStruct(int nFakeArgs){ /* Initialize the fields... */ } public foobarStruct(byte[] data) : this(0) { unsafe { GCHandle hByteData = GCHandle.Alloc(data, GCHandleType.Pinned); IntPtr pByteData = hByteData.AddrOfPinnedObject(); this = (foobarStruct)Marshal.PtrToStructure(pByteData, this.GetType()); hByteData.Free(); } }}
我有两个构造函数的原因 foobarStruct
- 是否不能有一个空的构造函数。
- 实例化结构时,将一块内存(作为字节数组)传入构造函数。
该实施方案是否足够好,或者有更清洁的方法来实现这一目标?
编辑: 我不想使用ISerializable接口或其实现。我正在尝试读取一个二进制映像,以计算出使用的字段并使用PE结构确定其数据。
答案1
小编典典使用P /
Invoke编组器没有任何问题,这也不是不安全的,并且您不必使用unsafe关键字。弄错它只会产生错误的数据。与显式编写反序列化代码相比,使用起来容易得多,尤其是在文件包含字符串的情况下。您不能使用BinaryReader.ReadString(),它假定字符串是由BinaryWriter编写的。但是,请确保使用struct声明声明数据的结构,this.GetType()不太可能正常工作。
这是一个通用类,可用于任何结构声明:
class StructureReader<T> where T : struct { private byte[] mBuffer; public StructureReader() { mBuffer = new byte[Marshal.SizeOf(typeof(T))]; } public T Read(System.IO.FileStream fs) { int bytes = fs.Read(mBuffer, 0, mBuffer.Length); if (bytes == 0) throw new InvalidOperationException("End-of-file reached"); if (bytes != mBuffer.Length) throw new ArgumentException("File contains bad data"); T retval; GCHandle hdl = GCHandle.Alloc(mBuffer, GCHandleType.Pinned); try { retval = (T)Marshal.PtrToStructure(hdl.AddrOfPinnedObject(), typeof(T)); } finally { hdl.Free(); } return retval; }
文件中数据结构的示例声明:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]struct Sample { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 42)] public string someString;}
您需要调整结构声明和属性,以与文件中的数据匹配。读取文件的示例代码:
var data = new List<Sample>(); var reader = new StructureReader<Sample>(); using (var stream = new FileStream(@"c:\temp\test.bin", FileMode.Open, FileAccess.Read)) { while(stream.Position < stream.Length) { data.Add(reader.Read(stream)); } }
C# 的 Java 代码的 SecretKeySpec AES 等效性
如何解决C# 的 Java 代码的 SecretKeySpec AES 等效性?
使用 Base64 解码值和 AES 算法创建 SecretKey 实例。这里的密钥长度必须为 32
此代码是用 Java 编写的,我需要 C# 的等效代码
public static SecretKey getSecretKey(String secretKey)
{
byte[] decodeSecretKey = base64Decode(secretKey);
return new SecretKeySpec(decodeSecretKey,decodeSecretKey.Length,"AES");
}
public static byte[] base64Decode(string data)
{
return Base64.getDecoder().decode(data);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
C#等效于Java的Arrays.fill()方法
我在Java中使用以下语句:
Arrays.fill(mynewArray, oldArray.Length, size, -1);
请提出等效的C#。
答案1
小编典典我不知道框架中执行此操作的任何内容,但是实现起来很容易:
// Note: start is inclusive, end is exclusive (as is conventional// in computer science)public static void Fill<T>(T[] array, int start, int end, T value){ if (array == null) { throw new ArgumentNullException("array"); } if (start < 0 || start >= end) { throw new ArgumentOutOfRangeException("fromIndex"); } if (end >= array.Length) { throw new ArgumentOutOfRangeException("toIndex"); } for (int i = start; i < end; i++) { array[i] = value; }}
或者,如果您要指定计数而不是开始/结束:
public static void Fill<T>(T[] array, int start, int count, T value){ if (array == null) { throw new ArgumentNullException("array"); } if (count < 0) { throw new ArgumentOutOfRangeException("count"); } if (start + count >= array.Length) { throw new ArgumentOutOfRangeException("count"); } for (var i = start; i < start + count; i++) { array[i] = value; }}
今天关于C#等效于AES的Java SecretKeySpec的分享就到这里,希望大家有所收获,若想了解更多关于#MyBatis:selectKey标签 <selectKey> #keyProperty、keyColumn、order、resultType @FDDLC、AC#等效于C的读取文件I / O、C# 的 Java 代码的 SecretKeySpec AES 等效性、C#等效于Java的Arrays.fill()方法等相关知识,可以在本站进行查询。
本文标签: