贴一个.net 的程序:
public static XmlTextWriter CreateEncryptWriter(string fileName, string passWord, ref FileStream fs) { try { if (! (passWord == null || passWord.Length < 5)) { fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider(); cryptic.Key = ASCIIEncoding.ASCII.GetBytes(passWord); cryptic.IV = ASCIIEncoding.ASCII.GetBytes(passWord); CryptoStream crStream = new CryptoStream(fs, cryptic.CreateEncryptor(), CryptoStreamMode.Write); return new XmlTextWriter(crStream, Encoding.ASCII); } else { return new XmlTextWriter(fileName, Encoding.ASCII); } } catch { throw; } }
public static XmlTextReader CreateDecryptReader(string fileName, string passWord, ref FileStream fs) { try { if (! (passWord == null || passWord.Length < 4)) { fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider(); cryptic.Key = ASCIIEncoding.ASCII.GetBytes(passWord); cryptic.IV = ASCIIEncoding.ASCII.GetBytes(passWord); CryptoStream crStream = new CryptoStream(fs, cryptic.CreateDecryptor(), CryptoStreamMode.Read); return new XmlTextReader(crStream); } else { return new XmlTextReader(fileName); } } catch { throw; } }
c 下的程序复杂些,以后再贴。加密的作用在于传输过程,用于程序保护,有些牵强。这段C# 程序用于设备与 pc 传输过程中,对text 形式的 xml 格式数据的保护。 |