dongyou2714 2015-07-07 13:33 采纳率: 0%
浏览 109

Blowfish CBC加密不解密

I have difficulties decrypting a blowfish encrypted string in a .net environment, that was encrypted by the mcrypt php library.

Here is the script I use to encrypt some data

<?php

function encrypt_blowfish($data, $key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $crypttext = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $data, MCRYPT_MODE_CBC,    $iv);
    echo 'IV:   ' . bin2hex($iv) . "
";
    echo 'DATA: ' . bin2hex($crypttext) ."
" ;
}

$secretKey = 'somekey'; 
$data = 'Hello World this is an encryptiontest!';

encrypt_blowfish($data, $secretKey);

I decided to use the bouncingcastle library since it seemed to be the default choice for encryption and they had a PCL version (which I need). For testing purpose I just copy/pasted the echo'd values into my C# code.

var ivString = "34c33fed0386dda1";
var iv = Hex.Decode (ivString);

var dataString = "ced4ed218d7a1fd228f8c43ca6b83f097648811661d5510678a26953729ceccdf6d78a7695cbfe43";
var data = Hex.Decode (dataString);

var keyString = "somekey";
var key = System.Text.Encoding.UTF8.GetBytes (keyString);

var engine = new BlowfishEngine();
var cipher =new PaddedBufferedBlockCipher(new CbcBlockCipher(engine));

var keyParam = new KeyParameter(key);
cipher.Init (false, keyParam);

var outBytes = new byte[data.Length];
var len = cipher.ProcessBytes (data, 0, data.Length, outBytes, 0);

cipher.DoFinal(outBytes, len);

Console.WriteLine(System.Text.Encoding.UTF8.GetString(outBytes));

When I run this code DoFinal explodes with a "Corrupt padding block" exception. So I read about pcks7 padding which essentially fills the bytes of the original string. I calculated that for my input string and the blowfish cbc algorithm block size of 8, I would need two bytes of padding so I added "22" at the end of the string. This however yielded the same result.

Also, I don't see any point where I can insert the IV into the blowfish decryption. It feels like I am completely lacking/not understanding a vital point here. Any1 any ideas on what goes wrong here? Also if possible I would like to skip on the padding part in my php and simply decrypt with iv/passphrase in c#, is that even possible?

Cheers and thanks

Tom

  • 写回答

2条回答 默认 最新

  • dtup3446 2015-07-07 13:56
    关注

    I ended up using a simpler library which supperts cbc mode in a very simple fashion.

    http://jaryl-lan.blogspot.de/2014/07/openfire-blowfish-encryptiondecryption.html

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)