symmetric encryption in c# resembles JAVA -


private static byte[] encryptdata(bytearrayoutputstream data, byte[] symmetrickey) throws encryptionexception {         try {             secretkey seckey = new secretkeyspec(symmetrickey, "aes");             cipher cipher = cipher.getinstance("aes");             cipher.init(cipher.encrypt_mode, seckey);             return cipher.dofinal(data.tobytearray());         } catch (nosuchalgorithmexception | nosuchpaddingexception | illegalblocksizeexception |                 invalidkeyexception |                 badpaddingexception e) {             throw new encryptionexception(e);         }     } 

i have situation need encrypt data using .net , decrypt same data using java. essentially, need rewrite above encryption method in .net.

public byte[] encrypt(byte[] key, byte[] plaintext)         {             using (aescryptoserviceprovider aesprovider = new aescryptoserviceprovider())             {                 using (icryptotransform encryptor = aesprovider.createencryptor(key, magiciv))                 {                     using (memorystream ms = new memorystream())                     {                         using (cryptostream cs = new cryptostream(ms, encryptor, cryptostreammode.write))                         {                             cs.write(plaintext, 0, plaintext.length);                         }                         byte[] ciphertext = ms.toarray();                         return ciphertext;                     }                 }             }         } 

the above code used somewhere mandates iv java not asking for. iv used in java code?

i tried many links didn't work. symmetric encryption between .net , java

please help

if current java decryption code not ask iv (and decryption returns same data encrypted) cipher.getinstance("aes") returning object using ecb block mode.

.net symmetric algorithms default cbc block mode, requires iv.

you have couple of options:

  • set aesprovider.mode = ciphermode.ecb before calling createencryptor.
  • pass aesprovider.iv iv parameter of createencryptor. iv property make cryptographically random value on first read if it's not set.
    • you need pass data decryption routine, should use "aes/cbc/pkcs5padding", , set iv 1 in java.
    • one common method of transport prepend data ciphertext, pick off first 16 bytes @ decryption time.
    • do not use fixed value iv, because it's same ecb.

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -