Skip to content

Instantly share code, notes, and snippets.

@hamakn
Last active November 19, 2015 07:35
Show Gist options
  • Save hamakn/5cd147809e2023766ea8 to your computer and use it in GitHub Desktop.
Save hamakn/5cd147809e2023766ea8 to your computer and use it in GitHub Desktop.
Crypt by Blowfish CBC / work with PHP using ZeroBytePadding
<?php
require_once 'Crypt/Blowfish.php';
$data = "test text";
$key = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
$iv = "bbbbbbbb";
$blowfish = Crypt_Blowfish::factory('cbc', $key, $iv); # Crypt_Blowfish-1.1.0RC2 only
$encrypted_data = $blowfish->encrypt($data);
echo base64_encode($encrypted_data) . "\n";
# => TQkucQjEyfJSagKaMtCKYg==
?>
use strict;
use warnings;
use Crypt::CBC;
use MIME::Base64;
my $cipher = Crypt::CBC->new(
-cipher => 'Blowfish',
-key => "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
-iv => "bbbbbbbb",
-padding => 'null',
-prepend_iv => 0,
-regenerate_key => 0,
#-keysize => 56,
);
my $text = 'test text';
my $ciphertext = $cipher->encrypt($text);
my $base64text = MIME::Base64::encode($ciphertext);
print "$base64text";
# => TQkucQjEyfJSagKaMtCKYg==
require "base64"
require "openssl"
require "pry"
KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
IV = "bbbbbbbb"
text = "test text"
text += "\x00" * (8 - text.size % 8) # ZeroBytePadding for PHP
cipher = OpenSSL::Cipher::Cipher.new("BF-CBC")
cipher.encrypt
cipher.padding = 0
cipher.key_len = 56
cipher.key = KEY
cipher.iv = IV
puts Base64.encode64(cipher.update(text) + cipher.final)
# => TQkucQjEyfJSagKaMtCKYg==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment