【问题标题】:Decrypt returning junk data解密返回的垃圾数据
【发布时间】:2014-05-23 05:02:45
【问题描述】:

我的加密算法运行良好,并且搁置了几个月。今天我需要再次使用它,但它以某种方式坏了。我已经戳了一段时间,无法发现问题所在。没有错误,它只是返回垃圾数据。

设置:一个 PHP 脚本(在生产环境中工作了很长时间)使用以下方法加密一些字符串:

function hexstr($hexstr) 
{
  // return pack('H*', $hexstr); also works but it's much harder to understand.
  $return = '';
  for ($i = 0; $i < strlen($hexstr); $i+=2) {
    $return .= chr(hexdec($hexstr[$i] . $hexstr[$i+1]));
  }
  return $return;
}

function encrypt($str, $key)
{
    $key = hexstr($key);
    $size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($size, MCRYPT_RAND);
    return $iv . mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_CBC,$iv);
}

如果我在 php 端解密它,它工作正常。

那么字符串是base64编码的:

$encoded = base64_encode($encrypted);

最后,C# 程序得到它并执行以下操作:

....
byte[] decoded = Convert.FromBase64String(myWebText);
Decrypt(decoded)
.....


public static byte[] Decrypt(byte[] p)
{
    RijndaelManaged aes128 = new RijndaelManaged();
    aes128.KeySize = 128;
    //aes128.BlockSize = 
    aes128.Padding = PaddingMode.Zeros;
    aes128.Mode = CipherMode.CBC;
    aes128.Key = StringToByteArray("SOMEHEXSTRING");
    //pull the iv off the front of the byte array
    if (p.Length <= 16)
    {
        Utils.ReportError("byte array too short");
        return null;
    }
    byte[] iv = new byte[16];
    Array.Copy(p, 0, iv, 0, 16);
    aes128.IV = iv;
    ICryptoTransform transform = aes128.CreateDecryptor();
    byte[] result = transform.TransformFinalBlock(p, 16, p.Length - 16);

    Debug.Log("If this encrypted stuff was text it would be:"+System.Text.Encoding.UTF8.GetString(result));
    return result;
}


public static byte[] StringToByteArray(string hex)
{
    if (hex.Length % 2 == 1)
        throw new Exception("The binary key cannot have an odd number of digits");

    byte[] arr = new byte[hex.Length >> 1];

    for (int i = 0; i < hex.Length >> 1; ++i)
    {
        arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
    }

    return arr;
}

有谁知道这可能不起作用?它必须非常接近,因为我确信它在某一时刻有效。 更新:我对 C# encode\decode 和 php encode decode 进行了二进制比较。 php 编码的 Hello World 和 c# Hello world 在使用相同的 IV 时是不一样的。这可能吗,或者这是否表明他们没有以某种方式使用相同的配置。

【问题讨论】:

  • 更新为包含base64解码代码
  • 您能发布一个示例加密字符串和密钥吗?我通常不使用 php,但我可以尝试解密一个已经加密的字符串,看看我是否能看到任何潜在的问题。
  • 好的,这里有一些密钥和 Hello World 编码(在本例中也加密了它的 base64 编码之后)"Data:ewp/sWeooBQd7xUCNxtmwTsTy5KdZkOglkPo7PPPuY0=" 密钥是 "BAAAAAAAAAAAAAAAAAAAAAAAAAAAB" 不要忘记 IV前导 16 个字节也在里面
  • 你也可以发布你的 StringToByteArray 方法吗?
  • 主要描述中添加的功能

标签: c# encryption


【解决方案1】:

原来我已将键更改为非大写字母,而我的 GetHexVal 函数显示在那里只使用大写字母......哎哟

【讨论】:

    【解决方案2】:

    IDEA:解密密钥已更改。

    当它发生时非常讨厌。 您是否使用带有解密密钥的配置文件? 如果不是,使用的解密密钥是什么? 变了吗?

    如果使用 localApp.configROOT.config 并且密钥已更改,您可以搜索所有备份以获取密钥。

     <machineKey decryptionKey="SomeHexValueHere" validationKey="SomeveryLongHexvalueHere" />
    

    但如果您的 PHP 版本正常工作,则密钥必须是“已知的”

    【讨论】:

    • 是的。我有钥匙。我尝试了在这里找到的另一个解决方案。 stackoverflow.com/questions/165808/…。我可以在 c# 端加密和解密,但是从 php 到 c# 时我遇到了同样的问题。只是垃圾字符作为输出。我尝试添加填充并使用 PKCS7,但我得到:CryptographicException: Bad PKCS7 padding。长度无效 197。我正在处理 16 个字节的加密内容,所以我不确定该数字来自哪里。
    猜你喜欢
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 2015-06-27
    • 2016-07-12
    相关资源
    最近更新 更多