byte[] Encrypt(byte[] buffer) { var empty = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; var des = new DESCrypto(empty, empty); return(des.Encrypt(buffer)); }
//存储配置文件数据 public static void WriteDataInCacheDir(string filePath, string text) { filePath = OuterDir + "/" + filePath; string dir = GetParentDir(filePath); if (Directory.Exists(dir) == false) { Directory.CreateDirectory(dir); } //加密 if (SystemConfig.Instance.IsEncryptConfigFile) { if (filePath.EndsWith(EncryptXMLFileSuffix) == false) { filePath = filePath + EncryptXMLFileSuffix; } byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(text); data = DESCrypto.Encrypt(data, EncryptKey); File.WriteAllBytes(filePath, data); } else { File.WriteAllText(filePath, text); } }
public static void EncodeConfigFile() { if (SystemConfig.Instance.IsEncryptConfigFile == false) { return; } List <string> configList = new List <string>(); MyFileUtil.GetFileList(MyFileUtil.InnerConfigDir, ref configList, ".xml"); foreach (string fileName in configList) { if (fileName.EndsWith(".meta") == true) { continue; } byte[] data = File.ReadAllBytes(fileName); data = DESCrypto.Encrypt(data, MyFileUtil.EncryptKey); File.WriteAllBytes(fileName + MyFileUtil.EncryptXMLFileSuffix, data); //MyFileUtil.DeleteFile(fileName); } AssetDatabase.Refresh(); Debug.Log("加密配置文件结束"); }
internal static string EncryptAndSign(string txt) { string sign = Md5Encrypt(txt + HeaderContextHelper.PRIVATE_KEY); string content = DESCrypto.Encrypt(txt); return(content.Length.ToString() + ";" + content + sign); }
static public void EncryptLuaCode(string filePath) { if (SystemConfig.Instance.IsEncryptLuaCode) { byte[] data = File.ReadAllBytes(filePath); data = DESCrypto.Encrypt(data, MyFileUtil.EncryptKey); File.WriteAllBytes(filePath, data); } }
//加密Lua代码 static public byte[] EncryptLuaCode(byte[] data) { if (SystemConfig.Instance.IsEncryptLuaCode) { return(DESCrypto.Encrypt(data, MyFileUtil.EncryptKey)); } return(data); }
static void Main(string[] args) { string plaintext = "abc"; string strmd5 = "900150983CD24FB0D6963F7D28E17F72"; string strdes = "Wr+MOY7jM8M="; //MD5 var s1 = MD5Crypto.Encrypt(plaintext); Console.WriteLine("MD5 加密:{0} {1} {2}", s1 == strmd5, strmd5, s1); Console.WriteLine(); //DES var s2 = DESCrypto.Encrypt(plaintext); Console.WriteLine("DES 加密:{0} {1} {2}", s2 == strdes, strdes, s2); var s3 = DESCrypto.Decrypt(s2); Console.WriteLine("DES 解密:{0} {1} {2}", s3 == plaintext, plaintext, s3); Console.WriteLine(); //RSA string modulus = "nJDcBWNIV+DzZb8ZY5h4JJInVwVy5NvJ9hG0qH0TUM36j5DUFeUivBIdX+7fxwKIxPRkRyvwVjGjnMxna3Kq53Y5BLGpl84DvRqPGjxly2kAitbuHRIR5iiuza0rbA+ZPo/8kNrbRCYquaqnL1KIrDcIh7bZDWN6qY22+RVaVvs="; string publicExponent = "AQAB"; string privateExponent = "ZZTTPCerc2D/ar9vYKA3KzssjRh68CPuSFo6hasJEj9iVy2XfVE6lR2Hs4uP41YwmOEcAtVuTO5OAljYrO0sFpdYNrEthZG5UBkC2wH+SsXOAaTDb2YRCEsdxFA8MRqRQLux/9/Fef/oIk+od1sjC3WzBwMqvVHBO232u9V9suE="; //公钥加密 var s4 = RSACrypto.EncryptByPublicExponent(plaintext, modulus, publicExponent); Console.WriteLine("RSA 公钥加密:" + s4); string p = "y8v6G7Ap6jeTHILLAjQT0auvd9kRh91txQ4YkGf8ocijRbThKgAtWUrvNx27km6PEetWqw0VnA2YN53v6WCBMQ=="; string q = "xKuz+9vgOYZf9WvA0vU8byCmZd93mYrw+uAymUiT6jvG1MQ0vAQMW7wwoifJYIqWFNZo356R6g2OeOz8Edfv6w=="; string dp = "vqI3et721ljWC71tGMqOH3txz7IFbAn9PG9LGwmqj8uWrwXb+eXghb5KtkvhwcAZpLF3iNncdPViheP/H1degQ=="; string dq = "dZ+ruYo7hKwVYBbd8E2zo1MHsg4A3df3YFQObxa1QHYX6NCgKYLSUVswSws4qYC5WiUR/Aw+gJkzCKfT6mgXmQ=="; string inverseQ = "Fohpu2QfkHBCy11L0MV88pX3+EszJWWSgXqsGUzxTx0c2WK33o5wZkjq0AEKgk19aOOJc0RoKwcw6vtRRux/+Q=="; //私钥解密 var s5 = RSACrypto.DecryptByPrivateExponent(s4, modulus, privateExponent, publicExponent, p, q, dp, dq, inverseQ); var s55 = RSACrypto.Decrypt(s4, modulus, privateExponent); Console.WriteLine("RSA 私钥解密 : {0} {1} {2}", s5 == plaintext, plaintext, s5); Console.WriteLine("RSA 私钥解密2: {0} {1} {2}", s55 == plaintext, plaintext, s55); Console.WriteLine(); //私钥加密 var s6 = RSACrypto.Encrypt(plaintext, modulus, privateExponent); Console.WriteLine("RSA 私钥加密:" + s6); //公钥解密 var str6 = RSACrypto.Decrypt(s6, modulus, publicExponent); Console.WriteLine("RSA 公钥解密2:{0} {1} {2}", str6 == plaintext, plaintext, str6); Console.ReadKey(); }
static void GenerateCopyProjectFileList() { string dir = CopyProjectTargetDir + "Assets/StreamingAssets/AssetBundles/" + ResourcesManager.GetPlatformDir() + "/"; List <string> fileList = ResourcesManager.GenerateFileList(dir); string xml = ResourcesManager.GetFileListXMLString(fileList, dir); string filePath = CopyProjectTargetDir + "Assets/StreamingAssets/Config/" + ResourcesManager.FileListConfigFileName + MyFileUtil.EncryptXMLFileSuffix; File.WriteAllText(filePath, xml); DESCrypto.Encrypt(filePath, MyFileUtil.EncryptKey); Debug.Log("生成拷贝工程资源列表结束"); }
void Start() { string content = "测试一下DES加密,hahhaha"; string key = "abcdefgh454365"; //key,iv 可以相同 string iv = "abcdefgh35345"; byte[] contentBytes = Encoding.UTF8.GetBytes(content); byte[] encryBytes = DESCrypto.Encrypt(contentBytes, key, iv); string encryStr = Encoding.UTF8.GetString(encryBytes); Debug.Log(encryStr); byte[] decryBytes = DESCrypto.Decrypt(encryBytes, key, iv); string decryStr = Encoding.UTF8.GetString(decryBytes); Debug.Log(decryStr); }
public void Test() { using (var desCrypto = new DESCrypto()) { desCrypto.Initialize(new Dictionary <string, object> { { "Key", "qxMfZpmQ1Rk=" }, { "IV", "XaX73vwx694=" } }); var plainText = "SmartSql"; var cipherText = desCrypto.Encrypt(plainText); var decryptText = desCrypto.Decrypt(cipherText); Assert.Equal(plainText, decryptText); } }
static void EncryptFile() { foreach (UnityEngine.Object obj in Selection.objects) { string path = AssetDatabase.GetAssetPath(obj.GetInstanceID()); if (string.IsNullOrEmpty(path)) { continue; } string filePath = Application.dataPath.Replace("Assets", "") + path; DESCrypto.Encrypt(filePath, MyFileUtil.EncryptKey); } AssetDatabase.Refresh(); Debug.Log("文件加密结束"); }
/***************************************************************************************/ public static byte[] EncryptData(byte[] data) { data = DESCrypto.Encrypt(data, EncryptKey); return(data); }