Cryptography is the practice and study of techniques to secure communications in the presence of third parties. Historically, cryptography was synonymous with encryption. Its goal was to keep messages private. In modern times, cryptography includes other responsibilities:
密碼學(cryptography)是研究並實踐在有第三方存在的情況下保護通訊安全的技術。歷史上,密碼學等同於加密,其目標是保持訊息隱密。在現代,密碼學還包含其他責任:
- Confidentiality: Ensuring that only authorized parties can read a message機密性:確保只有經授權的一方才能讀取訊息
- Data integrity: Ensuring that any changes to data in transit will be detected and rejected資料完整性:確保傳輸中資料的任何變更都會被偵測並拒絕
- Origin authentication: Ensuring that any messages received were actually sent from the perceived origin來源身分驗證:確保收到的任何訊息確實是由聲稱的來源所發送
- Non-repudiation:Ensuring that the original source of a secured message cannot deny having produced the message不可否認性:確保經保護訊息的原始來源無法否認曾產生該訊息
Hashing Algorithms
雜湊演算法
Hashing is a mechanism that is used for data integrity assurance. Hashes confirm that the message is authentic without transmitting the message itself. Hashing functions are designed so that you cannot revert hashed data into the original message.
雜湊(hashing)是一種用於確保資料完整性的機制。雜湊可以在不傳送訊息本身的情況下確認訊息是真實的。雜湊函式的設計使得你無法將雜湊後的資料還原成原始訊息。
Hashing is based on a one-way mathematical function: functions that are relatively easy to compute, but significantly difficult to reverse. Grinding coffee is a good example of a one-way function: it is easy to grind coffee beans, but it is almost impossible to put back together all the tiny pieces to rebuild the original beans.
雜湊是以單向數學函式為基礎:這類函式相對容易計算,卻極難反向推算。磨咖啡豆是單向函式的一個好例子:磨碎咖啡豆很容易,但要把所有細小碎片重新組合成原本的咖啡豆幾乎是不可能的。
Data of an arbitrary length is input into the hash function, and the result of the hash function is the fixed-length hash, which is known as the "digest" or "fingerprint." If the same data is passed through a hash algorithm at different times, the output is identical. Any small modification to the data produces a drastically different output. For example, flipping one bit in the data might produce output in which half the bits are flipped. This characteristic is often referred to as the avalanche effect, because one bit flipped and caused an avalanche of bits to flip. Data is deemed authentic if running the data through the hash algorithm produces the expected result, also called the fingerprint.
任意長度的資料輸入雜湊函式後,函式的輸出結果是固定長度的雜湊值,稱為「摘要(digest)」或「指紋(fingerprint)」。若相同的資料在不同時間經過同一個雜湊演算法運算,輸出結果會完全相同。資料只要有微小的修改,就會產生截然不同的輸出。例如,翻轉資料中的一個位元,可能導致輸出中一半的位元都被翻轉。這種特性通常稱為雪崩效應(avalanche effect),因為一個位元的翻轉引發了一連串位元的翻轉。若將資料經過雜湊演算法運算後得到預期結果(即指紋),即可判定該資料是真實的。
The following output illustrates how adding a period to the end of the sentence results in a different digest due to the avalanche effect.
以下輸出說明了因雪崩效應,在句尾加上一個句點會導致完全不同的摘要。
SHA-256("Changing the way we work, live, play and learn")=
d8a04ed6a21ff9c71020490a864aa09ce8afb81198d5ee69ff8d92f69035dfb0
SHA-256("Changing the way we work, live, play and learn.")=
9d85598ccf40898b5f0b5743edf71a0139d7bc502c985f6cbcbe1f8cc0efbdd9Since hash algorithms produce a fixed-length output, there are a finite number of possible outputs. It is possible for two different inputs to produce an identical output. They are referred to as hash collisions.
由於雜湊演算法產生固定長度的輸出,可能的輸出數量是有限的。因此有可能出現兩個不同的輸入產生相同輸出的情況,這稱為雜湊碰撞(hash collision)。
Hashing is similar to the calculation of cyclic redundancy check (CRC) checksums, but it is much stronger cryptographically. CRCs were designed to detect randomly occurring errors in digital data, while hash algorithms were designed to assure data integrity even when data modifications are intentional with the objective to pass fraudulent data as authentic. One primary distinction is the size of the digest produced. CRC checksums are relatively small, often 32 bits. Commonly used hash algorithms produce digests in the range of 128 to 512 bits in length. It is relatively easier for an attacker to find two inputs with identical 32-bit checksum values than it is to find two inputs with identical digests of 128 to 512 bits in length.
雜湊與循環冗餘校驗(CRC)總和的計算相似,但在密碼學上強度更高。CRC 的設計目的是偵測數位資料中隨機發生的錯誤,而雜湊演算法的設計目的則是即使資料被刻意修改,也要確保資料完整性,以防止偽造資料被冒充為真實資料。兩者的主要差異之一是產生摘要的大小。CRC 總和相對較小,通常為 32 位元。常用的雜湊演算法產生的摘要長度介於 128 到 512 位元之間。攻擊者要找到兩個具有相同 32 位元總和值的輸入,遠比找到兩個具有相同 128 到 512 位元摘要的輸入來得容易。
The following figure illustrates how hashing is performed.
下圖說明了雜湊的執行方式。
The following figure shows one use of hash algorithms to provide data integrity. Organizations that offer software for download often publish hash digests on the download page that can be used to verify data integrity of the downloaded software.
下圖顯示了雜湊演算法用於提供資料完整性的一種應用。提供軟體下載的組織通常會在下載頁面上發布雜湊摘要,供使用者驗證下載軟體的資料完整性。
Examples of hash algorithms include:
雜湊演算法的範例包括:
- Deprecated:已淘汰:
- MD5: Produces a 128-bit hash value that is typically represented as a sequence of 32-hex digits. However, MD5 is considered insecure and should be avoided.MD5:產生 128 位元的雜湊值,通常以 32 個十六進位數字表示。然而,MD5 已被視為不安全,應避免使用。
- SHA-1:Produces a 160-bit hash value that is typically represented as a sequence of 40-hex digits. It is a legacy algorithm and thus is adequately secure. Both MD5 and SHA-1 are vulnerable to hash collisions.SHA-1:產生 160 位元的雜湊值,通常以 40 個十六進位數字表示。它是一種舊式演算法,因此安全性已不足。MD5 與 SHA-1 都容易受到雜湊碰撞攻擊。
- Next-generation (recommended):新一代(建議使用):
- SHA-2: Includes significant changes from its predecessor SHA-1, and is the recommended hash algorithm today. The SHA-2 family consists of multiple hash functions with different bit values. The larger the better, and more bits equal better security.SHA-2:相較於前代 SHA-1 有重大改進,是目前建議使用的雜湊演算法。SHA-2 系列包含多種不同位元長度的雜湊函式,位元數愈大,安全性愈高。
- SHA-256: Produces a 256-bit hash value that is typically represented as a sequence of 64-hex digits.SHA-256:產生 256 位元的雜湊值,通常以 64 個十六進位數字表示。
- SHA-384: Produces a 384-bit hash value that is typically represented as a sequence of 96-hex digits.SHA-384:產生 384 位元的雜湊值,通常以 96 個十六進位數字表示。
- SHA-512: Produces a 512-bit hash value that is typically represented as a sequence of 128-hex digits.SHA-512:產生 512 位元的雜湊值,通常以 128 個十六進位數字表示。
Encryption
加密
A cipher is an algorithm for performing encryption and decryption. Ciphers are a series of well-defined steps that you can follow as a procedure.
加密演算法(cipher)是用於執行加密與解密的演算法。加密演算法是一系列定義明確、可作為程序遵循的步驟。
Encryption is the process of disguising a message in such a way as to hide its original contents. With encryption, the plaintext readable message is converted to ciphertext, which is the unreadable, "disguised" message. Decryption reverses this process. Encryption is used to guarantee confidentiality so that only authorized entities can read the original message.
加密(encryption)是一種將訊息偽裝以隱藏其原始內容的程序。透過加密,可讀的明文訊息會被轉換成無法閱讀、經過「偽裝」的密文;解密則會反轉此程序。加密用來確保機密性,使只有經授權的實體才能讀取原始訊息。
Modern encryption relies on public algorithms that are cryptographically strong using secret keys. It is much easier to change keys than it is to change algorithms. In fact, most cryptographic systems dynamically generate new keys over time, limiting the amount of data that may be compromised with the loss of a single key.
現代加密依賴使用密鑰、在密碼學上強度高的公開演算法。變更密鑰遠比變更演算法容易許多。事實上,大多數密碼系統會隨時間動態產生新的密鑰,藉此限制單一密鑰外洩時可能危及的資料量。
Encryption can provide confidentiality at different network layers, such as the following:
加密可以在不同的網路層提供機密性,例如:
- Encrypt application layer data, such as encrypting email messages with Pretty Good Privacy (PGP).使用如 Pretty Good Privacy(PGP)等方式對應用層資料進行加密,例如加密電子郵件訊息。
- Encrypt session layer data using a protocol such as Secure Sockets Layer (SSL) or Transport Layer Security (TLS). Both SSL and TLS are considered to be operating at the session layer and higher in the Open Systems Interconnection (OSI) reference model.使用如安全通訊端層(SSL)或傳輸層安全性(TLS)等協定對會議層資料進行加密。SSL 與 TLS 皆被視為在開放式系統互連(OSI)參考模型中運作於會議層及其以上層級。
- Encrypt network layer data using protocols such as those provided in the IP Security (IPsec) protocol suite.使用如 IP 安全性(IPsec)協定套組所提供的協定,對網路層資料進行加密。
- Encrypt data link layer using MAC Security (MACsec) (IEEE 802.1AE) or proprietary link-encrypting devices.使用 MAC 安全性(MACsec,IEEE 802.1AE)或專屬的鏈路加密裝置對資料鏈結層進行加密。
Encryption Algorithm Features
加密演算法的特性
A good cryptographic algorithm is designed in such a way that it resists common cryptographic attacks. The best way to break data that is protected by the algorithm is to try to decrypt the data using all possible keys. The amount of time needed by such an attack depends on the number of possible keys, but the time is generally very long. With appropriately long keys, such attacks are usually considered unfeasible.
良好的密碼演算法設計上要能抵禦常見的密碼攻擊。破解受該演算法保護資料的最佳方法,是嘗試以所有可能的密鑰進行解密。這類攻擊所需的時間取決於可能密鑰的數量,但通常非常漫長。只要密鑰長度足夠,這類攻擊通常會被視為不可行。
Variable key lengths and scalability are also desirable attributes of a good encryption algorithm. The longer the encryption key is, the longer it takes an attacker to break it. For example, a 16-bit key means that there are 65,536 possible keys, but a 56-bit key means that there are around 72,000,000,000,000,000 possible keys. Scalability provides flexible key length and allows you to select the strength and speed of encryption that you need.
可變密鑰長度與可擴充性也是良好加密演算法的理想特性。加密密鑰愈長,攻擊者破解所需的時間就愈長。例如,16 位元的密鑰意味著有 65,536 種可能的密鑰,而 56 位元的密鑰則意味著約有 72,000,000,000,000,000 種可能的密鑰。可擴充性提供彈性的密鑰長度,讓你可以選擇所需的加密強度與速度。
Changing only a few bits of the plaintext message causes its ciphertext to change completely, which is known as an avalanche effect. The avalanche effect is a desired feature of an encryption algorithm, because it allows very similar messages to be sent over an untrusted medium, with the encrypted (ciphertext) messages being completely different.
只變更明文訊息中的少數位元,就會導致其密文完全改變,這稱為雪崩效應。雪崩效應是加密演算法期望具備的特性,因為它讓非常相似的訊息可以在不受信任的媒介上傳送,而加密後的(密文)訊息卻完全不同。
You must carefully consider export and import restrictions when you use encryption internationally. Some countries do not allow the export of encryption algorithms, or they allow only the export of those algorithms with shorter keys. Some countries impose import restrictions on cryptographic algorithms.
在國際間使用加密時,必須謹慎考量出口與進口限制。有些國家不允許出口加密演算法,或僅允許出口密鑰較短的演算法。有些國家則對密碼演算法設有進口限制。
Encryption Algorithms and Keys
加密演算法與密鑰
A key is a required parameter for encryption algorithms. There are two classes of encryption algorithms, which differ in their use of keys:
密鑰是加密演算法所需的參數。加密演算法依密鑰使用方式分為兩類:
- Symmetric encryption algorithm: Uses the same key to encrypt and decrypt data對稱式加密演算法:使用相同的密鑰進行加密與解密
- Asymmetric encryption algorithm: Uses different keys to encrypt and decrypt data非對稱式加密演算法:使用不同的密鑰進行加密與解密
Symmetric Encryption Algorithms
對稱式加密演算法
Symmetric encryption algorithms use the same key for encryption and decryption. Therefore, the sender and the receiver must share the same secret key before communicating securely. The security of a symmetric algorithm rests in the secrecy of the shared key; by obtaining the key, anyone can encrypt and decrypt messages. Symmetric encryption is often called secret-key encryption. Symmetric encryption is the more traditional form of cryptography. The typical key-length range of symmetric encryption algorithms is 40 to 256 bits.
對稱式加密演算法對加密與解密使用相同的密鑰。因此,發送方與接收方必須在安全通訊之前先共享相同的密鑰。對稱演算法的安全性取決於共享密鑰的保密性;只要取得密鑰,任何人都能加密與解密訊息。對稱式加密通常也稱為密鑰加密(secret-key encryption),是較傳統的密碼學形式。對稱式加密演算法典型的密鑰長度範圍為 40 到 256 位元。
Because symmetric algorithms are usually quite fast, they are often used for wire-speed encryption in data networks. Symmetric algorithms are based on simple mathematical operations and can easily be accelerated by hardware.
由於對稱演算法通常速度很快,因此常用於資料網路中線速加密。對稱演算法基於簡單的數學運算,也容易透過硬體加速。
Key management can be a challenge, because the communicating parties must obtain a common secret key before any encryption can occur. Therefore, the security of any cryptographic system depends greatly on the security of the key management methods.
密鑰管理可能是一項挑戰,因為通訊雙方必須在加密開始前先取得共同的密鑰。因此,任何密碼系統的安全性都極度依賴密鑰管理方法的安全性。
Symmetric algorithms are frequently used for encryption services, with additional key management algorithms providing secure key exchange. They are used for bulk encryption when data privacy is required, such as to protect a VPN. The reason we use symmetrical encryption algorithms for most of the data in VPNs is because it is much faster to use a symmetrical algorithm and takes less CPU than it would for an asymmetrical algorithm.
對稱演算法常用於加密服務,並搭配額外的密鑰管理演算法提供安全的密鑰交換。它們用於需要資料隱密性的大量加密,例如保護 VPN。之所以在 VPN 的大部分資料中使用對稱加密演算法,是因為使用對稱演算法速度快得多,且所需的 CPU 資源比非對稱演算法少。
Some examples of where the symmetric encryption is used:
以下是使用對稱式加密的一些範例:
- Payment applications: Where the data needs to be protected to prevent identity theft or fraudulent charges, such as card transactions.支付應用程式:需要保護資料以防止身分盜用或詐欺性扣款,例如卡片交易。
- Validating information:To confirm that the sender of a message is who they claim to be. For example, if a device A receives an encrypted message, and if that device can decrypt it to produce a valid message, then this validates that it was sent by a device B. Knowing only the valid key, the sender was able to encrypt the message, and at the receiver end, the same key was used to decrypt the message in symmetric encryption algorithms, which validates the sender.驗證資訊:確認訊息發送方確實是其所聲稱的身分。例如,若裝置 A 收到一則加密訊息,並且能夠解密產生有效訊息,即可證明該訊息是由裝置 B 發送。在對稱式加密演算法中,只有知道有效密鑰的一方才能加密訊息,而在接收端則使用相同的密鑰解密訊息,藉此驗證發送方的身分。
- Random Number Generation:In essence, the data that you encrypt tends to have strong random characteristics.亂數產生:基本上,你所加密的資料往往具有強烈的隨機特性。
Asymmetric Encryption Algorithms
非對稱式加密演算法
Asymmetric algorithms use a pair of keys for encryption and decryption. The paired keys are intimately related and are generated together. Most commonly, an entity with a key pair will share one of the keys (the public key) and it will keep the other key in complete secrecy (the private key). The private key cannot, in any reasonable amount of time, be calculated from the public key. Data that is encrypted with the private key requires the public key to decrypt. Vice versa, data that is encrypted with the public key requires the private key to decrypt. Asymmetric encryption is also known as public key encryption.
非對稱演算法使用一對密鑰進行加密與解密。這一對密鑰彼此密切相關,且是一同產生的。最常見的做法是,擁有金鑰對的實體會公開其中一把密鑰(公鑰),並將另一把密鑰(私鑰)完全保密。在合理的時間內,無法從公鑰推算出私鑰。以私鑰加密的資料需要用公鑰才能解密;反之,以公鑰加密的資料需要用私鑰才能解密。非對稱式加密也稱為公開金鑰加密(public key encryption)。
The typical key length range for asymmetric algorithms is 512 to 4096 bits. You cannot directly compare the key length of asymmetric and symmetric algorithms, because the underlying design of the two algorithm families differs greatly.
非對稱演算法典型的密鑰長度範圍為 512 到 4096 位元。你不能直接比較非對稱與對稱演算法的密鑰長度,因為這兩類演算法在底層設計上有很大差異。
Asymmetric algorithms are substantially slower than symmetric algorithms. Their design is based on computational problems, such as factoring extremely large numbers or computing discrete logarithms of extremely large numbers. Because they lack speed, asymmetric algorithms are typically used in low-volume cryptographic mechanisms, such as digital signatures and key exchange. However, the key management of asymmetric algorithms tends to be simpler than symmetric algorithms, because usually one of the two encryption or decryption keys can be made public.
非對稱演算法比對稱演算法慢得多。它們的設計基於計算難題,例如對極大數字進行因式分解或計算極大數字的離散對數。由於速度較慢,非對稱演算法通常用於低流量的密碼機制,例如數位簽章與密鑰交換。然而,非對稱演算法的密鑰管理通常比對稱演算法簡單,因為加密或解密密鑰其中之一通常可以公開。
Examples of asymmetric cryptographic algorithms include Rivest, Shamir, and Adleman (RSA), Digital Signature Algorithm (DSA), ElGamal, and elliptic curve algorithms.
非對稱式密碼演算法的範例包括 Rivest、Shamir 與 Adleman(RSA)演算法、數位簽章演算法(DSA)、ElGamal,以及橢圓曲線演算法。
Usually asymmetric algorithms, such as RSA and DSA, are used for digital signatures.
通常像 RSA 與 DSA 這類非對稱演算法會用於數位簽章。
For example, a customer sends transaction instructions via an email to a stockbroker, and the transaction turns out badly for the customer. It is conceivable that the customer could claim never to have sent the transaction order or that someone forged the email. The brokerage could protect itself by requiring the use of digital signatures before accepting instructions via email.
例如,一位客戶透過電子郵件向股票經紀人發出交易指示,而該筆交易結果對客戶不利。這時客戶有可能聲稱自己從未發送過該交易指令,或聲稱有人偽造了該封電子郵件。券商可以要求在透過電子郵件接受指示前必須使用數位簽章,藉此保護自身權益。
Handwritten signatures have long been used as a proof of authorship of, or at least agreement with, the contents of a document. Digital signatures can provide the same functionality as handwritten signatures, and much more.
手寫簽名長久以來一直被用來證明文件內容的作者身分,或至少表示同意文件內容。數位簽章可以提供與手寫簽名相同的功能,甚至更多。
The idea of encrypting a file with your private key is a step toward digital signatures. Anyone who decrypts the file with your public key knows that you were the one who encrypted it. But, since asymmetric encryption is computationally expensive, this is not optimal. Digital signatures leave the original data unencrypted. It does not require expensive decryption to simply read the signed documents. In contrast, digital signatures use a hash algorithm to produce a much smaller fingerprint of the original data. This fingerprint is then encrypted with the signer’s private key. The document and the signature are delivered together. The digital signature is validated by taking the document and running it through the hash algorithm to produce its fingerprint. The signature is then decrypted with the sender’s public key. If the decrypted signature and the computed hash match, then the document is identical to what was originally signed by the signer.
用你的私鑰加密檔案的做法,是邁向數位簽章的第一步。任何以你的公鑰解密該檔案的人都能確認是你加密了它。但由於非對稱加密的運算成本很高,這並非最佳做法。數位簽章使原始資料保持未加密狀態,因此不需要昂貴的解密程序即可直接讀取已簽署的文件。相對地,數位簽章使用雜湊演算法對原始資料產生小得多的指紋,再以簽署者的私鑰對該指紋進行加密。文件與簽章會一併傳送。驗證數位簽章的方式是將文件經過雜湊演算法運算以產生其指紋,再以發送方的公鑰解密簽章。若解密後的簽章與計算出的雜湊值相符,即表示該文件與簽署者原始簽署的文件完全相同。




