/** * Con un texto dado, este lo encripta con la * llave generada del TDES que ha recibido anteriormente. **/ public String encryptText(String text, String tdesKey1, String tdesKey2, String tdesKey3) { if (tdesKey1 != null && !tdesKey1.Equals("") && text != null && !text.Equals("")) { byte[] key1bytes = fromHexToByte(tdesKey1); byte[] key2bytes = fromHexToByte(tdesKey2); byte[] key3bytes = fromHexToByte(tdesKey3); var tdesKey = new byte[key1bytes.Length + key2bytes.Length + key3bytes.Length]; key1bytes.CopyTo(tdesKey, 0); key2bytes.CopyTo(tdesKey, key1bytes.Length); key3bytes.CopyTo(tdesKey, key1bytes.Length + key2bytes.Length); TDES.Key = tdesKey; byte[] ivInHex = RSA.Decrypt(fromHexToByte(tdesIVMasterEncrypted), true); TDES.IV = ivInHex; ICryptoTransform encryptor = TDES.CreateEncryptor(TDES.Key, TDES.IV); MemoryStream ms = new MemoryStream(); using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { using (StreamWriter sw = new StreamWriter(cs)) sw.Write(text); byte[] encrypted = ms.ToArray(); return(BitConverter.ToString(encrypted).Replace("-", "")); } } return(null); }
public void SaveToFile(int fileIndex) { string fileName = string.Format(ARCHIVE_FILE_NAME, fileIndex); string sFolderPath = Application.persistentDataPath + "/" + ARCHIVE_FILE_DIR; if (!Directory.Exists(sFolderPath)) { Directory.CreateDirectory(sFolderPath); } string sPath = sFolderPath + "/" + fileName; using (MemoryStream ms = new MemoryStream()) { //写入存档内容 TDES tdesTool = new TDES(); tdesTool.Init(ConStr.DES_KEY); Hashtable hsData = Save(); byte[] strBuffer = System.Text.Encoding.Default.GetBytes(hsData.toJson()); byte[] encryptDatas = tdesTool.Encrypt(strBuffer); ms.Write(BitConverter.GetBytes(encryptDatas.Length), 0, sizeof(int)); ms.Write(encryptDatas, 0, encryptDatas.Length); File.WriteAllBytes(sPath, ms.GetBuffer()); } }
public static GameRuntimeData LoadArchive(int fileIndex) { string sPath = GetArchiveFilePath(fileIndex); GameRuntimeData tagArchive = null; if (File.Exists(sPath)) { using (FileStream fs = File.OpenRead(sPath)) { byte[] buffer1 = new byte[sizeof(int)]; fs.Read(buffer1, 0, sizeof(int)); int dataLen = BitConverter.ToInt32(buffer1, 0); byte[] archiveData = new byte[dataLen]; fs.Read(archiveData, 0, dataLen); TDES tdesTool = new TDES(); tdesTool.Init(ConStr.DES_KEY); byte[] decryptData = tdesTool.Decrypt(archiveData); string txtData = System.Text.Encoding.Default.GetString(decryptData, 0, decryptData.Length); Hashtable hsData = txtData.hashtableFromJson(); tagArchive = Saveable.Facade.LoadRoot <GameRuntimeData>(hsData); fs.Close(); } } _instance = tagArchive;//记录单例 _instance.SaveIndex = fileIndex; _instance.InitAllRole(); return(tagArchive); }
public SlaveWorkFlow() { this.RSAEnrcypter = new RSA(); this.TDESEncrypter = new TDES(); this.fileHandler = new XMLHandler(); this.tdes2 = new TdesV2(); }
/** * Recibe un texto presuntamente encriptado, este * lo desencripta con su llave del TDES generada. **/ public String decryptText(String encryptedText) { if (encryptedText != null && !encryptedText.Equals("")) { try { var textInBytes = fromHexToByte(encryptedText); ICryptoTransform decryptor = TDES.CreateDecryptor(TDES.Key, TDES.IV); using (MemoryStream ms = new MemoryStream(textInBytes)) { using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) { using (StreamReader reader = new StreamReader(cs)) return(reader.ReadToEnd()); } } } catch (Exception) { throw new Exception("No se pudo realizar la acción. Verifique que las claves sean las correctas"); } } return(encryptedText); }
public MasterWorkFlow() { this.RSAEnrcypter = new RSA(); this.TDESEncrypter = new TDES(); this.fileHandler = new XMLHandler(); this.TdesKeys = new string[3]; this.TdesIV = new byte[8]; }
public void MessageReceived(string json) { if (asymkey != null) { json = RSA.Decrypt(asymkey, json); } else if (symkey != null) { json = TDES.Decrypt(symkey, json); } JSONObject jo = new JSONObject(json); callback?.Invoke(this, jo); }
/** * Genera las llaves para el TDES si no hay llaves. * Luego estas pasan a un string hexadecimal. */ public void generateTDESKey() { if (tdesKey1 == null && tdesKey2 == null && tdesKey3 == null) { TDES.GenerateKey(); TDES.GenerateIV(); Console.WriteLine("Original Key: " + Convert.ToBase64String(TDES.Key)); Console.WriteLine("Hex Key: " + BitConverter.ToString(TDES.Key).Replace("-", "")); tdesKey1 = BitConverter.ToString(TDES.Key.Take(8).ToArray()).Replace("-", ""); tdesKey2 = BitConverter.ToString(TDES.Key.Skip(8).Take(8).ToArray()).Replace("-", ""); tdesKey3 = BitConverter.ToString(TDES.Key.Skip(16).Take(8).ToArray()).Replace("-", ""); } }
void MessageReceivedCallback(SocketIOHandler socket, JSONObject message) { NetUtil.Log(socket.IpAddress + ": " + message.ToString()); if (authenticateUserCallback != null) { JSONObject response = authenticateUserCallback.Invoke(message.GetString("username"), message.GetString("password")); if (useTDES) { Send(socket, Convert.ToBase64String(TDES.Encrypt(message.GetString("key"), response.ToString()))); } else { Send(socket, response.ToString()); } } }
public static string Decrypt(Algorithm algorithm, string symmetricKey, string token, string content) { if (algorithm == Algorithm.AES) { content = AES.Decrypt(symmetricKey, content, token); } else if (algorithm == Algorithm.RIJ) { content = RIJ.Decrypt(symmetricKey, content, token); } else if (algorithm == Algorithm.DES) { content = TDES.Decrypt(symmetricKey, content, token); } else { content = string.Empty; } return(content); }
public static string Encrypt(Algorithm algorithm, string symmetricKey, string message, out string IV) { if (algorithm == Algorithm.AES) { return(AES.Encrypt(symmetricKey, message, out IV)); } else if (algorithm == Algorithm.RIJ) { return(RIJ.Encrypt(symmetricKey, message, out IV)); } else if (algorithm == Algorithm.DES) { return(TDES.Encrypt(symmetricKey, message, out IV)); } else { IV = string.Empty; return("Algorithm Issue"); } }
protected void _ConnectedCallback() { JSONObject jsonObject = new JSONObject().Put("username", username).Put("password", password); if (useTDES) { tdesKey = TDES.GenerateKey(); jsonObject.Put("key", tdesKey); } if (rsaKey != null && rsaKey != "") { Send(RSA.Encrypt(rsaKey, jsonObject.ToString())); } else { Send(jsonObject.ToString()); } this.sdw.Symkey = tdesKey; }