本人初学c#,在做个记事本,在“保存”这一功能中,我想给即将保存的文件加密,但不想用复杂的加密算法,就希望当其它软件打开保密过的文件时会出现乱码,以此起到保密功能,这该如何实现呢?还有,当用我自己做的这个日记本读取保密过的文本时,要能恢复正常的显示的。希望大家指教一下,谢谢。

解决方案 »

  1.   

    最简单的,对文本进行Base64加密解密就行了……           string str1 = Convert.ToBase64String(Encoding.UTF8.GetBytes("文本"));//加密            string str2 = Encoding.UTF8.GetString(Convert.FromBase64String("加密后的文本"));//解密
      

  2.   

    或者用这个类:using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;namespace CommonLib
    {
        public class Security
        {
            //可以更改byte的值,但必须是8个byte
            static byte[] KEY_64 = new byte[8] { 54, 224, 97, 32, 70, 109, 204, 115 };
            static byte[] IV_64 = new byte[8] { 21, 19, 57, 110, 105, 159, 77, 45 };
            public static string Encode(string data)
            {
                byte[] byKey = KEY_64;
                byte[] byIV = IV_64;            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                int i = cryptoProvider.KeySize;
                MemoryStream ms = new MemoryStream();
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);            StreamWriter sw = new StreamWriter(cst);
                sw.Write(data);
                sw.Flush();
                cst.FlushFinalBlock();
                sw.Flush();
                return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);        }        public static string Decode(string data)
            {
                byte[] byKey = KEY_64;
                byte[] byIV = IV_64;            byte[] byEnc;
                try
                {
                    byEnc = Convert.FromBase64String(data);
                }
                catch
                {
                    return null;
                }            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream(byEnc);
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cst);
                return sr.ReadToEnd();
            }        public static class MD5Hashing
            {
                private static MD5 md5 = MD5.Create();
                /// <summary> 
                /// 使用utf8编码将字符串散列 
                /// </summary> 
                /// <param name="sourceString">要散列的字符串</param> 
                /// <returns>散列后的字符串</returns> 
                public static string HashString(string sourceString)
                {
                    return HashString(Encoding.UTF8, sourceString);
                }
                /// <summary> 
                /// 使用指定的编码将字符串散列 
                /// </summary> 
                /// <param name="encode">编码</param> 
                /// <param name="sourceString">要散列的字符串</param> 
                /// <returns>散列后的字符串</returns> 
                public static string HashString(Encoding encode, string sourceString)
                {
                    byte[] source = md5.ComputeHash(encode.GetBytes(sourceString));
                    StringBuilder sBuilder = new StringBuilder();
                    for (int i = 0; i < source.Length; i++)
                    {
                        sBuilder.Append(source[i].ToString("x"));
                    }
                    return sBuilder.ToString();
                }
            } 
        }
    }
      

  3.   

    so,easy,最简单的方法是随机取一个值设为x,让每一个Byte与x异或,就可以加密了,解密的时候让异或之后的值与x再异或一次就okay。因为y^x^x= y
      

  4.   


    #include <iostream>
    #include <string.h>
    using namespace std;void EnCode(char* buffer, int iKey);
    void Decode(char* buffer, int iKey);int main(){
    char buffer[100];
    cin>>buffer;
    EnCode(buffer, 90);
    cout<<buffer<<endl;
    Decode(buffer, 90);
    cout<<buffer<<endl;
    }void EnCode(char* buffer, int iKey){
    for(int i= 0; i< strlen(buffer); ++i){
    buffer[i]^= iKey;
    }
    }void Decode(char* buffer, int iKey){
    for(int i= 0; i< strlen(buffer); ++i){
    buffer[i]^= iKey;
    }
    }
    这是代码,楼主可以看看,soeasy
      

  5.   

    度娘,搜索DES加密,可以满足你的要求也可以设置密码输入。了解一下各类加密算法的特点、用法就easy了。顺便忠告md5和base64不适应。base64基本不具备加密的功能