Monthly Archives: July 2010



The output may look like,

   Operation | Options | Object Name | Cost | Cardinality | Bytes

Operation, Options: 数据库在这一步采取的操作及其使用的参数. 请参考

Object Name: Name of the table or index.

Cost:  a function of the CPU_COST and IO_COST. 它是无量纲的,仅用于各步操作的代价比较

Cardinality: the number of rows accessed by the operation

Bytes:   the number of bytes accessed by the operation


Network Security Essentials — Notes4. Public-key Cryptography

1. Public-key Cryptography

   The schem uses two keys, if one is for encryption, then the other is for decryption.

   "Truly revolutionary advance in cryptography"

   "Profound concequences in the areas of Confidentiality, key distrituion and Message Authentication"

2.Six Integriends


  b.Encryption Algorithm (Not Secret)

  c.Public Key (Not Secret)

  d.Private Key (Only known to self)

  e.Decryption Algorithm (Not Secret)

3.Use Case 1 — Encryption

  a.Sender encrypts the msg with receiver’s public key and then sends it.

  b.Receiver decrypts the msg with its own private key. It would succeed.

4.Use Case 2 — Digital Signature

  Problem: C sends a message to B and saying "My name is A". Should B believe this is really from A?

  Solution: A sends a message and attach a signature ( encrypt(message, A’s private key) ). B then decrypts the signature with A’s public key and finds out that the plaintext  = message. Then he believes this message is indeed from A.

5. Use Case 3 — Exchange secret key for a short conversation

  Problem: It’s not safe to deliver secret keys via Email or IM.

  Solution: Encrypt/decrypt the secret key with public/private key

     a. A(The sender) generates a secret key for conversation and encrypt(msg, secretKey)

     b. A calls  encrypt(secretKey, B’s public key)

     c. A sends the encrypted msg and the encryped secret key

     d. B gets the secret key by calling decrypt(encrypted secret key, B’s private key)

     e. B then get the msg in plaintext by calling decrypt(encryptedMsg, secretKey)

6. What if C announces "My name is A. And this is my public key" ?


      a. A (The real sender) puts his name and his public key in a "certificate", and it along with the message

      b. B (The receiver) should read this certificate and see if A’s name is on it

      c. But the certificate can be a fake one. So B should validate this certificate is issued by an trusted authority, the CA


      d. B does the validation by decrypt(certificate, CA’s public key) and see if A’s name is on it, because the certificate is encrypted with CA’s private key.

7. Algothrims

   a. RSA — A large size of key should be used to be safe (1024-bit, for example)

   b.Diffie-Hellman: Used for "Secret Key Exchange" only

   c. DSS: Only for digital signature

   d.elliptic-Curve(ECC): It’s to replace RSA, but it’s new and people still doubt it.


Java API Examples


   //Generate a public/private key pair

  KeyPairGenerator keyGen =  KeyPairGenerator.getInstance("RSA");

  SecureRandom random = SecureRandom.getInstance("SHA1PRNG","SUN");

  keyGen.initialize(1024, random);

  KeyPair pair = keyGen.generateKeyPair()

  //Make a Digital Signature

  Signature sig = Signature.getInstance("SHA1withDSA", "SUN");



  byte[] realSig = sig.sign();

  //Verifying a Signature

   PublicKey pub = pair.getPublic();



   boolean verifies = sig.verify(sig);


Network Security Essentials — Notes3. Message Authentication

1. Message Authentication

  a. Msg is not altered

  b. Msg is indeed from the alleged sender

  c. Msg is has not been artifically delayed or replayed

2.Two Techniques of Authentication

   a. Encrypt the whole message before sending

        Msg is authentic <=> encrypted msg can be successfully decrypted

Reversable Encryption/Decryption of the whole msg is slow

   b. Msg + Digest( A function of msg, f(msg) )  

       i. Digest = Encrypt(Msg, Key)   (Digest here is also called MAC)

          Sender sends Msg + Digest

          Receiver regardd Msg as authentic <=>  Encrypt(Msg, Key) = Digest

The encryption algorithm here needs not to be reversable

       ii.Digest = Hash(Msg + SecretValue)

          Sender sends Msg + Digest

          Receiver regardd Msg as authentic <=>  Hash(Msg + SecretValue) = Digest          

3. Hash Functions

   a.SHA-1: 160bits output (Not as safe as it claims. Avoid this one)

   b.SHA-250, SHA-384, SHA-512: safe

   c.MD5: 128bits output (Can be brutely cracked)

   d.Whirpool: 512bit output. safe

Java API examples


   MessageDigest md = MessageDigest.getInstance("SHA-1");

   MessageDigest md = MessageDigest.getInstance("MD5");


        KeyGenerator kg = KeyGenerator.getInstance("HmacMD5");

        SecretKey sk = kg.generateKey();

        Mac mac = Mac.getInstance("HmacMD5");


        byte[] result = mac.doFinal(message.getBytes());

Network Security Essentials — Notes2. Symmetric Ciphers

1. Overview

Symmetric Ciphers use the same key for both decryption and encryption.

  Other terms for symmetric-key encryption are secret-key, single-key, shared-key,  one-key, and private-key encryption.

5 Indredients


  b.Encryption Algorithm

  c.Secret Key


  f.Decryption Algorithm

Algorithms are not kept secret, but keys are

3.Two kinds of Ciphers:

Block Cipher  — Block by block, for example, encrypting a file

Stream Cipher — Element by element (a byte e.g.), for example, encrypting a multi-media stream

4.3 most commonly used Block Ciphers

DES: blockSize=64 bits, keyLength=56 bits

      I.The algorithm is not vulnerable, though studied a lot

     II.The key length of 56bits is so small that it can be cracked by brute force

3DES: 3 executions of DES with seperate 3 keys

      I.The algorithm is also good since it’s the same of DES

     II.The key length is 56bits * 3 = 168 bits. So it’s impossible to crack by brute force

    III.It’s slow

AES: Both secure and fast.

5. How to encrypt blocks broken from a message?

   a. b1 + b2 + … => encrypt(b1) + enrypt(b2) + … —

ECB is not safe because two "ABC"s in the message will generate two copies of ciphertext. Attackers may exploit the regularities in this case.

   b. b1 + b2 + … => encrypt(b1) + encrypt((enrypt(b1) XOR b2)) + …  —

6.Stream Ciphers

RC4 is a commonly used Stream cipher

   And CFB for Stream Ciphers is just like ECB/CBC for Block Ciphers

7. How to deliver keys?

   a.end-to-end delivery is not safe

   b.Permanent key is not safe


KDS Scheme is recommended to distribute keys.

    a. A third party KDS is responsible to deliver a temporary keys used only for a session

    b. These session keys are encrypted themselvez by permanentt keys used between KDS and the end parties.

Java API Examples

   //generate a DES key

    KeyGenerator keygen = KeyGenerator.getInstance("DES");

    SecretKey desKey = keygen.generateKey();


  //Create a cipher

   Cipher c1 = Cipher.getInstance("DES/ECB/PKCS5Padding");

   Cipher c2 = Cipher.getInstance("DESede");


  //Use a PBE key

    String password = "password";

    byte[] salt = "salt1234".getBytes();

    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20);

    PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());

    SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");

    SecretKey passwordKey = kf.generateSecret(keySpec);


Network Security Essentials — Notes1. Introduction

‘Network Security Essentials: Applications and Standards’, by Dr. William Stallings

There is a systematic way of defining security requirements —
X.800 Security Architecture for OSI

1. The architecture defines,

  a. Attack

  b. Service  — detecting and preventing Attacks

  c. Mechanism — implements Service

2. Attack

   a.Passive Attack

      i. Release of message contents

     ii. Traffic Analysis

It’s almost impossible to detect. Normally we just prevent it, by encryption.


   b.Active Attack




     iv.Denial of Service

As for this kind of task, we emphasize on "detection and recovery"

3. Service

    a. Authentication

    b. Access Control

    c. Data Confidentiality

    d. Nonrepudiation

    e. Availability Service

4. We’ll cover Mechanisms in the subsequent articles.


如果某个方法自己负责 流的打开/关闭,那这个方法就可以把IOException包装成RuntimeException再抛出


如果某个方法只不打开流,却要负责流的关闭,那这种写法本身就错了…… 谁打开,谁关闭,是处理IO流的一个准则。

关于网络安全, William Stallings 有两本书

一本是久负盛名的 ‘Cryptography and Network Security’

另一本是 ‘Network Security Essentials: Applications and Standards’

据作者的说法,第二本是从第一本中 "Adapt" 而来。