兰姆达表达式 C#代码 用VB代码疑问,这个是查资料套上的。 不知道为什么这样可以成功
array as sbyte()=new sbyte() { -84 , 99 , -7 }c#.NET代码:
System.Array.ConvertAll<sbyte, byte>(array, a => (byte) a)
代码:
System.Array.ConvertAll(Of SByte, Byte)(array, Function(a) (IIf(a > 0, a, 256 - Math.Abs(a))))
array参数是定义好的sbyte()
套嵌 iif(a>0,cbyte(a),cbyte(256-Math.Abs(a))) 时候出错,array数组里面有负数
把cbyte 壳去掉竟然好了
里面没有更好的转换方法么?将SBbyte转换Byte
【完整过程代码】
Public Shared Function DecryptionKEY(ByVal message As String) As String
Try
Dim keystring As String = "q1!2@3#we>WacE/.Q,?<b"
Dim array As SByte() = New SByte() {-87, -101, -56, 50, &H56, &H35, -29, 3}
Dim num As Integer = &H13
Dim generator As New PKCSKeyGenerator(keystring, System.Array.ConvertAll(Of SByte, Byte)(array, Function(a) (IIf(a > 0, a, 256 - Math.Abs(a)))), num, 1)
Dim inputBuffer As Byte() = Convert.FromBase64String(message)
Dim bytes As Byte() = generator.Decryptor.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length)
Return Encoding.UTF8.GetString(bytes)
Catch ex As Exception
Return "ERROR CODE 702"
End Try
End Function
【C#完整代码】
public static String DecryptionKEY(String message)
{
try
{
String key = "q1!2@3#we>WacE/.Q,?<b";
sbyte[] salt = { -87, -101, -56, 50, 86, 53, -29, 3 };
int count = 19;
PKCSKeyGenerator cipher = new PKCSKeyGenerator(key, Array.ConvertAll(salt, a => (byte)a), count, 1);
byte[] src = Convert.FromBase64String(message);
byte[] result = cipher.Decryptor.TransformFinalBlock(src, 0, src.Length);
return Encoding.UTF8.GetString(result);
}
catch
{
return "DecryptionKEY Fail.";
}
}
[ 本帖最后由 hn496658432 于 2014-4-8 23:00 编辑 ]