using System;
using System.Security.Principal;
using System.Security.Permissions;
using System.Security.Cryptography; namespace Demo
{
class MainClass
{
/// <summary>
/// DESCryptoServiceProvider
/// </summary>
public static void DESCryptoDemo ()
{
string message = "This message is security!";
string strEncryp, strDecryp; DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
System.Text.Encoding encoding = new System.Text.UTF8Encoding (); byte[] key = encoding.GetBytes ("1a345689");
byte[] iv = { , , , , , , , };
ICryptoTransform encryptor = des.CreateEncryptor (key, iv);
ICryptoTransform decryptor = des.CreateDecryptor (key, iv);
//all above 4 lines can be TiHuan as below
des.GenerateKey ();
des.GenerateIV ();
encryptor = des.CreateEncryptor ();
decryptor = des.CreateDecryptor ();
{
byte[] byteMessage = encoding.GetBytes (message);
byte[] byteEncrypto = encryptor.TransformFinalBlock (byteMessage, , byteMessage.Length);
strEncryp = Convert.ToBase64String (byteEncrypto);
}
{
byte[] byteEncryto2 = Convert.FromBase64String (strEncryp);
byte[] byteDecrypto = decryptor.TransformFinalBlock (byteEncryto2, , byteEncryto2.Length);
strDecryp = encoding.GetString (byteDecrypto);
}
Console.WriteLine ("Message:" + message);
Console.WriteLine ("Encrypted:" + strEncryp);
Console.WriteLine ("Decrypted:" + strDecryp);
Console.ReadKey ();
} public static void Invoke (Action action)
{
try {
action ();
} catch (Exception ex) {
Console.WriteLine (ex.Message);
}
} public static void Main (string[] args)
{
DESCryptoDemo ();
Invoke (() => TestMethod1 ());
Invoke (() => TestMethod2 ());
Invoke (() => TestMethod3 ());
Console.ReadKey ();
} [PrincipalPermission (SecurityAction.Demand, Name = "Administrator")]
public static void TestMethod1 ()
{
Console.WriteLine ("TestMethod1 successfully Executed!");
} [PrincipalPermission (SecurityAction.Demand, Role = "Administrators")]
public static void TestMethod2 ()
{
Console.WriteLine ("TestMethod2 successfully Executed!");
} [PrincipalPermission (SecurityAction.Demand, Role = "Guests")]
public static void TestMethod3 ()
{
Console.WriteLine ("TestMethod3 successfully Executed!");
}
}
}

DES对称性加密的更多相关文章

  1. DES对称加密

    DES是对称性加密里面常见一种,全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法.密钥长度是64位(bit),超过位数密钥被忽略.所谓对称性加密,加密 ...

  2. .NET和JAVA同等加密方法,MD5和DES对称加密记录

    C#版: using System; using System.Security.Cryptography; using System.Text; namespace ConsoleApplicati ...

  3. Java和.NET使用DES对称加密的区别

    Java和.NET的系统类库里都有封装DES对称加密的实现方式,但是对外暴露的接口却各不相同,甚至有时会让自己难以解决其中的问题,比如Java加密后的结果在.NET中解密不出来等,由于最近项目有跨Ja ...

  4. 基于DES算法加密的防撞库密码系统项目总结

    项目内容:基于DES算法加密的防撞库密码系统 小组名:zqhzkzkj 目标:1.对用户输入的8位字符进行DES加密,要求用户输入8位密钥 2.对于不同的网站,不同的用户名生成不同的密码 小组成员:周 ...

  5. 使用openssl库实现des,3des加密

    原文地址: 使用openssl库实现des,3des加密 主要是调整了一下格式,以及一些变量的类型,以解决在VC2008下无法编译通过的问题. #include <stdio.h> #in ...

  6. PHP使用DES进行加密解密

    DES是一种对称加密算法,也就是通过密文和合法的密钥能够将明文还原出来,在程序开发过程中有些 接口可能需要获取原始数据,而发送的数据又比较敏感(比如用户的密码等信息),这时可以选择DES加密算法,DE ...

  7. DES的加密与解密算法(Python实现)

    DES的加密与解密算法(Python实现) 密码学实验:实现了DES的简单的加密和解密算法,DES算法的相关资料网上很多,这里不再赘述,仅仅贴出源代码给大家分享,源码中包含很多汉字注释,相信大家都是可 ...

  8. NET实现RSA AES DES 字符串 加密解密以及SHA1 MD5加密

    本文列举了    数据加密算法(Data Encryption Algorithm,DEA) 密码学中的高级加密标准(Advanced EncryptionStandard,AES)RSA公钥加密算法 ...

  9. .NET中的DES对称加密

    DES是一种对称加密(Data Encryption Standard)算法,于1977年得到美国政府的正式许可,是一种用56位密钥来加密64位数据的方法.一般密码长度为8个字节,其中56位加密密钥, ...

随机推荐

  1. Swift笔记

    最近从Xcode6 beta4开始到现在的Xcode6.0.1,使用Swift一段时间了,Swift大体来说,语法与java.c++比较接近,相比objective-c要友好多了,也更容易上手,这里记 ...

  2. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xm

    mybatis解析xml配置文件出现异常: org.apache.ibatis.exceptions.PersistenceException: Error building SqlSession. ...

  3. nginx配置文件语法高亮显示方法

    1.去官方站点下载nginx.vim http://www.vim.org/scripts/script.php?script_id=1886 2.创建nginx.vim的存放目录 # mkdir - ...

  4. css伪类选择器及伪元素选择器

    1.类选择器 在css中可以使用类选择器把相同的元素定义成不同的样式.比如: 结果如下: 标题背景未变 2.伪类选择器 类选择器和伪类选择器的区别在于,类选择器我们定义的,而伪类选择器是CSS中已经定 ...

  5. Linux-awk直接修改原文件

    #注意:重定向符号后面的文件名需要加双引号 awk '{print > "file"}' file

  6. chrome/ie中图片底部多出几像素问题

    如果给图片的父元素设置背景色,则图片的底部就会多出几像素,chrome/IE/FF中均是如此,如下图: 代码如下,就是简单的div中放张图片: <!doctype html> <ht ...

  7. split 转义

    问题:str.split("?");报错了 原因: .?*+!^$[]()\在正则里需要转义 转义需双反斜线.splt("\\+");

  8. Linux GCC常用命令

    1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Compilation) 2.3汇编(Assembly) 2.4连接(Linking) 3多个程序文件的编译 4检错 5库文件连接 5.1编译成 ...

  9. C#中双问号、双冒号等几个特殊关键字

    1.@ 这个东东看似和邮件有关啊,但是在C#的世界里,可跟邮件没有一毛钱关系,它是string的女朋友(当然了string有N多女友),二者结合就可以发挥作用了.你可以给它起个名字,叫做“逐字字符串” ...

  10. C# HmacMD5 加密

    string HmacMD5(string source, string key) { HMACMD5 hmacmd = new HMACMD5(Encoding.Default.GetBytes(k ...