# Cryptography fundamentals for hackers Complete Guide by Blackhat Pakistan 2023

**What is cryptography?**

Cryptography literally means “secret writing”. It is the science of protecting sensitive data from being read (or modified) by unauthorized parties, such as eavesdropping, eavesdropping, and so on.

While the old encryption algorithms have been largely broken, modern cryptography is based on strong mathematical principles and has been subjected to intensive study by professional cryptographers. As a result, when used properly, cryptography can be an effective defense against privacy. However, even a small flaw in design or implementation can provide an ethical hacker with the necessary opportunity to break the encryption and read the encrypted data.

**Basics of cryptography**

Before diving into the details of how cryptography works, it’s important to understand the terminology. Technically, cryptography (the creation of codes) is a subfield of cryptology (the science of codes), but the two terms are often used interchangeably. Another subfield of cryptology, cryptanalysis, focuses on attempts to break encryption algorithms.

Also read:Ethical hacking: Buffer overflow by Blackhat Pakistan 2023

Any good encryption algorithm has at least three main components: plaintext, ciphertext, and secret key.

Plaintext is a message that will be protected by an encryption algorithm. While this message is often a secret the hacker is trying to steal, it can also be under the hacker’s control. Most encryption algorithms are protected against known plaintext attacks, where an attacker knows the plaintext and tries to learn the secret key.

Ciphertext is the result of encrypting plaintext with a secret key. The ciphertext should be essentially indistinguishable from a random number. This is achieved by creating encryption algorithms with high confusion (each bit of the ciphertext is dependent on several bits of the secret key) and diffusion (ensuring that flipping one bit of the plaintext flips on average half the bits of the ciphertext).

The secret key is information that should only be known to the authorized user of the protected data. It is provided as an argument to the encryption algorithm (along with the plaintext) to create the ciphertext.

According to the Kerckhoff principle, the secret key should be the only secret part of the encryption algorithm. Relying on secrecy (eg, secrecy of the encryption algorithm or parts of it) is bad cryptography. Relying on obscurity security is one of the flaws that made the Enigma cipher breakable.

**Types of cryptography**

There are several different ways that cryptographic algorithms can be classified. Some of the main ones include asymmetric versus symmetric and block versus stream. Knowing these classifications can be useful for ethical hacking, as different types of cryptography, if misused, can be vulnerable to attack.

Symmetrical and asymmetrical

One of the main ways to distinguish different encryption algorithms is based on whether they use symmetric or asymmetric encryption keys. A symmetric encryption algorithm uses the same secret key for both encryption and decryption, while an asymmetric algorithm uses two related keys: a private key and a public key.

Symmetric encryption algorithms are generally better for bulk data encryption, but they have one major drawback: both parties need to have a copy of the same shared secret key. A good cryptographic implementation will use asymmetric cryptography to set up a channel to share a symmetric encryption key for bulk encryption. However, if the symmetric key is sent in plain text or embedded in software, capturing it can allow an ethical hacker to decrypt and read all protected data.

Asymmetric encryption uses a related public and private key. In asymmetric cryptography (also called public key cryptography), the main vulnerability of the protocol is to quantum computers, which can easily break it.

However, public key cryptography can also face the authentication problem. A user’s public key can be used to encrypt a message for them or to verify a digital signature generated using their associated private key. This assumes that the public key held and used by the software is correct. If the authenticity of the public key is not properly verified, replacing the public key by an ethical hacker may allow them to decrypt messages and generate digital signatures accepted by the software.

**Block and stream**

Another major difference between the types of cryptographic algorithms is whether they are block or stream ciphers. As the name suggests, a block cipher encrypts and decrypts blocks of data with a fixed size. On the other hand, a stream cipher generates a stream of pseudo-random output bits that are exclusively ordered (XORed) with the plaintext bits to form the ciphertext.

The main problem with block encryption algorithms is that they do not have a built-in mechanism for encrypting data longer than the size of a single block. To remedy this, block cipher modes of operation were developed to define a protocol for encrypting multi-block plaintexts. However, some of this information leaks about encrypted data that can be exploited by an ethical hacker.

Stream ciphers are designed to encrypt plaintexts of any length because they encrypt in chunks. However, this bit-level encryption can also make the system vulnerable to bit flipping errors. If the application does not test the integrity of the encrypted data (via MAC or checksum), a hacker can reverse bits without detection. The most famous stream cipher (RC4) also has vulnerabilities that can leak secret key data if misused.

**Cryptography for Ethical Hacking**

The widespread use of modern cryptography is good for privacy and security, but it also complicates the ethical hacker’s job. Encrypting data can hide other flaws in the system if a hacker cannot properly analyze the data at rest or in transit to identify it.

During an ethical hacking exercise, it is important to check the use of cryptography for flaws that could allow a hacker to break the encryption. It may also be a good idea to request access to unencrypted data to enable analysis of the system for other errors hidden by encryption.

**Sources**

- Padding oracles and the decline of CBC-mode cipher suites, The Cloudflare Blog
- Stream Cipher Attack: Bit-Flipping Attack, Total Web Security
- CVE-2015-2808, CVE