user1457227
user1457227

Reputation: 71

Issue with Base64-encoded data (encrypt in c#, decrypt in java)

I am using code to Base-64 encode and encrypt data in c#, then I ship the file over to my Android app where I attempt to decrypt it.

Problem is, I get an "Length of Base64 encoded input string is not a multiple of 4." error when decrypting:

(Java code for Android):

    try
    {
          Boolean inEvent = false;

          // read encrypted file to string
          BufferedInputStream fin = new BufferedInputStream(new FileInputStream(filename));
          ByteArrayOutputStream bout = new ByteArrayOutputStream();
          byte buffer[] = new byte[8192];
          int read = fin.read(buffer);
          while(read != -1) {
              bout.write(buffer, 0, read);
              read = fin.read(buffer);
          }
          fin.close();
          String encryptedText = bout.toByteArray().toString();
          String unencryptedText = "";

          // decrypt string
          try
          {
              unencryptedText = Decrypt(encryptedText, sKey); <-- error occurs here
          }
          catch ( Exception e)
          {
              alert(e.getMessage());
              return sched;
          }

Decrypt method:

protected String Decrypt(String text, String key) throws Exception
{
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    byte[] keyBytes= new byte[16];
    byte[] b= key.getBytes("UTF-8");
    int len= b.length;
    if (len > keyBytes.length) len = keyBytes.length;
    System.arraycopy(b, 0, keyBytes, 0, len);
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
    cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);

    byte [] results = cipher.doFinal(Base64Coder.decode(text));
    return new String(results,"UTF-8");
}

Finally, here is the c# code I am encrypting with:

(c# code):

  string Encrypt(string textToEncrypt, string key)
  {
    RijndaelManaged rijndaelCipher = new RijndaelManaged();
    rijndaelCipher.Mode = CipherMode.CBC;
    rijndaelCipher.Padding = PaddingMode.PKCS7;

    rijndaelCipher.KeySize = 0x80;
    rijndaelCipher.BlockSize = 0x80;
    byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
    byte[] keyBytes = new byte[0x10];
    int len = pwdBytes.Length;
    if (len > keyBytes.Length)
    {
      len = keyBytes.Length;
    }
    Array.Copy(pwdBytes, keyBytes, len);
    rijndaelCipher.Key = keyBytes;
    rijndaelCipher.IV = keyBytes;
    ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
    byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt);
    return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0,   plainText.Length));
  }

Not sure what's wrong. Does the length of the key have to be some specific number of bytes long?

Upvotes: 0

Views: 1901

Answers (1)

Sean Owen
Sean Owen

Reputation: 66881

The comment has already identified the problem, and you'd see it immediately if you debugged the key item here: the base 64 string you think you are reading.

You collect your bytes from the file in bout. But your attempt to convert it to a string representation is not doing anything like what you imagine. It's going to be something like "[B@2352544e]", just Java's internal default toString() from the array. Instead, try new String(bout.toByteArray(), Charset.forName("US-ASCII")).

Upvotes: 2

Related Questions