3

Here are my encryption and decryption methods. I have two databases and I copied the encrypted password from one database to the other. The code was in vb but I converted it into C#.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Security.Cryptography;
 using System.IO;
 namespace AccountSystem.Class{
class ClEncrDecr
{
    private TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider();

    private byte[] TruncateHash(string key, int length)
    {
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        //Hash the Key
        byte[] keyBytes = System.Text.Encoding.Unicode.GetBytes(key);
        byte[] hash = sha1.ComputeHash(keyBytes);

        // truncate or pad the hash
        Array.Resize(ref hash, length);
        return hash;
    }

    public ClEncrDecr()
    {
        string key = "ABCD";
        tripleDESCryptoServiceProvider.Key = TruncateHash(key, tripleDESCryptoServiceProvider.KeySize / 8 );
        tripleDESCryptoServiceProvider.IV = TruncateHash("", tripleDESCryptoServiceProvider.BlockSize / 8 );
    }

    public string EncryptData(string plainText)
    {
        byte[] plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plainText);
        MemoryStream ms = new MemoryStream();
        CryptoStream encStream = new CryptoStream(ms, tripleDESCryptoServiceProvider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
        encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
        encStream.FlushFinalBlock();
        return Convert.ToBase64String(ms.ToArray());
    }

    public string DecryptData(string encryptedtext)
    {
        byte[] encryptedBytes = Convert.FromBase64String(encryptedtext);
        MemoryStream ms = new MemoryStream();
        CryptoStream decStream = new CryptoStream(ms, tripleDESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
        decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
        decStream.FlushFinalBlock();
        return System.Text.Encoding.Unicode.GetString(ms.ToArray());
    }
}
}

Login Code :

MessageBox.Show(crypto.DecryptData(obj.password))

When we call DecryptData(string encryptedtext) it throws an exception saying Invalid length for a Base-64 char array or string. What can I do?

sirdank
  • 3,351
  • 3
  • 25
  • 58
Joy
  • 157
  • 1
  • 3
  • 11
  • The Encryption method would be interesting. **encryptedtext** is probably not a valid Base64. Maybe you have an example value of **encryptedtext**. – Claudio P Feb 11 '15 at 10:09
  • what do u mean ?? i don't understand. – Joy Feb 11 '15 at 10:19
  • The exception tries to tell you, that the parameter **encryptedtext** is not a valid Base64String. Could you give an example value for **encryptedtext**, on which the exception is thrown? This means the output of the Encryption method, if there is one in your application, doesn't generate the correct output (an Base64String, Example: "YWJjZGVmPT0=") – Claudio P Feb 11 '15 at 10:24
  • actually i have 2 database.and password copied from one to another. now i am apply decrypt method. my password is InvertAdmin. but it gives error from this line byte[] encryptedBytes = Convert.FromBase64String(encryptedtext); and encrypt password is :- dfghfgdfgd667878nnvghv now i have a question code is in vb them i converted into c# so encrypt does not depend on application that means password are same but different-2 encrypt text in different application – Joy Feb 11 '15 at 10:41
  • **dfghfgdfgd667878nnvghv** is not a valid Base64String – Claudio P Feb 11 '15 at 10:45
  • how ?? give me region ?? – Joy Feb 11 '15 at 10:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70696/discussion-between-joy-and-claudio-p). – Joy Feb 11 '15 at 10:53

3 Answers3

9

If you have the following encrypted Password:

dfghfgdfgd667878nnvghv

It can't be converted to a byte array from Base64, because it's not a valid Base64String. A valid Base64String would be:

dfghfgdfgd667878nnvghv==

Claudio P
  • 2,133
  • 3
  • 25
  • 45
1

As Claudio mentioned in the comment, your encryptedtext variable is not a base64 encoded string, perhaps it's at least missing padding character(s) at the end.

It's not visible from the example how it's created, but you might want to look for example this SO question: How do I encode and decode a base64 string?

About padding: http://en.wikipedia.org/wiki/Base64#Padding

Community
  • 1
  • 1
Olli
  • 11
  • 2
0

I had this exact issue. Turned out that I had forgot to increase the varchar size of both my columns in the database. Resulted in the values being truncated. Never rule out your own stupidity.

Diven Desu
  • 41
  • 6
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32146497) – Özgür Güzeldereli Jul 06 '22 at 20:12