0

有一个特殊的工作进展,我们加密和解密输入。突然,昨天,有一个我们无法解密的加密字符串。

看来我的密码学知识不足以解决这个问题。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Crypto
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public static class Crypto
{

    public static void Main()
    {
        //String NotWorking_dec = "鈦ꧪ㧘聯ꢮ玗硴廜᭮⸂濅�";

        String enc ="mIAU::__*?";
        //String dec = "砿첩뜞ꦽ嶾蝡泛ɝࠪ塤偏ꍨ";
        Console.WriteLine(decrypt(dec));
        //writeFile(encrypt(enc));
        Console.ReadLine();
    }
    private static string key = "ZlKMpRwoPLmNXEpCLxEa6g==";
    private static string iv = "U5ZB4W4bQqg=";

    private static ICryptoTransform enc;
    private static ICryptoTransform dec;

    static Crypto()
    {
        RC2 rc = System.Security.Cryptography.RC2.Create();
        enc = rc.CreateEncryptor(Convert.FromBase64String(key), Convert.FromBase64String(iv));
        dec = rc.CreateDecryptor(Convert.FromBase64String(key), Convert.FromBase64String(iv));
    }

    public static String encrypt(String value)
    {
        byte[] input = toBytes(value);
        return toString(enc.TransformFinalBlock(input, 0, input.Length));
    }

    public static String decrypt(String value)
    {
        byte[] input = toBytes(value);
        Console.WriteLine(input.Length);
        return toString(dec.TransformFinalBlock(input, 0, input.Length));

    }

    private static void writeFile(String value)
    {
        try
        {
            StreamWriter sw = new StreamWriter("output.tmp", true);
            sw.WriteLine(value);
            sw.Close();
        }
        catch (Exception ex) { }
    }

    private static byte[] toBytes(String value)
    {
        return Encoding.Unicode.GetBytes(value);
    }

    private static String toString(byte[] value)
    {
        return Encoding.Unicode.GetString(value);
    }
}
 }

这一工作进展持续了几个月。您可以使用输入对其进行测试

mIAU::__*?

你得到

砿첩뜞ꦽ嶾泛泛ɝࠪ埙偏ꍨ</p>

解密它,你又得到了

mIAU::__*?

但是加密后的字符串“钛ꧪ㧘联ꢮ玗硴廜᭮⸂濅�”会抛出错误:(当然我们用的是同一个key和iv)

BAD DATA

   bei System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   bei System.Security.Cryptography.Utils._DecryptData(SafeKeyHandle hKey, Byte[] data, Int32 ib, Int32 cb, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode PaddingMode, Boolean fDone)
   bei System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
   bei Crypto.Crypto.decrypt(String value) in c:\Users\Td\Documents\Visual Studio 2013\Projects\Crypto\Crypto\Program.cs:Zeile 56.
   bei Crypto.Crypto.Main() in c:\Users\Td\Documents\Visual Studio 2013\Projects\Crypto\Crypto\Program.cs:Zeile 23.
   bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   bei System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   bei System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
   bei System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
   bei System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   bei System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
   bei System.Activator.CreateInstance(ActivationContext activationContext)
   bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   bei System.Threading.ThreadHelper.ThreadStart()
4

1 回答 1

1
于 2014-05-06T19:13:22.880 回答