0

How can I create a key and iv, so that encrypted string should only contain digits and characters.key string and iv string should contain 32 characters.purpose is encrypted string should not contain any special characters.block size is 256. now I am using key and iv like this.after encryption using this, getting encrypted string with special characters.how can we avoid this.

AES_Key=PSVJQRk9QTEpNVU1DWUZCRVFGV1VVT0ZOV1RRU1NaWQ=

Aes_iv=YWlFLVEZZUFNaWlhPQ01ZT0lLWU5HTFJQVFNCRUJZVA=

user922834
  • 195
  • 2
  • 6
  • 18
  • A block size of 256 bits is quite unusual, since it's not part of AES. – CodesInChaos Apr 25 '12 at 16:46
  • I am using code specified in this article.http://blog.djekldevelopments.co.uk/?p=334 problem is decryption is not working in php if input contanins special charcter like ' <' – user922834 Apr 25 '12 at 16:59
  • 1
    That blog author is clearly confused. The introduction claim to use AES-256, but then he uses Rijndael with a block size of 256 bits. – CodesInChaos Apr 25 '12 at 17:02
  • You cannot store a key of 256 random bits (32 bytes) in 32 bytes that may only contain digits and alphanumerics. I think this should be obvious. – Maarten Bodewes Jul 22 '12 at 23:24

1 Answers1

2

You need to Base64 encode the output. This will ensure it's only printable ASCII, and is rather standard for storing/transmitting encrypted data.

You want your keys and IV's to be completely random. Just because you don't have special characters in your keys, doesn't mean you won't get special characters in your output. There is absolutely no correlation. It looks like in your keys and IV's are base64 encoded there. Make sure you are using the raw keys and IV's.

Here's how to do it in C#. Assuming your AES output is a byte array:

        string encoded = System.Convert.ToBase64String(aesOutput);

If it's a string:

        byte[] encodedBytes = System.Text.Encoding.ASCII.GetBytes(aesOutput);
        string base64encoded = System.Convert.ToBase64String(encodedBytes);

You can use System.Convert.FromBase64String(encodedData) to convert back to it's original output.

mfanto
  • 14,168
  • 6
  • 51
  • 61
  • my doubt is if we are not using special characters in key and iv, then we get – user922834 Apr 25 '12 at 17:10
  • problem is decryption is not working in php if input contains special charcter like ' <' – user922834 Apr 25 '12 at 17:15
  • You need to post a lot more information. That's entirely a different question. Post your keys, IVs, the encrypted string you're sending to PHP, any relevant errors, relevant parts of the PHP and C# code etc. – mfanto Apr 25 '12 at 17:23
  • there is one correction .key and iv contains special characters. key is PSVJQRk9QTEpNVU1DWUZCRVFGV1VVT0ZOV1RRU1NaWQ= iv is YWlFLVEZZUFNaWlhPQ01ZT0lLWU5HTFJQVFNCRUJZVA= – user922834 Apr 25 '12 at 17:27
  • key and iv contains equal symbol also – user922834 Apr 25 '12 at 17:30
  • Read up on base 64, use base 32 if you really don't want any special characters (for web applications, you may put base 64 through another encoding function, like urlencode). – Maarten Bodewes May 10 '12 at 00:05