public static void DoTest() { Console.WriteLine("使用 DSACryptoServiceProvider 类创建哈希值的数字签名,然后验证签名的例子!"); // 创建一个 DSA 算法的加密服务提供程序 (CSP) 实现的包装对象. DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); // 需要被签名的数据. byte[] HashValue = { 59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135 }; Console.WriteLine("初始 Hash 数据."); ByteArrayOutput.Print(HashValue); // 签名处理. byte[] SignedHashValue = DSASignHash(HashValue, DSA.ExportParameters(true), "SHA1"); Console.WriteLine("使用私钥签名的数据."); ByteArrayOutput.Print(SignedHashValue); // 验证签名. if (DSAVerifyHash(HashValue, SignedHashValue, DSA.ExportParameters(false), "SHA1")) { Console.WriteLine("使用公钥验证 签名是有效的!"); } else { Console.WriteLine("使用公钥验证 签名无效."); } }
/// <summary> /// 一个 MD5 后 再 MD5 的 密码处理逻辑. /// </summary> public static void DoTest2() { ASCIIEncoding ByteConverter = new ASCIIEncoding(); string source = "1234567890"; byte[] step1 = ByteConverter.GetBytes(source); byte[] step1MD5 = MD5hash(step1); ByteArrayOutput.Print(step1MD5); string step2Str = ByteArrayOutput.Hexdigest(step1MD5); step2Str = step2Str + "TestString"; Console.WriteLine(step2Str); byte[] step2 = ByteConverter.GetBytes(step2Str); byte[] step2MD5 = MD5hash(step2); ByteArrayOutput.Print(step2MD5); }
public static void DoTest() { Console.WriteLine("MD5 测试!"); UnicodeEncoding ByteConverter = new UnicodeEncoding(); string source = "Data to Encrypt 这个是一个用于测试MD5的文本信息!"; byte[] dataToEncrypt = ByteConverter.GetBytes(source); Console.WriteLine("原始文本信息:{0}", source); Console.WriteLine("原始数据!"); ByteArrayOutput.Print(dataToEncrypt); Console.WriteLine("MD5"); ByteArrayOutput.Print(MD5hash(dataToEncrypt)); Console.WriteLine("SHA256"); ByteArrayOutput.Print(SHA256hash(dataToEncrypt)); Console.WriteLine("SHA384"); ByteArrayOutput.Print(SHA384hash(dataToEncrypt)); Console.WriteLine("SHA512"); ByteArrayOutput.Print(SHA512hash(dataToEncrypt)); }
public static void DoTest() { Console.WriteLine("使用 RSACryptoServiceProvider 类将一个字符串加密为一个字节数组,然后将这些字节解密为字符串。"); try { // Create a UnicodeEncoder to convert between byte array and string. UnicodeEncoding ByteConverter = new UnicodeEncoding(); string source = "Data to Encrypt 这个是一个用于测试加密的文本信息!"; //Create byte arrays to hold original, encrypted, and decrypted data. byte[] dataToEncrypt = ByteConverter.GetBytes(source); byte[] encryptedData; byte[] decryptedData; Console.WriteLine("原始文本信息:{0}", source); Console.WriteLine("原始数据!"); ByteArrayOutput.Print(dataToEncrypt); // 加密服务提供程序 (CSP) 提供的 RSA 算法的实现 RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); // 加密 (公钥加密). encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false); Console.WriteLine("公钥加密后的数据!"); ByteArrayOutput.Print(encryptedData); // 解密 (私钥解密) decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false); Console.WriteLine("私钥解密后的数据!"); ByteArrayOutput.Print(decryptedData); // 输出. Console.WriteLine("解密后的文本: {0}", ByteConverter.GetString(decryptedData)); } catch (ArgumentNullException) { //Catch this exception in case the encryption did //not succeed. Console.WriteLine("Encryption failed."); } }
/// <summary> /// 输出密钥的内部数据信息. /// </summary> /// <param name="RSAKeyInfo"></param> public static void PrintRSAParameters(RSAParameters RSAKeyInfo) { if (RSAKeyInfo.D != null) { Console.WriteLine("-----D-----"); ByteArrayOutput.Print(RSAKeyInfo.D); } if (RSAKeyInfo.DP != null) { Console.WriteLine("-----DP-----"); ByteArrayOutput.Print(RSAKeyInfo.DP); } if (RSAKeyInfo.DQ != null) { Console.WriteLine("-----DQ-----"); ByteArrayOutput.Print(RSAKeyInfo.DQ); } if (RSAKeyInfo.Exponent != null) { Console.WriteLine("-----Exponent-----"); ByteArrayOutput.Print(RSAKeyInfo.Exponent); } if (RSAKeyInfo.Modulus != null) { Console.WriteLine("-----Modulus-----"); ByteArrayOutput.Print(RSAKeyInfo.Modulus); } if (RSAKeyInfo.InverseQ != null) { Console.WriteLine("-----InverseQ-----"); ByteArrayOutput.Print(RSAKeyInfo.InverseQ); } if (RSAKeyInfo.P != null) { Console.WriteLine("-----P-----"); ByteArrayOutput.Print(RSAKeyInfo.P); } if (RSAKeyInfo.Q != null) { Console.WriteLine("-----Q-----"); ByteArrayOutput.Print(RSAKeyInfo.Q); } }
/// <summary> /// 一个 HMAC-SHA1 加密的处理. /// </summary> public static void DoTest3() { string appid = "173a9608f2d411e4936600ffa64984b5"; string expired_time = "1452663274"; string secret_key = "aOahcMWCoX6I"; string rawMask = appid + "_" + expired_time + "_" + secret_key; byte[] data = Encoding.UTF8.GetBytes(rawMask); byte[] key = Encoding.UTF8.GetBytes(secret_key); Console.WriteLine("HMAC-SHA1"); ByteArrayOutput.Print(HMACSHA1hash(data, key)); }
/// <summary> /// 测试 生成对称密钥. /// </summary> public static void DoTest() { Console.WriteLine("##### 生成对称密钥的例子 !"); TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider(); Console.WriteLine("对称算法的密钥:"); ByteArrayOutput.Print(TDES.Key); Console.WriteLine("对称算法的对称算法的初始化向量 (IV):"); ByteArrayOutput.Print(TDES.IV); Console.WriteLine("再多生成一组密钥 !"); TDES.GenerateIV(); TDES.GenerateKey(); Console.WriteLine("对称算法的密钥:"); ByteArrayOutput.Print(TDES.Key); Console.WriteLine("对称算法的对称算法的初始化向量 (IV):"); ByteArrayOutput.Print(TDES.IV); Console.WriteLine("再多生成一组密钥 !"); TDES.GenerateIV(); TDES.GenerateKey(); Console.WriteLine("对称算法的密钥:"); ByteArrayOutput.Print(TDES.Key); Console.WriteLine("对称算法的对称算法的初始化向量 (IV):"); ByteArrayOutput.Print(TDES.IV); }
public void DoTest() { Console.WriteLine("##### 访问 {0} 算法的测试!!!#####", GetServiceName()); try { string original = "Here is some data to encrypt! 这里是一些需要被加密的数据..."; Console.WriteLine("原始数据: {0}", original); // 注意:这个构造函数, 每次会初始化不同的 密钥(Key) 与 初始化向量 (IV) // 实际开发过程中, 密钥(Key) 与 初始化向量 (IV) 需要保存在本地系统中。 // 加密/解密以前, 设置一下 Key 和 IV。 // 不能简单地 加密的时候 new 一个 解密的时候,再 new 一个 SymmetricAlgorithm service = GetService(); // 私钥加密的缺点是它假定双方已就密钥和 IV 达成协议,并且互相传达了密钥和 IV 的值。 // 一般认为,IV 并不安全,且可以在消息的纯文本中传输。 // 但是,密钥必须对未经授权的用户保密。 // 由于存在这些问题,因此通常将私钥加密与公钥加密配合使用,以秘密地传达密钥和 IV 的值。 // 假定 Alice 和 Bob 是希望在非安全信道上通信的双方, // 他们可以按如下方式使用私钥加密:Alice 和 Bob 同意对特定的密钥和 IV 应用一种特定的算法(例如 AES)。 // Alice 撰写一条消息并创建要在其上发送该消息的网络流(可能是一个命名管道或网络电子邮件)。 // 接下来,她使用该密钥和 IV 加密文本,并通过 Intranet 向 Bob 发送该加密消息和 IV。 // Bob 在收到该加密文本后,可使用 IV 和预先商定的密钥对它进行解密。 // 即使传输的内容被人截获,截获者也无法恢复原始消息,因为他并不知道密钥。 // 在此方案中,只有密钥必须保密。 Console.WriteLine("对称算法的密钥:"); ByteArrayOutput.Print(service.Key); Console.WriteLine("对称算法的对称算法的初始化向量 (IV):"); ByteArrayOutput.Print(service.IV); Console.WriteLine("开始做加密处理......"); byte[] encrypted = EncryptStringToBytes(service, original); Console.WriteLine("加密后的内容:"); ByteArrayOutput.Print(encrypted); Console.WriteLine("开始做解密处理......"); string roundtrip = DecryptStringFromBytes(service, encrypted); Console.WriteLine("解密后的数据: {0}", roundtrip); Console.WriteLine(); } catch (Exception e) { Console.WriteLine("Error: {0}", e.Message); } }
/// <summary> /// 测试 生成非对称密钥. /// </summary> public static void DoTest() { // Generate a public/private key pair. RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); // 每当创建不对称算法类的新实例时,都生成一个公钥/私钥对。创建该类的新实例后,可以用以下两种方法之一提取密钥信息: // ToXMLString 方法,它返回密钥信息的 XML 表示形式。 Console.WriteLine("仅仅包含公钥的情况!"); Console.WriteLine(RSA.ToXmlString(false)); Console.WriteLine("同时包含公钥与私钥的情况!"); Console.WriteLine(RSA.ToXmlString(true)); // ExportParameters 方法,它返回保存密钥信息的 RSAParameters 结构。 // 将使用 RSACryptoServiceProvider 创建的密钥信息导出为 RSAParameters 对象。 RSAParameters RSAKeyInfo = RSA.ExportParameters(false); // 取得目标的 公钥的 XML 信息. string xmlString = RSA.ToXmlString(false); // Create a UnicodeEncoder to convert between byte array and string. UnicodeEncoding ByteConverter = new UnicodeEncoding(); string source = "Data to Encrypt 这个是一个用于测试加密的文本信息!"; //Create byte arrays to hold original, encrypted, and decrypted data. byte[] dataToEncrypt = ByteConverter.GetBytes(source); byte[] encryptedData; Console.WriteLine("原始文本信息:{0}", source); Console.WriteLine("原始数据!"); ByteArrayOutput.Print(dataToEncrypt); // 这里创建一个新的 RSACryptoServiceProvider RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider(); // 通过 XML 字符串中的密钥信息初始化 RSA 对象。 RSA2.FromXmlString(xmlString); // 公钥加密. encryptedData = RSAEncrypt(dataToEncrypt, RSA2.ExportParameters(false), false); Console.WriteLine("公钥加密后的数据!"); ByteArrayOutput.Print(encryptedData); byte[] decryptedData; // 解密 decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false); Console.WriteLine("私钥解密后的数据!"); ByteArrayOutput.Print(decryptedData); // 输出. Console.WriteLine("私钥解密后的文本: {0}", ByteConverter.GetString(decryptedData)); }
void OnGUI() { if (GUI.Button(new Rect(50, 50, 200, 30), "使用BigEndian写入文件")) { ByteArrayOutput output = new ByteArrayOutput(); output.endian = Endian.BigEndian; output.WriteBoolean(true); output.WriteByte(-55); output.WriteUnsignedByte(188); output.WriteShort(-12345); output.WriteUnsignedShort(65535); output.WriteInt(-1234567); output.WriteUnsignedInt(1234567); output.WriteLong(-1234567891011); output.WriteUnsignedLong(1234567891011); output.WriteFloat(123.456f); output.WriteDouble(123456789.123456789d); output.WriteUTF("Hello Unity3D! 你好,尤三弟!"); byte[] bytes = output.bytes; FileHelper.SaveAsBytes("C:\\test_big_endian.bytes", bytes); Debug.Log("save file ok!"); } if (GUI.Button(new Rect(50, 100, 200, 30), "读取BigEndian的数据")) { byte[] bytes = FileHelper.ReadAsBytes("C:\\test_big_endian.bytes"); ByteArrayInput input = new ByteArrayInput(bytes); input.endian = Endian.BigEndian; Debug.Log("位置: " + input.position + ", 剩余: " + input.bytesAvailable); Debug.Log(input.ReadBoolean()); Debug.Log(input.ReadByte()); Debug.Log(input.ReadUnsignedByte()); Debug.Log(input.ReadShort()); Debug.Log(input.ReadUnsignedShort()); Debug.Log(input.ReadInt()); Debug.Log("位置: " + input.position + ", 剩余: " + input.bytesAvailable); Debug.Log(input.ReadUnsignedInt()); Debug.Log(input.ReadLong()); Debug.Log(input.ReadUnsignedLong()); Debug.Log(input.ReadFloat()); Debug.Log(input.ReadDouble()); Debug.Log(input.ReadUTF()); Debug.Log("位置: " + input.position + ", 剩余: " + input.bytesAvailable); } if (GUI.Button(new Rect(300, 50, 200, 30), "使用LittleEndian写入文件")) { ByteArrayOutput output = new ByteArrayOutput(); output.endian = Endian.LittleEndian; output.WriteBoolean(true); output.WriteByte(-55); output.WriteUnsignedByte(188); output.WriteShort(-12345); output.WriteUnsignedShort(65535); output.WriteInt(-1234567); output.WriteUnsignedInt(1234567); output.WriteLong(-1234567891011); output.WriteUnsignedLong(1234567891011); output.WriteFloat(123.456f); output.WriteDouble(123456789.123456789d); output.WriteUTF("Hello Unity3D! 你好,尤三弟!"); byte[] bytes = output.bytes; FileHelper.SaveAsBytes("C:\\test_little_endian.bytes", bytes); Debug.Log("save file ok!"); } if (GUI.Button(new Rect(300, 100, 200, 30), "读取LittleEndian的数据")) { byte[] bytes = FileHelper.ReadAsBytes("C:\\test_little_endian.bytes"); ByteArrayInput input = new ByteArrayInput(bytes); input.endian = Endian.LittleEndian; Debug.Log("位置: " + input.position + ", 剩余: " + input.bytesAvailable); Debug.Log(input.ReadBoolean()); Debug.Log(input.ReadByte()); Debug.Log(input.ReadUnsignedByte()); Debug.Log(input.ReadShort()); Debug.Log(input.ReadUnsignedShort()); Debug.Log(input.ReadInt()); Debug.Log("位置: " + input.position + ", 剩余: " + input.bytesAvailable); Debug.Log(input.ReadUnsignedInt()); Debug.Log(input.ReadLong()); Debug.Log(input.ReadUnsignedLong()); Debug.Log(input.ReadFloat()); Debug.Log(input.ReadDouble()); Debug.Log(input.ReadUTF()); Debug.Log("位置: " + input.position + ", 剩余: " + input.bytesAvailable); } }