c# 最简单加密
2007-03-21 16:24:00 weixin_34232363 阅读数 0

/*
  Filename: encrypt_string.cs
  Author : zhanghua
  Date  : 2005-08-11
  Fuction : input a strig  and encrypt a string
            加密后的字符串的第一个字符是原先字符串的最后一个字符,
            其余的每一个字符是对应的原字符串中的前一个字符的值加
            上3。
            example: welcome -> ezhofrp
        
*/
using System;

class Test
{
 public static void Main()
 {
  string strInput, strOutput;
  
  Console.WriteLine("please input a string to encrypt: \n");
  strInput=Console.ReadLine();

  
  Console.WriteLine(" your input string is : {0}\n", strInput);
   //Encrypt(strInput);
  
  strOutput = Encrypt(strInput);
 
   Console.WriteLine("strOutput is :{0}\n", strOutput);
 }
  
  private static string  Encrypt(string  strInput)
  {
   string strFont,  strEnd;
   string strOutput;
   char[] charFont;
   int i,len, intFont;
   
   len = strInput.Length;
   //Console.WriteLine(" strInput @#s length is :{0} \n", len);
   strFont = strInput.Remove(len-1,1);                                 
   strEnd= strInput.Remove(0, len-1);                                 
                                
   //Console.WriteLine(" strFont is : {0} \n" , strFont);                                 
   //Console.WriteLine(" strEnd is : {0} \n" , strEnd);                                 
                                 
     charFont = strFont.ToCharArray();                                 
   for(i=0; i<strFont.Length; i++)                                 
   {                                 
    intFont = (int)charFont[i] + 3;                                 
    //Console.WriteLine(" intFont is : {0} \n", intFont);                                 
                                     
    charFont[i]= Convert.ToChar(intFont);                                   
    //Console.WriteLine("charFont[{0}] is : {1}\n", i, charFont[i]);                                 
   }
     strFont = ""; //let strFont  null
   for (i=0; i<charFont.Length; i++)
   {
      strFont += charFont[i];
    } 
     strOutput=strEnd + strFont;
     return strOutput;
   
  }
}

转载于:https://www.cnblogs.com/hzuIT/articles/682798.html

2005-08-11 22:19:00 hustbill 阅读数 2504

/*
  Filename: encrypt_string.cs
  Author : zhanghua
  Date  : 2005-08-11
  Fuction : input a strig  and encrypt a string
            加密后的字符串的第一个字符是原先字符串的最后一个字符,
            其余的每一个字符是对应的原字符串中的前一个字符的值加
            上3。
            example: welcome -> ezhofrp
        
*/
using System;

class Test
{
 public static void Main()
 {
  string strInput, strOutput;
  
  Console.WriteLine("please input a string to encrypt: /n");
  strInput=Console.ReadLine();

  
  Console.WriteLine(" your input string is : {0}/n", strInput);
   //Encrypt(strInput);
  
  strOutput = Encrypt(strInput);
 
   Console.WriteLine("strOutput is :{0}/n", strOutput);
 }
  
  private static string  Encrypt(string  strInput)
  {
   string strFont,  strEnd;
   string strOutput;
   char[] charFont;
   int i,len, intFont;
   
   len = strInput.Length;
   //Console.WriteLine(" strInput 's length is :{0} /n", len);
   strFont = strInput.Remove(len-1,1);                                 
   strEnd= strInput.Remove(0, len-1);                                 
                                
   //Console.WriteLine(" strFont is : {0} /n" , strFont);                                 
   //Console.WriteLine(" strEnd is : {0} /n" , strEnd);                                 
                                 
     charFont = strFont.ToCharArray();                                 
   for(i=0; i<strFont.Length; i++)                                 
   {                                 
    intFont = (int)charFont[i] + 3;                                 
    //Console.WriteLine(" intFont is : {0} /n", intFont);                                 
                                     
    charFont[i]= Convert.ToChar(intFont);                                   
    //Console.WriteLine("charFont[{0}] is : {1}/n", i, charFont[i]);                                 
   }
     strFont = ""; //let strFont  null
   for (i=0; i<charFont.Length; i++)
   {
      strFont += charFont[i];
    } 
     strOutput=strEnd + strFont;
     return strOutput;
   
  }
}

2016-01-04 17:51:00 Rose_Girls 阅读数 538
class Program
    {
        static string iv = "11111111";
        static string key = "22222222";

        /// <summary>
        /// 对文件内容进行DES加密
        /// </summary>
        /// <param name="sourceFile">待加密的文件绝对路径</param>
        /// <param name="destFile">加密后的文件保存的绝对路径</param>
        public static void EncryptFile(string sourceFile, string destFile)
        {
            //判断当前的文件是否存在
            if (!File.Exists(sourceFile)) 
                throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);
            //将字符串中的所有字符编码为一个字节序列
            byte[] tmpKey = Encoding.Default.GetBytes(key);
            //将字符串中的所有字符编码为一个字节序列
            byte[] tmpIV = Encoding.Default.GetBytes(iv);
            //Crypto:加密 
            //DES:数据加密标准(data encryption standard)  将一些数据加密到内存,然后解密数据
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            //读取这个文件的所有内容 内容是字节
            byte[] btFile = File.ReadAllBytes(sourceFile);
            //文件流 使用指定的路径 创建模式和读/写权限初始化文件流的实例
            using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
            {
                try
                {
                    //定义的加密流对象  CreateEncryptor:用指定的秘钥和初始化向量 创建对称数据加密标准加密对象
                    using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(tmpKey, tmpIV), CryptoStreamMode.Write))
                    {
                        cs.Write(btFile, 0, btFile.Length);
                        cs.FlushFinalBlock();
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    fs.Close();
                }
            }
        }
        /// <summary>
        /// 对文件内容进行DES加密,加密后覆盖掉原来的文件
        /// </summary>
        /// <param name="sourceFile">待加密的文件的绝对路径</param>
        static public void EncryptFile(string sourceFile)
        {
            EncryptFile(sourceFile, sourceFile);
        }

        /// <summary>
        /// 对文件内容进行DES解密
        /// </summary>
        /// <param name="sourceFile">待解密的文件绝对路径</param>
        /// <param name="destFile">解密后的文件保存的绝对路径</param>
        public static void DecryptFile(string sourceFile, string destFile)
        {
            //判断文件是否存在
            if (!File.Exists(sourceFile))
                throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);

            byte[] btKey = Encoding.Default.GetBytes(key);
            byte[] btIV = Encoding.Default.GetBytes(iv);
            //将一些数据加密到内存,然后解密数据
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] btFile = File.ReadAllBytes(sourceFile);
          
            using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
            {
                try
                {
                    //CreateDecryptor(tmpIV, tmpIV):用指定的秘钥btKey,和初始化向量
                    //CryptoStream:加密流 fs:目标数据流 
                    // 返回结果: 
                    //     对称 System.Security.Cryptography.DES 解密器对象。
                    using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
                    {
                        cs.Write(btFile, 0, btFile.Length);
                        cs.FlushFinalBlock();
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    fs.Close();
                }
            }
        }

        /// <summary>
        /// 对文件内容进行DES解密,解密后覆盖掉原来的文件
        /// </summary>
        /// <param name="sourceFile">待解密的文件的绝对路径</param>
        public static void DecryptFile(string sourceFile)
        {
            DecryptFile(sourceFile, sourceFile);
        }

        
        
        static void Main(string[] args)
        {
            //可以得到当前默认的工程文件查找路径
            string path = AppDomain.CurrentDomain.BaseDirectory;
            //EncryptFile("aa.xml");
            DecryptFile("aa.xml");

            Console.ReadKey(); 
        }

2013-03-04 16:08:00 weixin_34348111 阅读数 2

AesManaged - 高级加密标准(AES) 对称算法的管理类

 1  public static class EncryptAndDecrypt
 2     {
 3         //加密
 4         public static string Encrypt(string input)
 5         {
 6             // 盐值
 7             string saltValue = "saltValue";
 8             // 密码值
 9             string pwdValue = "pwdValue";
10             byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(input);
11             byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(saltValue);
12             // AesManaged - 高级加密标准(AES) 对称算法的管理类
13             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
14             // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数)
15             // 通过 密码 和 salt 派生密钥
16             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
17             /**/
18             /*
19          * AesManaged.BlockSize - 加密操作的块大小(单位:bit)
20          * AesManaged.LegalBlockSizes - 对称算法支持的块大小(单位:bit)
21          * AesManaged.KeySize - 对称算法的密钥大小(单位:bit)
22          * AesManaged.LegalKeySizes - 对称算法支持的密钥大小(单位:bit)
23          * AesManaged.Key - 对称算法的密钥
24          * AesManaged.IV - 对称算法的密钥大小
25          * Rfc2898DeriveBytes.GetBytes(int 需要生成的伪随机密钥字节数) - 生成密钥
26          */
27             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
28             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
29             aes.Key = rfc.GetBytes(aes.KeySize / 8);
30             aes.IV = rfc.GetBytes(aes.BlockSize / 8);
31             // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象
32             System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor();
33             // 加密后的输出流
34             System.IO.MemoryStream encryptStream = new System.IO.MemoryStream();
35             // 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接
36             System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
37                 (encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
38             // 将一个字节序列写入当前 CryptoStream (完成加密的过程)
39             encryptor.Write(data, 0, data.Length);
40             encryptor.Close();
41             // 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串
42             string encryptedString = Convert.ToBase64String(encryptStream.ToArray());
43             return encryptedString;
44         }
45 
46 
47         #region silverlight密码解密
48         /**/
49         /// <summary>
50         /// 解密数据
51         /// </summary>
52         /// <param name="input">加密后的字符串</param>
53         /// <returns>加密前的字符串</returns>
54         public static string Decrypt(string input)
55         {
56             // 盐值(与加密时设置的值一致)
57             string saltValue = "saltValue";
58             // 密码值(与加密时设置的值一致)
59             string pwdValue = "pwdValue";
60             byte[] encryptBytes = Convert.FromBase64String(input);
61             byte[] salt = Encoding.UTF8.GetBytes(saltValue);
62             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
63             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
64             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
65             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
66             aes.Key = rfc.GetBytes(aes.KeySize / 8);
67             aes.IV = rfc.GetBytes(aes.BlockSize / 8);
68             // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象
69             System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor();
70             // 解密后的输出流
71             MemoryStream decryptStream = new MemoryStream();
72             // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接
73             System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
74                 decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
75             // 将一个字节序列写入当前 CryptoStream (完成解密的过程)
76             decryptor.Write(encryptBytes, 0, encryptBytes.Length);
77             decryptor.Close();
78             // 将解密后所得到的流转换为字符串
79             byte[] decryptBytes = decryptStream.ToArray();
80             string decryptedString = UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
81             return decryptedString;
82         }
83         #endregion
84     }

 

转载于:https://www.cnblogs.com/zxbzl/archive/2013/03/04/2942939.html

2014-07-29 10:41:00 weixin_34124577 阅读数 1

1.加密

 1 public class EncryptHepler {
 2         // 验值 
 3         static string saltValue = "XXXX";
 4         // 密码值 
 5         static string pwdValue = "XXXX";
 6  
 7         /// <summary>
 8         /// 加密
 9         /// </summary>
10         public static string Encrypt( string input ) {
11             byte[ ] data = System.Text.UTF8Encoding.UTF8.GetBytes( input );
12             byte[ ] salt = System.Text.UTF8Encoding.UTF8.GetBytes( saltValue );
13  
14             // AesManaged - 高级加密标准(AES) 对称算法的管理类 
15             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( );
16             // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数) 
17             // 通过 密码 和 salt 派生密钥 
18             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt );
19  
20             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
21             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
22             aes.Key = rfc.GetBytes( aes.KeySize / 8 );
23             aes.IV = rfc.GetBytes( aes.BlockSize / 8 );
24  
25             // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象 
26             System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor( );
27             // 加密后的输出流 
28             System.IO.MemoryStream encryptStream = new System.IO.MemoryStream( );
29             // 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接 
30             System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
31                 ( encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );
32  
33             // 将一个字节序列写入当前 CryptoStream (完成加密的过程)
34             encryptor.Write( data, 0, data.Length );
35             encryptor.Close( );
36             // 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串 
37             string encryptedString = Convert.ToBase64String( encryptStream.ToArray( ) );
38             return encryptedString;
39         }
View Code

2.解密

 1 /// <summary>
 2         /// 解密
 3         /// </summary>
 4         public static string Decrypt( string input ) {
 5             byte[ ] encryptBytes = Convert.FromBase64String( input );
 6             byte[ ] salt = Encoding.UTF8.GetBytes( saltValue );
 7             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( );
 8             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt );
 9  
10             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
11             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
12             aes.Key = rfc.GetBytes( aes.KeySize / 8 );
13             aes.IV = rfc.GetBytes( aes.BlockSize / 8 );
14  
15             // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象 
16             System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor( );
17             // 解密后的输出流 
18             System.IO.MemoryStream decryptStream = new System.IO.MemoryStream( );
19  
20             // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接 
21             System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
22                 decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );
23             // 将一个字节序列写入当前 CryptoStream (完成解密的过程) 
24             decryptor.Write( encryptBytes, 0, encryptBytes.Length );
25             decryptor.Close( );
26  
27             // 将解密后所得到的流转换为字符串 
28             byte[ ] decryptBytes = decryptStream.ToArray( );
29             string decryptedString = UTF8Encoding.UTF8.GetString( decryptBytes, 0, decryptBytes.Length );
30             return decryptedString;
31         }
32     }//class end
View Code

 

转载于:https://www.cnblogs.com/zxbzl/p/3874940.html

C#: 最简单的文件读写

阅读数 610

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;namespaceWindowsFormsAppli

博文 来自: yenange

C#最简单例子

阅读数 18

usingSystem;namespaceConsoleApplication1{classPeople{intage;stringname;publicstringgetname(){name="xiaofang";...

博文 来自: weixin_34320724

C#: 最简单的文件读写

阅读数 22

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;using...

博文 来自: iteye_20954

c# 最简单的线程

阅读数 243

(newThread(()=>{MessageBox.Show("处理完成");Application.Current.Dispatcher.BeginInvoke(newAction(()=>{(senderasButton).IsEnabled=tr

博文 来自: qq_15572445

最简单的C#快速入门

阅读数 30

在一小时内学会C#。使用例程,简单却完整的探索C#语言的构造和特点。本文特别适合有C++基础却没有太多精力学习C#的读者。关于作者AishaIkram我现在在英国一家软件公司任技术带头人。我是计算机科学的硕士。我主要使用.NET1.1/2.0,C#,VB.NET,ASP.NET,VC++6,MFC,ATL,COM/DCOM,SQL...

博文 来自: weixin_34192993
没有更多推荐了,返回首页