对称加密算法学习(1)


usingSystem;
usingSystem.Security.Cryptography;
usingSystem.IO;
usingSystem.Text;

namespaceSqlResetPwd
{
classProgram
{
staticvoidMain(string[]args)
{
while(true)
{
ConsoleKeyInfoi
=Console.ReadKey(true);
if(i.Key==ConsoleKey.Escape)
{
break;
}

else
{
Console.Write(
"INFileName:");stringinFileName=Console.ReadLine();
Console.Write(
"OUTFileName:");stringoutFileName=Console.ReadLine();
Console.Write(
"PassWord:");stringpassword=Console.ReadLine();
Console.Write(
"Choice0:Encryptother:Dcrypt:");stringChoice=Console.ReadLine();


//Createthepasswordkey
byte[]saltValueBytes=Encoding.ASCII.GetBytes("Thisismysa1t");
Rfc2898DeriveBytespasswordKey
=newRfc2898DeriveBytes(password,saltValueBytes);

//CreatethealgorithmandspecifythekeyandIV
RijndaelManagedalg=newRijndaelManaged();
alg.Key
=passwordKey.GetBytes(alg.KeySize/8);
alg.IV
=passwordKey.GetBytes(alg.BlockSize/8);


if(Choice=="0")
{

encrypt
}

else
{
dencrypt
}

}

}

}

}

}


从中不难看出对称加密解密的步骤:
选择要采用的加解密算法的类
创建相应的KEY,IV
创建要读出或写入的文件流
利用SymmetricAlgorithm.CreateEncryptor() CreateDecryptor()方法创建ICryptoTransform对象
利用ICryptoTransform对象和创建的文件流创建CryptoStream对象
写入或读出加解密的文件流
优质内容筛选与推荐>>
1、写测试用例心得
2、博客园客户端UAP开发随笔 -- App的心动杀手锏:动画
3、远程通信的几种选择(RPC,Webservice,RMI,JMS的区别)
4、[转]wordpress安装插件的3种方式
5、使用Redis实现分布式锁


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号