Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple string encryption/decryption with a small resulting string [closed]

Tags:

c#

encryption

I need to encrypt a string and then be able to decrypt it again.

I implemented the solution here and it works well, but the resulting string is not suitable as it needs to be simple and short enough for a user to use.

I am encrypting incrementing database ID's (from 1) and there won't be more than 500. Ideally I'd like the encrypted string to be not more than 6 characters in length.

Any ideas appreciated..

edit: It's a lengthy form which the user can resume at a later date with this generated string

like image 362
notAnonymousAnymore Avatar asked Nov 08 '12 19:11

notAnonymousAnymore


People also ask

How do I decrypt an encrypted string?

Given encrypted string str, the task is to decrypt the given string when the encryption rules are as follows: Start with the first character of the original string. In every odd step, append the next character to it. In every even step, prepend the next character to the encrypted string so far.

When you encrypt a string's you start with an initially empty resulting string R and append characters to it as follows?

When you encrypt a string S, you start with an initially-empty resulting string R and append characters to it as follows: Append the middle character of S (if S has even length, then we define the middle character as the left-most of the two central characters)

Which of the following string is used to encrypt the data in the string in C #?

Symmetric key is a string which is used to encrypt the data and with the same string, we can decrypt the data, which means a single string is required for encryption and decryption.


2 Answers

You can use AES in CTR mode without any padding. In this mode there is a counter that is encrypted and then the result is xor'd with your plaintext which is the number. The result should be small and you will get the encryption of AES which will be better than any substitution cipher you use (which you could probably break by hand). You will have to get the BouncyCastle crypto library however as the Microsoft implementation of Rijndael does not have CTR as an available mode. Below is an example of the AES class you would need to implement. As well as an example of encryption and decryption.

using System;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;

public class AES
{
    private readonly Encoding encoding;

    private SicBlockCipher mode;


    public AES(Encoding encoding)
    {
        this.encoding = encoding;
        this.mode = new SicBlockCipher(new AesFastEngine());
    }

    public static string ByteArrayToString(byte[] bytes)
    {
        return BitConverter.ToString(bytes).Replace("-", string.Empty);
    }

    public static byte[] StringToByteArray(string hex)
    {
        int numberChars = hex.Length;
        byte[] bytes = new byte[numberChars / 2];

        for (int i = 0; i < numberChars; i += 2)
        {
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        }

        return bytes;
    }


    public string Encrypt(string plain, byte[] key, byte[] iv)
    {
        byte[] input = this.encoding.GetBytes(plain);

        byte[] bytes = this.BouncyCastleCrypto(true, input, key, iv);

        string result = ByteArrayToString(bytes);

        return result;
    }


    public string Decrypt(string cipher, byte[] key, byte[] iv)
    {
        byte[] bytes = this.BouncyCastleCrypto(false, StringToByteArray(cipher), key, iv);

        string result = this.encoding.GetString(bytes);

        return result;
    }


    private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input, byte[] key, byte[] iv)
    {
        try
        {
            this.mode.Init(forEncrypt, new ParametersWithIV(new KeyParameter(key), iv));

            BufferedBlockCipher cipher = new BufferedBlockCipher(this.mode);

            return cipher.DoFinal(input);
        }
        catch (CryptoException)
        {
            throw;
        }
    }
}

Example Usage

string test = "1";

AES aes = new AES(System.Text.Encoding.UTF8);

RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
byte[] key = new byte[32];
byte[] iv = new byte[32];

// Generate random key and IV
rngCsp.GetBytes(key);
rngCsp.GetBytes(iv);

string cipher = aes.Encrypt(test, key, iv);

string plaintext = aes.Decrypt(cipher, key, iv);

Response.Write(cipher + "<BR/>");

Response.Write(plaintext);

Output Example

CB
1 
like image 200
nerdybeardo Avatar answered Oct 04 '22 09:10

nerdybeardo


//encryption   
string output="";   
char[] readChar = yourInput.ToCharArray();   
for (int i = 0; i < readChar.Length; i++)  
{   
    int no = Convert.ToInt32(readChar[i]) + 10;   
    string r = Convert.ToChar(no).ToString();   
    output+=r;   
}  
//decryption  
string output="";   
char[] readChar = yourInput.ToCharArray();   
for (int i = 0; i < readChar.Length; i++)   
{   
    int no = Convert.ToInt32(readChar[i]) - 10;   
    string r = Convert.ToChar(no).ToString();   
    output+=r;   
}
like image 29
Tapas Pal Avatar answered Oct 04 '22 07:10

Tapas Pal