【数据加密】

加密用于达到以下目的:

  • 保密性:帮助保护用户的标识或数据不被读取。

  • 数据完整性:帮助保护数据不被更改。

  • 身份验证:确保数据发自特定的一方。公钥加密还可以提供不可否认性。

 

加密基元

用法

私钥加密(对称加密)

对数据执行转换,使第三方无法读取该数据。此类型的加密使用单个共享的机密密钥来加密和解密数据。

公钥加密(不对称加密)

对数据执行转换,使第三方无法读取该数据。此类加密使用公钥/私钥对来加密和解密数据。

加密签名

通过创建对特定方唯一的数字签名来帮助验证数据是否发自特定方。此过程还使用哈希函数。

加密哈希

将数据从任意长度映射为定长字节序列。哈希在统计上是唯一的;不同的双字节序列不会哈希为同一个

 

1) 私钥加密IV初始向量和私钥。 传的时候传IV初始向量,私钥双方都预先知道。然后加密解密。



                块密码(例如数据加密标准 (DES)、TripleDES 和高级加密标准 (AES))可将 n 字节的输入块通过加密转换为由加密字节构成的输出块。如果要加密或解密字节序列,必须逐块进行。由于 n 很小(DES 和 TripleDES 为 8 字节;AES 为 16 字节 [默认值]、24 字节或 32 字节),因此如果数据值大于 n,则必须一次加密一个块。如果数据值小于 n,则必须将其扩展为 n 才能进行处理。

 

 

 

 

DES加密文件using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Security.Cryptography; //数据加密算法
///
namespace _55tuan
{
    public class EncryptData
    {
        // Encrypt the string.
        public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
        {
            // Create a memory stream.
            MemoryStream ms = new MemoryStream();

            // Create a CryptoStream using the memory stream and the 
            // CSP DES key.  
            CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);

            // Create a StreamWriter to write a string
            // to the stream.
            StreamWriter sw = new StreamWriter(encStream);

            // Write the plaintext to the stream.
            sw.WriteLine(PlainText);

            // Close the StreamWriter and CryptoStream.
            sw.Close();
            encStream.Close();

            // Get an array of bytes that represents
            // the memory stream.
            byte[] buffer = ms.ToArray();

            // Close the memory stream.
            ms.Close();

            // Return the encrypted byte array.
            return buffer;
        }

        // Decrypt the byte array.
        public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
        {
            // Create a memory stream to the passed buffer.
            MemoryStream ms = new MemoryStream(CypherText);

            // Create a CryptoStream using the memory stream and the 
            // CSP DES key. 
            CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);

            // Create a StreamReader for reading the stream.
            StreamReader sr = new StreamReader(encStream);

            // Read the stream as a string.
            string val = sr.ReadLine();

            // Close the streams.
            sr.Close();
            encStream.Close();
            ms.Close();

            return val;
        }

    }
    

}

加密,解密语句 // Create a new DES key.
                DESCryptoServiceProvider key = new DESCryptoServiceProvider();

                // Encrypt a string to a byte array.
                byte[] bytname =EncryptData.Encrypt(name, key);

                // Decrypt the byte array back to a string.
                string nameCookie = EncryptData.Decrypt(bytname, key);

 

2) 公钥加密(RSA,DSA):    A向B发送消息,首先索要B的公钥(非对称)用来加密,B收到后用自己的私钥进行 解密。     公钥是公开的,接收别人公钥要验证公钥的副本,验证是否被篡改。

公钥加密通常用于加密一个私钥算法将要使用的密钥和 IV。在传输密钥和 IV 后,将对会话的其余部分应用私钥加密。



特点: (与私钥加密相比)加密量小,加密慢,不过更安全。



 

 

 

3) 数字签名 -作用:鉴别信息是否为对方发送数字签名验证发送方的身份(如果您信任发送方的公钥)并帮助保护数据的完整性。使用由小红生成的公钥,小红的数据的接收者可以通过将数字签名与小红的数据和小红的公钥进行比较来验证是否是小红发送了该数据。

为了使用公钥加密对消息进行数字签名,小红首先将哈希算法应用于该消息以创建消息摘要。该消息摘要是数据的紧凑且唯一的表示形式。然后,小红用她的私钥加密该消息摘要以创建她的个人签名。在收到消息和签名时,小明使用小红的公钥解密签名以恢复消息摘要,并使用与小红所使用的相同的哈希算法来散列消息。如果小明计算的消息摘要与从小红那里收到的消息摘要完全一致,小明就可以确定该消息来自私钥的持有人,并且数据未被修改过。如果小明相信小红是私钥的持有人,则他将知道该消息来自小红。


posted on 2012-03-05 13:46 水墨.MR.H 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/StudyLife/archive/2012/03/05/2380348.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值