简体   繁体   中英

C# 3DES Encryption+Decryption (ECB+No padding+no IV)

I need to encrypt and decrypt a string in C#. I have managed to write the right decoder for a string which I receive from a Java service - 3DES, DESede/ECB/NoPadding.

Now, I am having some a time encoding a string accordingly. Below is the decryptor (which is perfect and not need any changes).

attached also the encryptor which should encrypt a string that will be decrypted with the attached decryptor.

decryptor:

public static string Decryptor240815B(string Message) /* Working */
{
    string cipher = Message.Replace(" ", "+");
    byte[] keyBytes;
    string cipherString = FromHexString(cipher);
    byte[] cipherBytes = Convert.FromBase64String(cipherString);

    keyBytes = UTF8Encoding.UTF8.GetBytes(seed);

    var tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = keyBytes;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.None;

    ICryptoTransform transformation = tdes.CreateDecryptor();
    byte[] decryptedBytes = transformation.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
    tdes.Clear();

    string response = UTF8Encoding.UTF8.GetString(decryptedBytes);
    return response;
}

public static string FromHexString(string hexString)
{
    var bytes = new byte[hexString.Length / 2];
    for (var i = 0; i < bytes.Length; i++)
    {
        bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
    }

    return Encoding.UTF8.GetString(bytes); 
}

encryptor (need change):

public static string Encrypt030915(string message)
{
    byte[] keyBytes = UTF8Encoding.UTF8.GetBytes(seed);
    //string hexedMSG = StringToHexString(message);
    byte[] textBytes = UTF8Encoding.UTF8.GetBytes(message);

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    ICryptoTransform cTransform = tdes.CreateEncryptor();

    tdes.Key = keyBytes;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.None;
    byte[] resultArray = cTransform.TransformFinalBlock(textBytes, 0, textBytes.Length);

    tdes.Clear();

    string base64 = Convert.ToBase64String(resultArray);
    string retVal = FromBase64ToHEX(base64);
    return retVal;
    //byte[] ba = Encoding.UTF8.GetBytes(base64);
//    return ToHexString(resultArray); 
    //return ByteArrayToString030915(ba); 

}

private static string FromBase64ToHEX(string base64)
{
    char[] c = new char[base64.Length * 2];
    byte b;
    for (int i = 0; i < base64.Length; ++i)
    {
        b = ((byte)(base64[i] >> 4));
        c[i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30);
        b = ((byte)(base64[i] & 0xF));
        c[i * 2 + 1] = (char)(b > 9 ? b + 0x37 : b + 0x30);
    }
    return new string(c);
}

You need to move the

ICryptoTransform transformation = tdes.CreateDecryptor();

line below setting the parameters, as CreateDecryptor uses the parameters (like Key !) as they are set on the TripleDESCryptoServiceProvider at the moment. But in your original code, you create the decryptor and only after that set the decryption parameters, which do not get used at all (and the decryptor gets to use a random key instead). Replace the relevant lines with:

var tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyBytes;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.None;
ICryptoTransform transformation = tdes.CreateDecryptor();

and it will work.

However, note that your code is 1) unnecessarily complicated, 2) completely insecure .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM