AES encryption in java and AES decryption in c #


I hope the question is not redundant, and you can help me. I am creating an application that can connect to a webservice. in the application I ask for a user and a password which I must encrypt with the AES algorithm, this encrypted data arrives at the webservice hosted in IIS, but when decrypting in the server sends me an error. this is the code to encrypt in android

   String password="1234";
   String encode="UTF-8"; 
   String algoritmo="AES";

      private String encripta(String serie) throws Exception {
      Cipher cipher=Cipher.getInstance(algoritmo);
      SecretKeySpec secretKey= generatekey();
     byte[] datosencriptadosbytes=cipher.doFinal(serie.getBytes());
     String datosencriptadosstring= 
     return  datosencriptadosstring;

code that generates the key

 private SecretKeySpec generatekey() throws Exception {
    MessageDigest sha=MessageDigest.getInstance(sha_str);
    byte[] key=password.getBytes(encode);
    SecretKeySpec secretKey=new SecretKeySpec(key,algoritmo);
    return  secretKey;

This is on the server side

 public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
       byte[] decryptedBytes = null;
       byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
       byte[] saltBytes = Encoding.UTF8.GetBytes("123456789ABCDEF");
        using (MemoryStream ms = new MemoryStream())
            using (AesManaged AES = new AesManaged())
                AES.Padding = PaddingMode.Zeros;
                AES.KeySize = 256;
                AES.BlockSize = 128;

            var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes,1000);
                AES.Key = key.GetBytes(AES.KeySize/8 );
                AES.IV = key.GetBytes(AES.BlockSize /8);

                AES.Mode = CipherMode.ECB;
                using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
                cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                decryptedBytes = ms.ToArray();

        return decryptedBytes;

and the error it gives me is "the filling between characters is not valid and can not be removed" in visual studio, I hope you can help me thanks

asked by Yeltssin Mendoza 28.06.2018 в 16:49

0 answers