0

i implement AES 256 bit algorithm in C# but i am encrypting 128bit block of plain text which require padding so i dont want to pad and want use to stream cipher

  1. use stream cipher instead of using 128 bit block
  2. encrypt stream byte by byte

    CryptLib _crypt = new CryptLib();
    
    //string plainText = "This is the text to be encrypted";
    String iv = CryptLib.GenerateRandomIV(16); //16 bytes = 128 bits
    string key = CryptLib.getHashSha256("my secret key", 31); //32 bytes = 256 bits
    MessageBox.Show(arm);//////////////////////
    String cypherText = _crypt.encrypt(string1, key, iv);
    Console.WriteLine("iv=" + iv);
    Console.WriteLine("key=" + key);
    Console.WriteLine("Cypher text=" + cypherText);
    MessageBox.Show(cypherText);
    textBox1.Text = cypherText;
    Console.WriteLine("Plain text =" + _crypt.decrypt(cypherText, key, iv));
    MessageBox.Show(_crypt.decrypt(cypherText, key, iv));
    
    
    
    String dypher = _crypt.decrypt(cypherText, key, iv);
    string outp = string.Empty;
    char[] value = dypher.ToCharArray();
    
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • 2
    what's your question? – xGeo Mar 08 '17 at 09:02
  • my question is instead of encrypting128 bit block which require padding can i encrypt byte by byte encryption like stream cipher because i dont want to use block because it requires padding i hope now you understand my question – Adnan Shafiq Mar 08 '17 at 09:10
  • I had created some time ago a C# implementation of AES-CTR, that is a stream encryption mode for AES. See http://stackoverflow.com/a/29562965/613130 – xanatos Mar 08 '17 at 09:59

1 Answers1

2

If the input data is always an exact multiple of the block size you can just specify no padding.

if you have data of unknown non-uniform block lengths padding is the general way to handle that. Why do you not want to use padding.

Additionally:

It is common to prefix the encrypted data with the IV for use during decryption. The IV does not need to be secret and with this method the IV does not need to be shared in some other way and can easily be a different random value for each encryption.

Deriving a key from a password (string) with a hash function is not considered secure, instead use a key derivarion function such as PBKDF2.

zaph
  • 111,848
  • 21
  • 189
  • 228