爬虫学习笔记,和指标之间的转移

By admin in 4858.com on 2019年4月11日

       
Internet上的1对站点平时存在着镜像网址(mirror),即多少个网站的内容一致但网页对应的域名不一致。那样会导致对同样份网页爬虫重复抓取数11遍。为了制止那种境况,对于每1份抓取到的网页,它首先须求进入ContentSeen模块。该模块会咬定网页的始末是或不是和已下载过的有个别网页的内容千篇一律,要是相同,则该网页不会再被送去举办下一步的拍卖。那样的做法能够显然的大跌爬虫供给下载的网页数。至于假设判断多少个网页的剧情是不是同样,1般的笔触是如此的:并不会去直接比较八个网页的内容,而是将网页的内容通过计量生成FingerPrint(指纹),常常FingerPrint是一个恒定长度的字符串,要比网页的正文短很多。要是三个网页的FingerPrint一样,则认为它们内容完全相同。

       
为了形成那1模块,首先大家供给2个强硬的指纹算法,将大家的网页内容总计成指纹存入数据库,下次直接判断指纹在保留前透过指纹的对峙统一即可成功实现去重新操作。

       
首先来看一下显赫的谷歌公司采纳的网页去重新算法SimHash吧:

        谷歌Moses Charikar发布的1篇散文“detecting near-duplicates
for web
crawling”中提出了simhash算法,专门用来缓解亿万级别的网页的去重任务。

        SimHash作为locality sensitive hash(局地敏感哈希)的1种:

       
其关键思考是降维,将高维的天性向量映射成低维的特征向量,通过八个向量的Hamming
Distance来明确作品是否再一次可能中度类似。

        当中,Hamming
Distance,又称汉明距离,在音信论中,三个等长字符串之间的汉明距离是四个字符串对应地点的例外字符的个数。也等于说,它正是将一个字符串变换成别的三个字符串所急需替换的字符个数。例如:十11十一 与 十0十01之间的汉明距离是
二。至于我们常说的字符串编辑距离则是形似方式的汉明距离。

       
如此,通过比较七个文书档案的SimHash值的海明距离,能够得到它们的相似度。

       
详情能够看那里SimHash算法

____________________________________________________________________________________________________________

上边大家来拓展代码实现:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Crawler.Common
{
public class SimHashAnalyser
{

private const int HashSize = 32;

public static float GetLikenessValue(string needle, string haystack, TokeniserType type = TokeniserType.Overlapping)
{
var needleSimHash = GetSimHash(needle, type);
var hayStackSimHash = GetSimHash(haystack, type);
return GetLikenessValue(needleSimHash, hayStackSimHash);
}

public static float GetLikenessValue(int needleSimHash, int hayStackSimHash)
{
return (HashSize – GetHammingDistance(needleSimHash, hayStackSimHash)) / (float)HashSize;
}

private static IEnumerable DoHashTokens(IEnumerable tokens)
{
return tokens.Select(token => token.GetHashCode()).ToList();
}

private static int GetHammingDistance(int firstValue, int secondValue)
{
var hammingBits = firstValue ^ secondValue;
var hammingValue = 0;
for (var i = 0; i < 32; i++) if (IsBitSet(hammingBits, i)) hammingValue += 1; return hammingValue; }
private static bool IsBitSet(int b, int pos)
{
return (b & (1 << pos)) != 0; } public static int GetSimHash(string input) { return GetSimHash(input, TokeniserType.Overlapping); }
public static int GetSimHash(string input, TokeniserType tokeniserType)
{
ITokeniser tokeniser;
if (tokeniserType == TokeniserType.Overlapping)
tokeniser = new OverlappingStringTokeniser();
else
tokeniser = new FixedSizeStringTokeniser();

var hashedtokens = DoHashTokens(tokeniser.Tokenise(input));
var vector = new int[HashSize];
for (var i = 0; i < HashSize; i++) vector[i] = 0;
foreach (var value in hashedtokens)
for (var j = 0; j < HashSize; j++) if (IsBitSet(value, j)) vector[j] += 1; else vector[j] -= 1; var fingerprint = 0; for (var i = 0; i < HashSize; i++) if (vector[i] > 0)
fingerprint += 1 << i; return fingerprint; }
}

public interface ITokeniser
{
IEnumerable Tokenise(string input);
}

public class FixedSizeStringTokeniser : ITokeniser
{
private readonly ushort _tokensize;
public FixedSizeStringTokeniser(ushort tokenSize = 5)
{
if (tokenSize < 二) throw new ArgumentException("Token 不能够超出范围"); if (tokenSize > 1二7)
throw new ArgumentException(“Token 无法超出范围”);
_tokensize = tokenSize;
}

public IEnumerable Tokenise(string input)
{
var chunks = new List();
var offset = 0;
while (offset < input.Length) { chunks.Add(new string(input.Skip(offset).Take(_tokensize).ToArray())); offset += _tokensize; } return chunks; }
}

public class OverlappingStringTokeniser : ITokeniser
{

private readonly ushort _chunkSize;
private readonly ushort _overlapSize;

public OverlappingStringTokeniser(ushort chunkSize = 4, ushort overlapSize = 3)
{
if (chunkSize <= overlapSize) throw new ArgumentException("Chunck 必须超出 overlap"); _overlapSize = overlapSize; _chunkSize = chunkSize; }
public IEnumerable Tokenise(string input)
{
var result = new List();
var position = 0;
while (position < input.Length - _chunkSize) { result.Add(input.Substring(position, _chunkSize)); position += _chunkSize - _overlapSize; } return result; } }
public enum TokeniserType
{
Overlapping,
FixedSize
}
}

 

调用方法如下:

var s1 = "the cat sat on the mat.";
var s2 = "the cat sat on a mat.";

var similarity = SimHashAnalyser.GetLikenessValue(s1, s2);

Console.Clear();
Console.WriteLine(“相似度: {0}%”, similarity * 100);
Console.ReadKey();

 

输出为:

相似度: 78.125%

接下来就是对ContentSeen模块的简单封装:

using Crawler.Common;

namespace Crawler.Processing
{
///

/// 对于每一份抓取到的网页,它首先需要进入Content Seen模块。该模块会判断网页的内容是否和已下载过的某个网页的内容一致,如果一致,则该网页不会再被送去进行下一步的处理。
///

public class ContentSeen
{
public static int GetFingerPrint(string html)
{
return SimHashAnalyser.GetSimHash(html);
}

public static float Similarity(int print1, int print2)
{
return SimHashAnalyser.GetLikenessValue(print1, print2);
}

}
}

       
Internet上的一部分站点平时存在着镜像网址(mirror),即三个网址的始末壹致但网页对应的域名不一致。那样会造成对同样份网页爬虫重复抓取数次。为了防止那种意况,对于每一份抓取到的网页,它首先供给进入ContentSeen模块。该模块会咬定网页的情节是不是和已下载过的有个别网页的始末千篇一律,假设壹致,则该网页不会再被送去开展下一步的拍卖。那样的做法能够分明的下挫爬虫要求下载的网页数。至于如若判断多个网页的内容是还是不是相同,一般的思绪是这么的:并不会去一向比较七个网页的始末,而是将网页的内容通过总结生成FingerPrint(指纹),平常FingerPrint是多少个定位长度的字符串,要比网页的正文短很多。若是八个网页的FingerPrint1样,则觉得它们内容完全相同。

       
为了成功那1模块,首先我们须要五个有力的指印算法,将大家的网页内容总结成指纹存入数据库,下次直接判断指纹在保存前经过指纹的自己检查自纠即可成功做到去重新操作。

       
首先来看一下众所周知的谷歌公司动用的网页去重新算法SimHash吧:

        谷歌Moses Charikar发布的壹篇杂谈“detecting near-duplicates
for web
crawling”中提出了simhash算法,专门用来化解亿万级别的网页的去重职分。

        SimHash作为locality sensitive hash(局部敏感哈希)的1种:

       
其首要思索是降维,将高维的性状向量映射成低维的特征向量,通过五个向量的Hamming
Distance来分明作品是或不是再一次恐怕中度类似。

        其中,Hamming
Distance,又称汉明距离,在音讯论中,七个等长字符串之间的汉明距离是多少个字符串对应地点的不等字符的个数。也正是说,它正是将三个字符串变换成别的二个字符串所急需替换的字符个数。例如:十11拾1 与 100十01之间的汉明距离是
贰。至于大家常说的字符串编辑距离则是形似情势的汉明距离。

       
如此,通过比较多少个文书档案的SimHash值的海明距离,能够拿走它们的相似度。

       
详情能够看那里SimHash算法

____________________________________________________________________________________________________________

上边我们来展开代码完结:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Crawler.Common
{
public class SimHashAnalyser
{

private const int HashSize = 32;

public static float GetLikenessValue(string needle, string haystack, TokeniserType type = TokeniserType.Overlapping)
{
var needleSimHash = GetSimHash(needle, type);
var hayStackSimHash = GetSimHash(haystack, type);
return GetLikenessValue(needleSimHash, hayStackSimHash);
}

public static float GetLikenessValue(int needleSimHash, int hayStackSimHash)
{
return (HashSize – GetHammingDistance(needleSimHash, hayStackSimHash)) / (float)HashSize;
}

private static IEnumerable DoHashTokens(IEnumerable tokens)
{
return tokens.Select(token => token.GetHashCode()).ToList();
}

private static int GetHammingDistance(int firstValue, int secondValue)
{
var hammingBits = firstValue ^ secondValue;
var hammingValue = 0;
for (var i = 0; i < 32; i++) if (IsBitSet(hammingBits, i)) hammingValue += 1; return hammingValue; }
private static bool IsBitSet(int b, int pos)
{
return (b & (1 << pos)) != 0; } public static int GetSimHash(string input) { return GetSimHash(input, TokeniserType.Overlapping); }
public static int GetSimHash(string input, TokeniserType tokeniserType)
{
ITokeniser tokeniser;
if (tokeniserType == TokeniserType.Overlapping)
tokeniser = new OverlappingStringTokeniser();
else
tokeniser = new FixedSizeStringTokeniser();

var hashedtokens = DoHashTokens(tokeniser.Tokenise(input));
var vector = new int[HashSize];
for (var i = 0; i < HashSize; i++) vector[i] = 0;
foreach (var value in hashedtokens)
for (var j = 0; j < HashSize; j++) if (IsBitSet(value, j)) vector[j] += 1; else vector[j] -= 1; var fingerprint = 0; for (var i = 0; i < HashSize; i++) if (vector[i] > 0)
fingerprint += 1 << i; return fingerprint; }
}

public interface ITokeniser
{
IEnumerable Tokenise(string input);
}

public class FixedSizeStringTokeniser : ITokeniser
{
private readonly ushort _tokensize;
public FixedSizeStringTokeniser(ushort tokenSize = 5)
{
if (tokenSize < 二) throw new ArgumentException("Token 不能超出范围"); if (tokenSize > 127)
throw new ArgumentException(“Token 不可能超出范围”);
_tokensize = tokenSize;
}

public IEnumerable Tokenise(string input)
{
var chunks = new List();
var offset = 0;
while (offset < input.Length) { chunks.Add(new string(input.Skip(offset).Take(_tokensize).ToArray())); offset += _tokensize; } return chunks; }
}

public class OverlappingStringTokeniser : ITokeniser
{

private readonly ushort _chunkSize;
private readonly ushort _overlapSize;

public OverlappingStringTokeniser(ushort chunkSize = 4, ushort overlapSize = 3)
{
if (chunkSize <= overlapSize) throw new ArgumentException("Chunck 必须高于 overlap"); _overlapSize = overlapSize; _chunkSize = chunkSize; }
public IEnumerable Tokenise(string input)
{
var result = new List();
var position = 0;
while (position < input.Length - _chunkSize) { result.Add(input.Substring(position, _chunkSize)); position += _chunkSize - _overlapSize; } return result; } }
public enum TokeniserType
{
Overlapping,
FixedSize
}
}

 

调用方法如下:

var s1 = "the cat sat on the mat.";
var s2 = "the cat sat on a mat.";

var similarity = SimHashAnalyser.GetLikenessValue(s1, s2);

Console.Clear();
Console.WriteLine(“相似度: {0}%”, similarity * 100);
Console.ReadKey();

 

输出为:

相似度: 78.125%

接下来就是对ContentSeen模块的简单封装:

using Crawler.Common;

namespace Crawler.Processing
{
///

/// 对于每一份抓取到的网页,它首先需要进入Content Seen模块。该模块会判断网页的内容是否和已下载过的某个网页的内容一致,如果一致,则该网页不会再被送去进行下一步的处理。
///

public class ContentSeen
{
public static int GetFingerPrint(string html)
{
return SimHashAnalyser.GetSimHash(html);
}

public static float Similarity(int print1, int print2)
{
return SimHashAnalyser.GetLikenessValue(print1, print2);
}

}
}

 

print?/// <summary>  
 /// List转成json  
 /// </summary>  
 /// <typeparam name=”T”></typeparam>  
 /// <param name=”jsonName”></param>  
 /// <param name=”list”></param>  
 /// <returns></returns>  
 public static string ListToJson<T>(IList<T> list, string
jsonName) 
 { 
     StringBuilder Json = new StringBuilder(); 
     if (string.IsNullOrEmpty(jsonName)) 
         jsonName = list[0].GetType().Name; 
     Json.Append(“{\”” + jsonName + “\”:[“); 
     if (list.Count > 0) 
     { 
         for (int i = 0; i < list.Count; i++) 
         { 
             T obj = Activator.CreateInstance<T>(); 
             PropertyInfo[] pi = obj.GetType().GetProperties(); 
             Json.Append(“{“); 
             for (int j = 0; j < pi.Length; j++) 
             { 
                 Type type = pi[j].GetValue(list[i],
null).GetType(); 
                 Json.Append(“\”” + pi[j].Name.ToString() + “\”:” +
StringFormat(pi[j].GetValue(list[i], null).ToString(), type)); 
 
                 if (j < pi.Length – 1) 
                 { 
                     Json.Append(“,”); 
                 } 
             } 
             Json.Append(“}”); 
             if (i < list.Count – 1) 
             { 
                 Json.Append(“,”); 
             } 
         } 
     } 
     Json.Append(“]}”); 
     return Json.ToString(); 
 } 
 
 /// <summary>  
 /// List转成json  
 /// </summary>  
 /// <typeparam name=”T”></typeparam>  
 /// <param name=”list”></param>  
 /// <returns></returns>  
 public static string ListToJson<T>(IList<T> list) 
 { 
     object obj = list[0]; 
     return ListToJson<T>(list, obj.GetType().Name); 
 } 
 
 /// <summary>  
 /// 对象转换为Json字符串  
 /// </summary>  
 /// <param name=”jsonObject”>对象</param>  
 /// <returns>Json字符串</returns>  
 public static string ToJson(object jsonObject) 
 { 
     try 
     { 
         StringBuilder jsonString = new StringBuilder(); 
         jsonString.Append(“{“); 
         PropertyInfo[] propertyInfo =
jsonObject.GetType().GetProperties(); 
         for (int i = 0; i < propertyInfo.Length; i++) 
         { 
             object objectValue =
propertyInfo[i].GetGetMethod().Invoke(jsonObject, null); 
             if (objectValue == null) 
             { 
                 continue; 
             } 
             StringBuilder value = new StringBuilder(); 
             if (objectValue is DateTime || objectValue is Guid ||
objectValue is TimeSpan) 
             { 
                 value.Append(“\”” + objectValue.ToString() +
“\””); 
             } 
             else if (objectValue is string) 
             { 
                 value.Append(“\”” + objectValue.ToString() +
“\””); 
             } 
             else if (objectValue is IEnumerable) 
             { 
                 value.Append(ToJson((IEnumerable)objectValue)); 
             } 
             else 
             { 
                 value.Append(“\”” + objectValue.ToString() +
“\””); 
             } 
             jsonString.Append(“\”” + propertyInfo[i].Name + “\”:” +
value + “,”); ; 
         } 
         return jsonString.ToString().TrimEnd(‘,’) + “}”; 
     } 
     catch (Exception ex) 
     { 
         throw ex; 
     } 
 } 
 
 /// <summary>  
 /// 对象集合转换Json  
 /// </summary>  
 /// <param name=”array”>集合对象</param>  
 /// <returns>Json字符串</returns>  
 public static string ToJson(IEnumerable array) 
 { 
     string jsonString = “[“; 
     foreach (object item in array) 
     { 
         jsonString += ToJson(item) + “,”; 
     } 
     jsonString.Remove(jsonString.Length – 1, jsonString.Length); 
     return jsonString + “]”; 
 } 
 
 /// <summary>  
 /// 普通集合转换Json  
 /// </summary>  
 /// <param name=”array”>集合对象</param>  
 /// <returns>Json字符串</returns>  
 public static string ToArrayString(IEnumerable array) 
 { 
     string jsonString = “[“; 
     foreach (object item in array) 
     { 
         jsonString = ToJson(item.ToString()) + “,”; 
     } 
     jsonString.Remove(jsonString.Length – 1, jsonString.Length); 
     return jsonString + “]”; 
 } 
 
 /// <summary>  
 /// Datatable转换为Json  
 /// </summary>  
 /// <param name=”table”>Datatable对象</param>  
 /// <returns>Json字符串</returns>  
 public static string ToJson(DataTable dt) 
 { 
     StringBuilder jsonString = new StringBuilder(); 
     jsonString.Append(“[“); 
     DataRowCollection drc = dt.Rows; 
     for (int i = 0; i < drc.Count; i++) 
     { 
         jsonString.Append(“{“); 
         for (int j = 0; j < dt.Columns.Count; j++) 
         { 
             string strKey = dt.Columns[j].ColumnName; 
             string strValue = drc[i][j].ToString(); 
             Type type = dt.Columns[j].DataType; 
             jsonString.Append(“\”” + strKey + “\”:”); 
             strValue = StringFormat(strValue, type); 
             if (j < dt.Columns.Count – 1) 
             { 
                 jsonString.Append(strValue + “,”); 
             } 
             else 
             { 
                 jsonString.Append(strValue); 
             } 
         } 
         jsonString.Append(“},”); 
     } 
     jsonString.Remove(jsonString.Length – 1, 1); 
     jsonString.Append(“]”); 
     return jsonString.ToString(); 
 } 
 
 /// <summary>  
 /// DataTable转成Json  
 /// </summary>  
 /// <param name=”jsonName”></param>  
 /// <param name=”dt”></param>  
 /// <returns></returns>  
 public static string ToJson(DataTable dt, string jsonName) 
 { 
     StringBuilder Json = new StringBuilder(); 
     if (string.IsNullOrEmpty(jsonName)) 
         jsonName = dt.TableName; 
     Json.Append(“{\”” + jsonName + “\”:[“); 
     if (dt.Rows.Count > 0) 
     { 
         for (int i = 0; i < dt.Rows.Count; i++) 
         { 
             Json.Append(“{“); 
             for (int j = 0; j < dt.Columns.Count; j++) 
             { 
                 Type type = dt.Rows[i][j].GetType(); 
                 Json.Append(“\”” +
dt.Columns[j].ColumnName.ToString() + “\”:” +
StringFormat(dt.Rows[i][j].ToString(), type)); 
                 if (j < dt.Columns.Count – 1) 
                 { 
                     Json.Append(“,”); 
                 } 
             } 
             Json.Append(“}”); 
             if (i < dt.Rows.Count – 1) 
             { 
                 Json.Append(“,”); 
             } 
         } 
     } 
     Json.Append(“]}”); 
     return Json.ToString(); 
 } 
 
 /// <summary>  
 /// DataReader转换为Json  
 /// </summary>  
 /// <param name=”dataReader”>DataReader对象</param>  
 /// <returns>Json字符串</returns>  
 public static string ToJson(IDataReader dataReader) 
 { 
     StringBuilder jsonString = new StringBuilder(); 
     jsonString.Append(“[“); 
 
     while (dataReader.Read()) 
     { 
         jsonString.Append(“{“); 
         for (int i = 0; i < dataReader.FieldCount; i++) 
         { 
             Type type = dataReader.GetFieldType(i); 
             string strKey = dataReader.GetName(i); 
             string strValue = dataReader[i].ToString(); 
             jsonString.Append(“\”” + strKey + “\”:”); 
             strValue = StringFormat(strValue, type); 
             if (i < dataReader.FieldCount – 1) 
             { 
                 jsonString.Append(strValue + “,”); 
             } 
             else 
             { 
                 jsonString.Append(strValue); 
             } 
         } 
         jsonString.Append(“},”); 
     } 
     dataReader.Close(); 
     jsonString.Remove(jsonString.Length – 1, 1); 
     jsonString.Append(“]”); 
     if (jsonString.Length == 1) 
     { 
         return “[]”; 
     } 
     return jsonString.ToString(); 
 } 
 
 /// <summary>  
 /// DataSet转换为Json  
 /// </summary>  
 /// <param name=”dataSet”>DataSet对象</param>  
 /// <returns>Json字符串</returns>  
 public static string ToJson(DataSet dataSet) 
 { 
     string jsonString = “{“; 
     foreach (DataTable table in dataSet.Tables) 
     { 
         jsonString += “\”” + table.TableName + “\”:” + ToJson(table)

C#下奥德赛SA算法的实现(适用于支付宝和易宝支付)

 

 

目录(?)[-]

  1. SportageSA算法代码
  2. TiggoSA算法测试代码

 

  • “,”; 
         } 
         jsonString = jsonString.TrimEnd(‘,’); 
         return jsonString + “}”; 
     } 
     
     /// <summary>  
     /// 过滤特殊字符  
     /// </summary>  
    爬虫学习笔记,和指标之间的转移。 /// <param name=”s”></param>  
     /// <returns></returns>  
     private static string String2Json(String s) 
     { 
         StringBuilder sb = new StringBuilder(); 
         for (int i = 0; i < s.Length; i++) 
         { 
             char c = s.ToCharArray()[i]; 
             switch (c) 
             { 
                 case ‘\”‘: 
                     sb.Append(“\\\””); break; 
                 case ‘\\’: 
                     sb.Append(“\\\\”); break; 
                 case ‘/’: 
                     sb.Append(“\\/”); break; 
                 case ‘\b’: 
                     sb.Append(“\\b”); break; 
                 case ‘\f’: 
                     sb.Append(“\\f”); break; 
                 case ‘\n’: 
                     sb.Append(“\\n”); break; 
                 case ‘\r’: 
                     sb.Append(“\\r”); break; 
                 case ‘\t’: 
                     sb.Append(“\\t”); break; 
                 default: 
                     sb.Append(c); break; 
             } 
         } 
         return sb.ToString(); 
     } 
     
     /// <summary>  
     /// 格式化字符型、日期型、布尔型  
     /// </summary>  
     /// <param name=”str”></param>  
     /// <param name=”type”></param>  
     /// <returns></returns>  
     private static string StringFormat(string str, Type type) 
     { 
         if (type != typeof(string) && string.IsNullOrEmpty(str)) 
         { 
             str = “\”” + str + “\””; 
         } 
         else if (type == typeof(string)) 
         { 
             str = String2Json(str); 
             str = “\”” + str + “\””; 
         } 
         else if (type == typeof(DateTime)) 
         { 
             str = “\”” + str.Split(‘ ‘)[0] + “\””; 
         } 
         else if (type == typeof(bool)) 
         { 
             str = str.ToLower(); 
         } 
     
         return str; 
     } 

KoleosSA算法代码:

 

[csharp] view
plain copy

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.IO;  
  5. using System.Security.Cryptography;  
  6.   
  7. namespace RSA.Class  
  8. {  
  9.     /// <summary>  
  10.     /// 类名:RSAFromPkcs8  
  11.     /// 功能:RSA加密、解密、签名、验签  
  12.     /// 详细:该类对Java生成的密钥举办解密和签署以及验签专用类,不须要修改  
  13.     /// 版本:3.0  
  14.     /// 日期:2013-07-08  
  15.     /// 说明:  
  16.     /// 以下代码只是为着便利商家测试而提供的样例代码,商家能够依照本身网址的急需,遵照技术文书档案编写,并非必然要接纳该代码。  
  17.     /// </summary>  
  18.     public sealed class RSAFromPkcs8  
  19.     {  
  20.         /// <summary>  
  21.         /// 签名  
  22.         /// </summary>  
  23.         /// <param name=”content”>待签名字符串</param>  
  24.         /// <param name=”privateKey”>私钥</param>  
  25.         /// <param name=”input_charset”>编码格式</param>  
  26.         /// <returns>签名后字符串</returns>  
  27.         public static string sign(string content, string privateKey, string input_charset)  
  28.         {  
  29.             byte[] Data = Encoding.GetEncoding(input_charset).GetBytes(content);  
  30.             RSACryptoServiceProvider rsa = DecodePemPrivateKey(privateKey);  
  31.             SHA1 sh = new SHA1CryptoServiceProvider();  
  32.             byte[] signData = rsa.SignData(Data, sh);  
  33.             return Convert.ToBase64String(signData);  
  34.         }  
  35.   
  36.         /// <summary>  
  37.         /// 验签  
  38.         /// </summary>  
  39.         /// <param name=”content”>待验签字符串</param>  
  40.         /// <param name=”signedString”>签名</param>  
  41.         /// <param name=”publicKey”>公钥</param>  
  42.         /// <param name=”input_charset”>编码格式</param>  
  43.         /// <returns>true(通过),false(不通过)</returns>  
  44.         public static bool verify(string content, string signedString, string publicKey, string input_charset)  
  45.         {  
  46.             bool result = false;  
  47.             byte[] Data = Encoding.GetEncoding(input_charset).GetBytes(content);  
  48.             byte[] data = Convert.FromBase64String(signedString);  
  49.             RSAParameters paraPub = ConvertFromPublicKey(publicKey);  
  50.             RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();  
  51.             rsaPub.ImportParameters(paraPub);  
  52.             SHA1 sh = new SHA1CryptoServiceProvider();  
  53.             result = rsaPub.VerifyData(Data, sh, data);  
  54.             return result;  
  55.         }  
  56.   
  57.         /// <summary>  
  58.         /// 加密  
  59.         /// </summary>  
  60.         /// <param name=”resData”>须求加密的字符串</param>  
  61.         /// <param name=”publicKey”>公钥</param>  
  62.         /// <param name=”input_charset”>编码格式</param>  
  63.         /// <returns>明文</returns>  
  64.         public static string encryptData(string resData, string publicKey, string input_charset)  
  65.         {  
  66.             byte[] DataToEncrypt = Encoding.ASCII.GetBytes(resData);  
  67.             string result = encrypt(DataToEncrypt, publicKey, input_charset);  
  68.             return result;  
  69.         }  
  70.   
  71.   
  72.         /// <summary>  
  73.         /// 解密  
  74.         /// </summary>  
  75.         /// <param name=”resData”>加密字符串</param>  
  76.         /// <param name=”privateKey”>私钥</param>  
  77.         /// <param name=”input_charset”>编码格式</param>  
  78.         /// <returns>明文</returns>  
  79.         public static string decryptData(string resData, string privateKey, string input_charset)  
  80.         {  
  81.             byte[] DataToDecrypt = Convert.FromBase64String(resData);  
  82.             string result = “”;  
  83.             for (int j = 0; j < DataToDecrypt.Length / 128; j++)  
  84.             {  
  85.                 byte[] buf = new byte[128];  
  86.                 for (int i = 0; i < 128; i++)  
  87.                 {  
  88.   
  89.                     buf[i] = DataToDecrypt[i + 128 * j];  
  90.                 }  
  91.                 result += decrypt(buf, privateKey, input_charset);  
  92.             }  
  93.             return result;  
  94.         }  
  95.  
  96.         #region 内部方法  
  97.   
  98.         private static string encrypt(byte[] data, string publicKey, string input_charset)  
  99.         {  
  100.             RSACryptoServiceProvider rsa = DecodePemPublicKey(publicKey);  
  101.             SHA1 sh = new SHA1CryptoServiceProvider();  
  102.             byte[] result = rsa.Encrypt(data, false);  
  103.               
  104.             return Convert.ToBase64String(result);  
  105.         }  
  106.   
  107.         private static string decrypt(byte[] data, string privateKey, string input_charset)  
  108.         {  
  109.             string result = “”;  
  110.             RSACryptoServiceProvider rsa = DecodePemPrivateKey(privateKey);  
  111.             SHA1 sh = new SHA1CryptoServiceProvider();  
  112.             byte[] source = rsa.Decrypt(data, false);  
  113.             char[] asciiChars = new char[Encoding.GetEncoding(input_charset).GetCharCount(source, 0, source.Length)];  
  114.             Encoding.GetEncoding(input_charset).GetChars(source, 0, source.Length, asciiChars, 0);  
  115.             result = new string(asciiChars);  
  116.             //result = ASCIIEncoding.ASCII.GetString(source);  
  117.             return result;  
  118.         }  
  119.   
  120.         private static RSACryptoServiceProvider DecodePemPublicKey(String pemstr)  
  121.         {  
  122.             byte[] pkcs8publickkey;  
  123.             pkcs8publickkey = Convert.FromBase64String(pemstr);  
  124.             if (pkcs8publickkey != null)  
  125.             {  
  126.                 RSACryptoServiceProvider rsa = DecodeRSAPublicKey(pkcs8publickkey);  
  127.                 return rsa;  
  128.             }  
  129.             else  
  130.                 return null;  
  131.         }  
  132.           
  133.         private static RSACryptoServiceProvider DecodePemPrivateKey(String pemstr)  
  134.         {  
  135.             byte[] pkcs8privatekey;  
  136.             pkcs8privatekey = Convert.FromBase64String(pemstr);  
  137.             if (pkcs8privatekey != null)  
  138.             {  
  139.                 RSACryptoServiceProvider rsa = DecodePrivateKeyInfo(pkcs8privatekey);  
  140.                 return rsa;  
  141.             }  
  142.             else  
  143.                 return null;  
  144.         }  
  145.   
  146.         private static RSACryptoServiceProvider DecodePrivateKeyInfo(byte[] pkcs8)  
  147.         {  
  148.             byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };  
  149.             byte[] seq = new byte[15];  
  150.   
  151.             MemoryStream mem = new MemoryStream(pkcs8);  
  152.             int lenstream = (int)mem.Length;  
  153.             BinaryReader binr = new BinaryReader(mem);    //wrap Memory Stream with BinaryReader for easy reading  
  154.             byte bt = 0;  
  155.             ushort twobytes = 0;  
  156.   
  157.             try  
  158.             {  
  159.                 twobytes = binr.ReadUInt16();  
  160.                 if (twobytes == 0x8130)    //data read as little endian order (actual data order for Sequence is 30 81)  
  161.                     binr.ReadByte();    //advance 1 byte  
  162.                 else if (twobytes == 0x8230)  
  163.                     binr.ReadInt16();    //advance 2 bytes  
  164.                 else  
  165.                     return null;  
  166.   
  167.                 bt = binr.ReadByte();  
  168.                 if (bt != 0x02)  
  169.                     return null;  
  170.   
  171.                 twobytes = binr.ReadUInt16();  
  172.   
  173.                 if (twobytes != 0x0001)  
  174.                     return null;  
  175.   
  176.                 seq = binr.ReadBytes(15);        //read the Sequence OID  
  177.                 if (!CompareBytearrays(seq, SeqOID))    //make sure Sequence for OID is correct  
  178.                     return null;  
  179.   
  180.                 bt = binr.ReadByte();  
  181.                 if (bt != 0x04)    //expect an Octet string  
  182.                     return null;  
  183.   
  184.                 bt = binr.ReadByte();        //read next byte, or next 2 bytes is  0x81 or 0x82; otherwise bt is the byte count  
  185.                 if (bt == 0x81)  
  186.                     binr.ReadByte();  
  187.                 else  
  188.                     if (bt == 0x82)  
  189.                         binr.ReadUInt16();  
  190.                 //—— at this stage, the remaining sequence should be the RSA private key  
  191.   
  192.                 byte[] rsaprivkey = binr.ReadBytes((int)(lenstream – mem.Position));  
  193.                 RSACryptoServiceProvider rsacsp = DecodeRSAPrivateKey(rsaprivkey);  
  194.                 return rsacsp;  
  195.             }  
  196. 4858.com ,  
  197.             catch (Exception)  
  198.             {  
  199.                 return null;  
  200.             }  
  201.   
  202.             finally { binr.Close(); }  
  203.   
  204.         }  
  205.   
  206.         private static bool CompareBytearrays(byte[] a, byte[] b)  
  207.         {  
  208.             if (a.Length != b.Length)  
  209.                 return false;  
  210.             int i = 0;  
  211.             foreach (byte c in a)  
  212.             {  
  213.                 if (c != b[i])  
  214.                     return false;  
  215.                 i++;  
  216.             }  
  217.             return true;  
  218.         }  
  219.   
  220.         private static RSACryptoServiceProvider DecodeRSAPublicKey(byte[] publickey)  
  221.         {  
  222.             // encoded OID sequence for  PKCS #1 rsaEncryption szOID_RSA_RSA = “1.2.840.113549.1.1.1”  
  223.             byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };  
  224.             byte[] seq = new byte[15];  
  225.             // ———  Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob  ——  
  226.             MemoryStream mem = new MemoryStream(publickey);  
  227.             BinaryReader binr = new BinaryReader(mem);    //wrap Memory Stream with BinaryReader for easy reading  
  228.             byte bt = 0;  
  229.             ushort twobytes = 0;  
  230.   
  231.             try  
  232.             {  
  233.   
  234.                 twobytes = binr.ReadUInt16();  
  235.                 if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)  
  236.                     binr.ReadByte();    //advance 1 byte  
  237.                 else if (twobytes == 0x8230)  
  238.                     binr.ReadInt16();   //advance 2 bytes  
  239.                 else  
  240.                     return null;  
  241.   
  242.                 seq = binr.ReadBytes(15);       //read the Sequence OID  
  243.                 if (!CompareBytearrays(seq, SeqOID))    //make sure Sequence for OID is correct  
  244.                     return null;  
  245.   
  246.                 twobytes = binr.ReadUInt16();  
  247.                 if (twobytes == 0x8103) //data read as little endian order (actual data order for Bit String is 03 81)  
  248.                     binr.ReadByte();    //advance 1 byte  
  249.                 else if (twobytes == 0x8203)  
  250.                     binr.ReadInt16();   //advance 2 bytes  
  251.                 else  
  252.                     return null;  
  253.   
  254.                 bt = binr.ReadByte();  
  255.                 if (bt != 0x00)     //expect null byte next  
  256.                     return null;  
  257.   
  258.                 twobytes = binr.ReadUInt16();  
  259.                 if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)  
  260.                     binr.ReadByte();    //advance 1 byte  
  261.                 else if (twobytes == 0x8230)  
  262.                     binr.ReadInt16();   //advance 2 bytes  
  263.                 else  
  264.                     return null;  
  265.   
  266.                 twobytes = binr.ReadUInt16();  
  267.                 byte lowbyte = 0x00;  
  268.                 byte highbyte = 0x00;  
  269.   
  270.                 if (twobytes == 0x8102) //data read as little endian order (actual data order for Integer is 02 81)  
  271.                     lowbyte = binr.ReadByte();  // read next bytes which is bytes in modulus  
  272.                 else if (twobytes == 0x8202)  
  273.                 {  
  274.                     highbyte = binr.ReadByte(); //advance 2 bytes  
  275.                     lowbyte = binr.ReadByte();  
  276.                 }  
  277.                 else  
  278.                     return null;  
  279.                 byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };   //reverse byte order since asn.1 key uses big endian order  
  280.                 int modsize = BitConverter.ToInt32(modint, 0);  
  281.   
  282.                 byte firstbyte = binr.ReadByte();  
  283.                 binr.BaseStream.Seek(-1, SeekOrigin.Current);  
  284.   
  285.                 if (firstbyte == 0x00)  
  286.                 {   //if first byte (highest order) of modulus is zero, don’t include it  
  287.                     binr.ReadByte();    //skip this null byte  
  288.                     modsize -= 1;   //reduce modulus buffer size by 1  
  289.                 }  
  290.   
  291.                 byte[] modulus = binr.ReadBytes(modsize);   //read the modulus bytes  
  292.   
  293.                 if (binr.ReadByte() != 0x02)            //expect an Integer for the exponent data  
  294.                     return null;  
  295.                 int expbytes = (int)binr.ReadByte();        // should only need one byte for actual exponent data (for all useful values)  
  296.                 byte[] exponent = binr.ReadBytes(expbytes);  
  297.   
  298.                 // ——- create RSACryptoServiceProvider instance and initialize with public key —–  
  299.                 RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();  
  300.                 RSAParameters RSAKeyInfo = new RSAParameters();  
  301.                 RSAKeyInfo.Modulus = modulus;  
  302.                 RSAKeyInfo.Exponent = exponent;  
  303.                 RSA.ImportParameters(RSAKeyInfo);  
  304.                 return RSA;  
  305.             }  
  306.             catch (Exception)  
  307.             {  
  308.                 return null;  
  309.             }  
  310.   
  311.             finally { binr.Close(); }  
  312.   
  313.         }  
  314.   
  315.         private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)  
  316.         {  
  317.             byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;  
  318.   
  319.             // ———  Set up stream to decode the asn.1 encoded RSA private key  ——  
  320.             MemoryStream mem = new MemoryStream(privkey);  
  321.             BinaryReader binr = new BinaryReader(mem);    //wrap Memory Stream with BinaryReader for easy reading  
  322.             byte bt = 0;  
  323.             ushort twobytes = 0;  
  324.             int elems = 0;  
  325.             try  
  326.             {  
  327.                 twobytes = binr.ReadUInt16();  
  328.                 if (twobytes == 0x8130)    //data read as little endian order (actual data order for Sequence is 30 81)  
  329.                     binr.ReadByte();    //advance 1 byte  
  330.                 else if (twobytes == 0x8230)  
  331.                     binr.ReadInt16();    //advance 2 bytes  
  332.                 else  
  333.                     return null;  
  334.   
  335.                 twobytes = binr.ReadUInt16();  
  336.                 if (twobytes != 0x0102)    //version number  
  337.                     return null;  
  338.                 bt = binr.ReadByte();  
  339.                 if (bt != 0x00)  
  340.                     return null;  
  341.   
  342.   
  343.                 //——  all private key components are Integer sequences —-  
  344.                 elems = GetIntegerSize(binr);  
  345.                 MODULUS = binr.ReadBytes(elems);  
  346.   
  347.                 elems = GetIntegerSize(binr);  
  348.                 E = binr.ReadBytes(elems);  
  349.   
  350.                 elems = GetIntegerSize(binr);  
  351.                 D = binr.ReadBytes(elems);  
  352.   
  353.                 elems = GetIntegerSize(binr);  
  354.                 P = binr.ReadBytes(elems);  
  355.   
  356.                 elems = GetIntegerSize(binr);  
  357.                 Q = binr.ReadBytes(elems);  
  358.   
  359.                 elems = GetIntegerSize(binr);  
  360.                 DP = binr.ReadBytes(elems);  
  361.   
  362.                 elems = GetIntegerSize(binr);  
  363.                 DQ = binr.ReadBytes(elems);  
  364.   
  365.                 elems = GetIntegerSize(binr);  
  366.                 IQ = binr.ReadBytes(elems);  
  367.   
  368.                 // ——- create RSACryptoServiceProvider instance and initialize with public key —–  
  369.                 RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();  
  370.                 RSAParameters RSAparams = new RSAParameters();  
  371.                 RSAparams.Modulus = MODULUS;  
  372.                 RSAparams.Exponent = E;  
  373.                 RSAparams.D = D;  
  374.                 RSAparams.P = P;  
  375.                 RSAparams.Q = Q;  
  376.                 RSAparams.DP = DP;  
  377.                 RSAparams.DQ = DQ;  
  378.                 RSAparams.InverseQ = IQ;  
  379.                 RSA.ImportParameters(RSAparams);  
  380.                 return RSA;  
  381.             }  
  382.             catch (Exception)  
  383.             {  
  384.                 return null;  
  385.             }  
  386.             finally { binr.Close(); }  
  387.         }  
  388.   
  389.         private static int GetIntegerSize(BinaryReader binr)  
  390.         {  
  391.             byte bt = 0;  
  392.             byte lowbyte = 0x00;  
  393.             byte highbyte = 0x00;  
  394.             int count = 0;  
  395.             bt = binr.ReadByte();  
  396.             if (bt != 0x02)        //expect integer  
  397.                 return 0;  
  398.             bt = binr.ReadByte();  
  399.   
  400.             if (bt == 0x81)  
  401.                 count = binr.ReadByte();    // data size in next byte  
  402.             else  
  403.                 if (bt == 0x82)  
  404.                 {  
  405.                     highbyte = binr.ReadByte();    // data size in next 2 bytes  
  406.                     lowbyte = binr.ReadByte();  
  407.                     byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };  
  408.                     count = BitConverter.ToInt32(modint, 0);  
  409.                 }  
  410.                 else  
  411.                 {  
  412.                     count = bt;        // we already have the data size  
  413.                 }  
  414.   
  415.   
  416.   
  417.             while (binr.ReadByte() == 0x00)  
  418.             {    //remove high order zeros in data  
  419.                 count -= 1;  
  420.             }  
  421.             binr.BaseStream.Seek(-1, SeekOrigin.Current);        //last ReadByte wasn’t a removed zero, so back up a byte  
  422.             return count;  
  423.         }  
  424.  
  425.         #endregion  
  426.  
  427.         #region 解析.net 生成的Pem  
  428.         private static RSAParameters ConvertFromPublicKey(string pemFileConent)  
  429.         {  
  430.   
  431.             byte[] keyData = Convert.FromBase64String(pemFileConent);  
  432.             if (keyData.Length < 162)  
  433.             {  
  434.                 throw new ArgumentException(“pem file content is incorrect.”);  
  435.             }  
  436.             byte[] pemModulus = new byte[128];  
  437.             byte[] pemPublicExponent = new byte[3];  
  438.             Array.Copy(keyData, 29, pemModulus, 0, 128);  
  439.             Array.Copy(keyData, 159, pemPublicExponent, 0, 3);  
  440.             RSAParameters para = new RSAParameters();  
  441.             para.Modulus = pemModulus;  
  442.             para.Exponent = pemPublicExponent;  
  443.             return para;  
  444.         }  
  445.   
  446.         private static RSAParameters ConvertFromPrivateKey(string pemFileConent)  
  447.         {  
  448.             byte[] keyData = Convert.FromBase64String(pemFileConent);  
  449.             if (keyData.Length < 609)  
  450.             {  
  451.                 throw new ArgumentException(“pem file content is incorrect.”);  
  452.             }  
  453.   
  454.             int index = 11;  
  455.             byte[] pemModulus = new byte[128];  
  456.             Array.Copy(keyData, index, pemModulus, 0, 128);  
  457.   
  458.             index += 128;  
  459.             index += 2;//141  
  460.             byte[] pemPublicExponent = new byte[3];  
  461.             Array.Copy(keyData, index, pemPublicExponent, 0, 3);  
  462.   
  463.             index += 3;  
  464.             index += 4;//148  
  465.             byte[] pemPrivateExponent = new byte[128];  
  466.             Array.Copy(keyData, index, pemPrivateExponent, 0, 128);  
  467.   
  468.             index += 128;  
  469.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//279  
  470.             byte[] pemPrime1 = new byte[64];  
  471.             Array.Copy(keyData, index, pemPrime1, 0, 64);  
  472.   
  473.             index += 64;  
  474.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//346  
  475.             byte[] pemPrime2 = new byte[64];  
  476.             Array.Copy(keyData, index, pemPrime2, 0, 64);  
  477.   
  478.             index += 64;  
  479.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//412/413  
  480.             byte[] pemExponent1 = new byte[64];  
  481.             Array.Copy(keyData, index, pemExponent1, 0, 64);  
  482.   
  483.             index += 64;  
  484.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//479/480  
  485.             byte[] pemExponent2 = new byte[64];  
  486.             Array.Copy(keyData, index, pemExponent2, 0, 64);  
  487.   
  488.             index += 64;  
  489.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//545/546  
  490.             byte[] pemCoefficient = new byte[64];  
  491.             Array.Copy(keyData, index, pemCoefficient, 0, 64);  
  492.   
  493.             RSAParameters para = new RSAParameters();  
  494.             para.Modulus = pemModulus;  
  495.             para.Exponent = pemPublicExponent;  
  496.             para.D = pemPrivateExponent;  
  497.             para.P = pemPrime1;  
  498.             para.Q = pemPrime2;  
  499.             para.DP = pemExponent1;  
  500.             para.DQ = pemExponent2;  
  501.             para.InverseQ = pemCoefficient;  
  502.             return para;  
  503.         }  
  504.         #endregion  
  505.   
  506.     }  
  507. }  

 

 

   /// <summary>
    /// List转成json
    /// </summary>
    /// <typeparam name=”T”></typeparam>
    /// <param name=”jsonName”></param>
    /// <param name=”list”></param>
    /// <returns></returns>
    public static string ListToJson<T>(IList<T> list, string
jsonName)
    {
        StringBuilder Json = new StringBuilder();
        if (string.IsNullOrEmpty(jsonName))
            jsonName = list[0].GetType().Name;
        Json.Append(“{\”” + jsonName + “\”:[“);
        if (list.Count > 0)
        {
            for (int i = 0; i < list.Count; i++)
            {
                T obj = Activator.CreateInstance<T>();
                PropertyInfo[] pi = obj.GetType().GetProperties();
                Json.Append(“{“);
                for (int j = 0; j < pi.Length; j++)
                {
                    Type type = pi[j].GetValue(list[i],
null).GetType();
                    Json.Append(“\”” + pi[j].Name.ToString() + “\”:”

LANDSA算法测试代码:

 

[csharp] view
plain copy

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using RSA.Class;  
  5.   
  6. namespace payapi_demo.RSA  
  7. {  
  8.     class TestRSA  
  9.     {  
  10.         static void Main(string[] arg)  
  11.         {  
  12.   
  13.             /**PRADOSA加密测试,牧马人SA中的密钥对由此SSL工具生成,生成命令如下: 
  14.              * 1 生成RSA私钥: 
  15.              * openssl genrsa -out rsa_private_key.pem 1024 
  16.              *2 生成RSA公钥 
  17.              * openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem 
  18.              * 
  19.              * 3 将QashqaiSA私钥转换来PKCS8格式 
  20.              * openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out rsa_pub_pk8.pem 
  21.              * 
  22.              * 直接打开rsa_private_key.pem和rsa_pub_pk八.pem文件就足以博得密钥对情节,获取密钥对情节结合字符串时,注意将换行符删除 
  23.              * */  
  24.   
  25.             string publickey = “MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDzOqfNunFxFtCZPlq7fO/jWwjqmTvAooVBB4y87BizSZ9dl/F7FpAxYc6MmX2TqivCvvORXgdlYdFWAhzXOnIUv9OGG///WPLe9TMs9kIwAZ/APUXauvC01oFLnYkzwPlAh0tQ1Au9arTE/OG1V1dKgf8BXHLPhKL4BmGBEUZBtQIDAQAB”;  
  26.             string privatekey = “MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBAPM6p826cXEW0Jk+Wrt87+NbCOqZO8CihUEHjLzsGLNJn12X8XsWkDFhzoyZfZOqK8K+85FeB2Vh0VYCHNc6chS/04Yb//9Y8t71Myz2QjABn8A9Rdq68LTWgUudiTPA+UCHS1DUC71qtMT84bVXV0qB/wFccs+EovgGYYERRkG1AgMBAAECgYEA2PmnPdgnYKnolfvQ9tXiLaBFGPpvGk4grz0r6FB5TF7N4rErwxECunq0xioaowK4HPc40qHd2SvkkWQ7FCjYIDsnMk1oOhxNKn0J3FG0n5Cg1/dFai4eoXHs/nKn3SVZ8YZC1T2cMtN2srectLqNqhB8aQEe8xmykyUlUpg/qmECQQD9vkwjUotG5oUUrOj6etcB4WcdyyH0FtThKgyoJUDwgBv6lGGzWyFJEREvp47IgV+FgC7zeP2mL4MhgnD3tNCZAkEA9WRrjOLBNc379XZpoDsH7rZjobVvhnTrEuRDx/whqZ+vk64EPrEW81XYh647bAbJlFn2jPhY+IUHkrxFEFT/fQJBAMoLNOULXQtfkqgb5odMONeue0Ul8itB4tBHgzyALW1TFPQ6InGGJsLfbCfd67uMCFts7fXAaXhibK/KBdm3iEECQQChwVAjzlUN4nnzk9qMhFz2PcPvFGovd2J9UXpcmRaXeWuDLXIe4Rz/ydaxmWgSDWdTIvoicpIzP31+fBwKZ/0BAkEAy0bh4weKmYF29//rK0sxmY8RtqkQeFrwWbqx1daa1w0DfWlNSvy47zyW1G5/AdZU6JSpXxlxdlM/HSDw+v7kcA==”;  
  27.   
  28.             //加密字符串  
  29.             string data = “yibao”;  
  30.   
  31.             Console.WriteLine(“加密前字符串内容:”+data);  
  32.             //加密  
  33.             string encrypteddata = RSAFromPkcs8.encryptData(data, publickey, “UTF-8”);  
  34.             Console.WriteLine(“加密后的字符串为:” + encrypteddata);  
  35.             Console.WriteLine(“解密后的字符串内容:” + 翼虎SAFromPkcs8.decryptData(encrypteddata, privatekey, “UTF-八”));  
  36.   
  37.             Console.WriteLine(“***********”);  
  38.   
  39.             //解密  
  40.             string endata = “LpnnvnfA72VnyjboX/OsCPO6FOFXeEnnsKkI7aAEQyVAPfCTfQ43ZYVZVqnADDPMW7VhBXJWyQMAGw2Fh9sS/XLHmO5XW94Yehci6JrJMynePgtIiDysjNA+UlgSTC/MlResNrBm/4MMSPvq0qLwScgpZDynhLsVZk+EQ6G8wgA=”;  
  41.             string datamw = RSAFromPkcs8.decryptData(endata, privatekey, “UTF-8”);  
  42.             Console.WriteLine(“静态加密后的字符串为:” + endata);  
  43.             Console.WriteLine(“解密后的字符串内容:” + datamw);  
  44.   
  45.             //签名  
  46.             string signdata = “YB010000001441234567286038508081299”;  
  47.             Console.WriteLine(“签名前的字符串内容:” + signdata);  
  48.             string sign = RSAFromPkcs8.sign(signdata, privatekey, “UTF-8”);  
  49.             Console.WriteLine(“签名后的字符串:” + sign);  
  50.   
  51.             Console.ReadLine();  
  52.         }  
  53.     }  
  54. }  
  • StringFormat(pi[j].GetValue(list[i], null).ToString(), type));
     
                        if (j < pi.Length – 1)
                        {
                            Json.Append(“,”);
                        }
                    }
                    Json.Append(“}”);
                    if (i < list.Count – 1)
                    {
                        Json.Append(“,”);
                    }
                }
            }
            Json.Append(“]}”);
            return Json.ToString();
        }
     
        /// <summary>
        /// List转成json
        /// </summary>
        /// <typeparam name=”T”></typeparam>
        /// <param name=”list”></param>
        /// <returns></returns>
        public static string ListToJson<T>(IList<T> list)
        {
            object obj = list[0];
            return ListToJson<T>(list, obj.GetType().Name);
        }
     
        /// <summary>
        /// 对象转换为Json字符串
        /// </summary>
        /// <param name=”jsonObject”>对象</param>
        /// <returns>Json字符串</returns>
        public static string ToJson(object jsonObject)
        {
            try
            {
                StringBuilder jsonString = new StringBuilder();
                jsonString.Append(“{“);
                PropertyInfo[] propertyInfo =
    jsonObject.GetType().GetProperties();
                for (int i = 0; i < propertyInfo.Length; i++)
                {
                    object objectValue =
    propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
                    if (objectValue == null)
                    {
                        continue;
                    }
                    StringBuilder value = new StringBuilder();
                    if (objectValue is DateTime || objectValue is Guid ||
    objectValue is TimeSpan)
                    {
                        value.Append(“\”” + objectValue.ToString() +
    “\””);
                    }
                    else if (objectValue is string)
                    {
                        value.Append(“\”” + objectValue.ToString() +
    “\””);
                    }
                    else if (objectValue is IEnumerable)
                    {
                        value.Append(ToJson((IEnumerable)objectValue));
                    }
                    else
                    {
                        value.Append(“\”” + objectValue.ToString() +
    “\””);
                    }
                    jsonString.Append(“\”” + propertyInfo[i].Name +
    “\”:” + value + “,”); ;
                }
                return jsonString.ToString().TrimEnd(‘,’) + “}”;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
     
        /// <summary>
        /// 对象集合转换Json
        /// </summary>
        /// <param name=”array”>集合对象</param>
        /// <returns>Json字符串</returns>
        public static string ToJson(IEnumerable array)
        {
            string jsonString = “[“;
            foreach (object item in array)
            {
                jsonString += ToJson(item) + “,”;
            }
            jsonString.Remove(jsonString.Length – 1, jsonString.Length);
            return jsonString + “]”;
        }
     
        /// <summary>
        /// 普通集合转换Json
        /// </summary>
        /// <param name=”array”>集合对象</param>
        /// <returns>Json字符串</returns>
        public static string ToArrayString(IEnumerable array)
        {
            string jsonString = “[“;
            foreach (object item in array)
            {
                jsonString = ToJson(item.ToString()) + “,”;
            }
            jsonString.Remove(jsonString.Length – 1, jsonString.Length);
            return jsonString + “]”;
        }
     
        /// <summary>
        /// Datatable转换为Json
        /// </summary>
        /// <param name=”table”>Datatable对象</param>
        /// <returns>Json字符串</returns>
        public static string ToJson(DataTable dt)
        {
            StringBuilder jsonString = new StringBuilder();
            jsonString.Append(“[“);
            DataRowCollection drc = dt.Rows;
            for (int i = 0; i < drc.Count; i++)
            {
                jsonString.Append(“{“);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    string strKey = dt.Columns[j].ColumnName;
                    string strValue = drc[i][j].ToString();
                    Type type = dt.Columns[j].DataType;
                    jsonString.Append(“\”” + strKey + “\”:”);
                    strValue = StringFormat(strValue, type);
                    if (j < dt.Columns.Count – 1)
                    {
                        jsonString.Append(strValue + “,”);
                    }
                    else
                    {
                        jsonString.Append(strValue);
                    }
                }
                jsonString.Append(“},”);
            }
            jsonString.Remove(jsonString.Length – 1, 1);
            jsonString.Append(“]”);
            return jsonString.ToString();
        }
     
        /// <summary>
        /// DataTable转成Json
        /// </summary>
        /// <param name=”jsonName”></param>
        /// <param name=”dt”></param>
        /// <returns></returns>
        public static string ToJson(DataTable dt, string jsonName)
        {
            StringBuilder Json = new StringBuilder();
            if (string.IsNullOrEmpty(jsonName))
                jsonName = dt.TableName;
            Json.Append(“{\”” + jsonName + “\”:[“);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Json.Append(“{“);
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        Type type = dt.Rows[i][j].GetType();
                        Json.Append(“\”” +
    dt.Columns[j].ColumnName.ToString() + “\”:” +
    StringFormat(dt.Rows[i][j].ToString(), type));
                        if (j < dt.Columns.Count – 1)
                        {
                            Json.Append(“,”);
                        }
                    }
                    Json.Append(“}”);
                    if (i < dt.Rows.Count – 1)
                    {
                        Json.Append(“,”);
                    }
                }
            }
            Json.Append(“]}”);
            return Json.ToString();
        }
     
        /// <summary>
        /// DataReader转换为Json
        /// </summary>
        /// <param name=”dataReader”>DataReader对象</param>
        /// <returns>Json字符串</returns>
        public static string ToJson(IDataReader dataReader)
        {
            StringBuilder jsonString = new StringBuilder();
            jsonString.Append(“[“);
     
            while (dataReader.Read())
            {
                jsonString.Append(“{“);
                for (int i = 0; i < dataReader.FieldCount; i++)
                {
                    Type type = dataReader.GetFieldType(i);
                    string strKey = dataReader.GetName(i);
                    string strValue = dataReader[i].ToString();
                    jsonString.Append(“\”” + strKey + “\”:”);
                    strValue = StringFormat(strValue, type);
                    if (i < dataReader.FieldCount – 1)
                    {
                        jsonString.Append(strValue + “,”);
                    }
                    else
                    {
                        jsonString.Append(strValue);
                    }
                }
                jsonString.Append(“},”);
            }
            dataReader.Close();
            jsonString.Remove(jsonString.Length – 1, 1);
            jsonString.Append(“]”);
            if (jsonString.Length == 1)
            {
                return “[]”;
            }
            return jsonString.ToString();
        }
     
        /// <summary>
        /// DataSet转换为Json
        /// </summary>
        /// <param name=”dataSet”>DataSet对象</param>
        /// <returns>Json字符串</returns>
        public static string ToJson(DataSet dataSet)
        {
            string jsonString = “{“;
            foreach (DataTable table in dataSet.Tables)
            {
                jsonString += “\”” + table.TableName + “\”:” +
    ToJson(table) + “,”;
            }
            jsonString = jsonString.TrimEnd(‘,’);
            return jsonString + “}”;
        }
     
        /// <summary>
        /// 过滤特殊字符
        /// </summary>
        /// <param name=”s”></param>
        /// <returns></returns>
        private static string String2Json(String s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s.ToCharArray()[i];
                switch (c)
                {
                    case ‘\”‘:
                        sb.Append(“\\\””); break;
                    case ‘\\’:
                        sb.Append(“\\\\”); break;
                    case ‘/’:
                        sb.Append(“\\/”); break;
                    case ‘\b’:
                        sb.Append(“\\b”); break;
                    case ‘\f’:
                        sb.Append(“\\f”); break;
                    case ‘\n’:
                        sb.Append(“\\n”); break;
                    case ‘\r’:
                        sb.Append(“\\r”); break;
                    case ‘\t’:
                        sb.Append(“\\t”); break;
                    default:
                        sb.Append(c); break;
                }
            }
            return sb.ToString();
        }
     
        /// <summary>
        /// 格式化字符型、日期型、布尔型
        /// </summary>
        /// <param name=”str”></param>
        /// <param name=”type”></param>
        /// <returns></returns>
        private static string StringFormat(string str, Type type)
        {
            if (type != typeof(string) && string.IsNullOrEmpty(str))
            {
                str = “\”” + str + “\””;
            }
            else if (type == typeof(string))
            {
                str = String2Json(str);
                str = “\”” + str + “\””;
            }
            else if (type == typeof(DateTime))
            {
                str = “\”” + str.Split(‘ ‘)[0] + “\””;
            }
            else if (type == typeof(bool))
            {
                str = str.ToLower();
            }
     
            return str;
        }

 

summary /// List转成json /// /summary
/// typeparam name=T/typeparam /// param name=jsonName/param /// param
name=list/param /// returns/returns public static string Li…

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有