-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from avinal/avinal/blowfish2
feat: add blowfish2 128 bit implementation
- Loading branch information
Showing
10 changed files
with
968 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,32 @@ | ||
# Blowfish Encryption Algorithm | ||
# Blowfish and Blowfish2 Encryption Algorithm | ||
|
||
<p align=center><a href="https://github.com/avinal/blowfish/actions"><img alt="build" src="https://github.com/avinal/blowfish/workflows/build/badge.svg?branch=main"></a><a href="https://github.com/avinal/blowfish/blob/main/LICENSE"><img src="https://img.shields.io/github/license/avinal/blowfish" alt="license"></a></p> | ||
|
||
|
||
Blowfish is a symmetric block cipher that can be used as a drop-in replacement for DES or IDEA. It takes a variable-length key, from 32 bits to 448 bits, making it ideal for both domestic and exportable use. Blowfish was designed in 1993 by Bruce Schneier as a fast, free alternative to existing encryption algorithms. Since then it has been analyzed considerably, and it is slowly gaining acceptance as a strong encryption algorithm. Blowfish is unpatented and license-free, and is available free for all uses. | ||
|
||
Blowfish 2 was released in 2005. It has exactly the same design but has twice as many S tables and uses 64-bit integers instead of 32-bit integers. It no longer works on 64-bit blocks but on 128-bit blocks like AES. 128-bit block, 64 rounds, key up to 4224 bits. | ||
|
||
## About this project | ||
|
||
This is a C++ implementation of the encryption algorithm. | ||
|
||
## How to use this in your project? | ||
|
||
1. You may fork it and use it like any other source file in your project. You only need [blowfish.hpp](include/blowfish/blowfish.hpp) and [blowfish.cpp](src/blowfish.cpp) files. Just modify the header as per your convienence. | ||
2. If you are using CMake, the work is lot easier. You can add this as a git submodule. It isolates your project from this dependency. | ||
2. If you are using CMake, the work is lot easier. You can add this as a git submodule. It isolates your project from this dependency. | ||
|
||
```bash | ||
# In your project root type these commands | ||
git submodule add https://github.com/avinal/blowfish | ||
# considering this addition is your only change | ||
git commit -m "blowfish submodule added" | ||
git push origin main | ||
``` | ||
|
||
Add this to your CMakeLists.txt as well. | ||
|
||
|
||
## References | ||
|
||
- [Description of a new variable-length key, 64-bit block cipher (Blowfish)](https://link.springer.com/chapter/10.1007/3-540-58108-1_24) | ||
- [The Blowfish Encryption Algorithm](https://www.schneier.com/academic/blowfish/) | ||
- [The Blowfish 2 C Implementation](https://github.com/erwanmilon/blowfish2/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// SPDX-FileCopyrightText: 2024 Avinal Kumar avinal.xlvii@gmail.com | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Original Blowfish 2 Algorithm copyright: | ||
// SPDX-FileCopyrightText: 2005 Alexander Pukall | ||
|
||
#include <algorithm> | ||
#include <array> | ||
#include <cstdint> | ||
#include <string> | ||
|
||
#define MAXKEYBYTES 56 // 4224 bits max | ||
#define N 64 | ||
|
||
#if !defined(BLOWFISH_BLOWFISH2_H_) | ||
#define BLOWFISH_BLOWFISH2_H_ | ||
|
||
class Blowfish2 { | ||
private: | ||
std::array<uint64_t, N + 2> PArray; | ||
std::array<std::array<uint64_t, 256>, 8> Sboxes; | ||
uint64_t F(uint64_t x); | ||
|
||
public: | ||
Blowfish2() {} | ||
Blowfish2(std::string const &key); | ||
Blowfish2(Blowfish2 const &) = delete; | ||
|
||
void initialize(std::string const &key); | ||
|
||
void encrypt(uint64_t &xl, uint64_t &xr); | ||
void decrypt(uint64_t &xl, uint64_t &xr); | ||
}; | ||
|
||
#endif // BLOWFISH_BLOWFISH2_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.