本人初学c#,在做个记事本,在“保存”这一功能中,我想给即将保存的文件加密,但不想用复杂的加密算法,就希望当其它软件打开保密过的文件时会出现乱码,以此起到保密功能,这该如何实现呢?还有,当用我自己做的这个日记本读取保密过的文本时,要能恢复正常的显示的。希望大家指教一下,谢谢。
解决方案 »
- ************正则表达式问题*****************
- 关于数据库读取问题
- 封装Session
- WinForm的DataGrid里面显示数据的小方格是不是TextBox?能否替换成Label?
- 为什么就没数据显示呢?
- 一个让我想了好久也没有搞定的问题!(按钮方面的)
- 我在winform上画出来的带箭头直线为什么是这个样子?
- 用我的机子,访问aspx页面变形!控件好像属性没有设置过一样的!
- 我做一个控件 UserContorl,我想使它具有boderStyle 属性(none,fixedSingle,fixed3d,) 如何实现?
- C#做一个类似VS这样的工具箱,能够当控件一样拖出来用的。
- 这样的序号怎么产生
- 16个数字选5个怎么实现
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();
}
}
}
}
#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