当前位置:文档之家› 数据的加密与一致性保持

数据的加密与一致性保持

数据的加密与一致性保持
数据的加密与一致性保持

数据的加密和一致性保持

1.实验原理:

加密是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容。加密技术是最常用的安全保密手段,利用技术手段把重要的数据变为乱码(加密)传送,到达目的地后再用相同或不同的手段还原(解密)。加密技术包括两个元素:算法和密钥。算法是将普通的信息或者可以理解的信息与一串数字(密钥)结合,产生不可理解的密文的步骤,密钥是用来对数据进行编码和解密的一种算法。在安全保密中,可通过适当的钥加密技术和管理机制来保证网络的信息通信安全。

有的时候我们既要加密在网络中传输的数据,还需要保证我们传输的数据没有被篡改过,数据的一致性可以通过哈希算法来保证,常用的有MD5和SHA-1等等,利用哈希算法在数据发出之前算出一块数据的哈希值,这个哈希值可以用来验证数据在网络传输的过程中有没有被篡改过。在真实的网络环境中,加密可以在不同层中完成,比如ip层的IPsec协议,传输层的ssl或者叫TLS等等。

2.实验目的:

掌握网络加密基本原理,掌握添加ns2协议补丁并利用补丁完成仿真的方法。3.实验设计:

ns2中没有实现数据加密的算法,所以传输的数据是没有经过加密的,我们要观察网络中数据的加密结果就需要对ns2打补丁,在这一节中,我们先介绍为ns2加入一个传输加密数据的应用层协议,然后观察协议运行的结果。协议运行如图所示:

发送者在发送数据前要对数据进行加密(我们可以不用去管具体加密方法),然后取hash值,将加密的数据和hash值传个接受者,接受者使用事先约定好的密钥进行解密。

4.实验步骤

1)将放入到apps文件夹下。

2)修改makefile,在OBJ_CC下面加入apps/Security_packet.o

3)在common/packet.h文件中的packet_t中适当位置(最后倒数第二)加入

PT_SECURITY_PACKET;在class p_info中加入name_[PT_SECURITY_PACKET] = "security_packet";

4)在NS_HOME目录下执行make命令重新编译。

5.实验代码

#Create a simulator object

set ns [new Simulator]

#Define different colors for data flows (for NAM)

$ns color 1 Blue

$ns color 2 Red

#Open a trace file

set nf [open out.nam w]

$ns namtrace-all $nf

#Define a 'finish' procedure

proc finish {} {

global ns nf

$ns flush-trace

close $nf

exec nam out.nam &

exit 0

}

#Create four nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set n5 [$ns node]

#Create links between the nodes

$ns duplex-link $n0 $n2 5Mb 10ms DropTail

$ns duplex-link $n1 $n2 5Mb 10ms DropTail

$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail

$ns duplex-link $n3 $n4 5Mb 10ms DropTail

$ns duplex-link $n3 $n5 5Mb 10ms DropTail

#Set Queue Size of link (n2-n3) to 100

$ns queue-limit $n2 $n3 100

$ns duplex-link-op $n0 $n2 orient right-down

$ns duplex-link-op $n1 $n2 orient right-up

$ns duplex-link-op $n2 $n3 orient right

$ns duplex-link-op $n3 $n4 orient right-up

$ns duplex-link-op $n3 $n5 orient right-down

#Define a 'recv' function for the class 'Agent/Security_packet'

Agent/Security_packet instproc recv {from rtt mess originmess hash} { $self instvar node_

puts "node [$node_ id] received packet from \

$from with trip-time $rtt ms - contend: $mess - decrypted $originmess -hash: $hash"

}

#使用新的协议的方法使用attach-agent来关联。

#Create two ping agents and attach them to the nodes n0 and n2

set p0 [new Agent/Security_packet]

$ns attach-agent $n0 $p0

$p0 set class_ 1

set p1 [new Agent/Security_packet]

$ns attach-agent $n1 $p1

$p1 set class_ 1

set p2 [new Agent/Security_packet]

$ns attach-agent $n4 $p2

$p2 set class_ 2

set p3 [new Agent/Security_packet]

$ns attach-agent $n5 $p3

$p3 set class_ 2

#Connect the two agents

$ns connect $p0 $p3

$ns connect $p1 $p2

#Schedule events

for {set i 1} {$i < 2} {incr i} {

set result [expr $i /2]

$ns at $result "$p0 send itisalongmessageIcansend"

$ns at [expr $result + 0.02] "$p1 send Itisashotermessage"

$ns at [expr $result + 0.04] "$p2 send test3"

$ns at [expr $result + 0.06] "$p3 send test4"

}

$ns at 1.0 "finish"

#Run the simulation

$ns run

6.实验数据分析:

运行脚本得到如下结果:

通过我们截取的一段信息可以看到信息被加密并且去验证了他的hash值。

相关主题
文本预览
相关文档 最新文档