android DES decrypt file : pad block corrupted

Posted by Kenny Chang on Stack Overflow See other posts from Stack Overflow or by Kenny Chang
Published on 2011-03-10T08:09:04Z Indexed on 2011/03/10 8:10 UTC
Read the original article Hit count: 586

Filed under:
|
|
|
  public class TestDES {
  Key key;
  public TestDES(String str) {
    getKey(str);
  }
  public void getKey(String strKey) {
    try {
        KeyGenerator _generator = KeyGenerator.getInstance("DES");
        _generator.init(new SecureRandom(strKey.getBytes()));
        this.key = _generator.generateKey();
        _generator = null;
    } catch (Exception e) {
        throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
    }
  }

  public void decrypt(String file, String dest) throws Exception {
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.DECRYPT_MODE, this.key);
    InputStream is = new FileInputStream(file);
    OutputStream out = new FileOutputStream(dest);
    CipherOutputStream cos = new CipherOutputStream(out, cipher);
    byte[] buffer = new byte[1024 * 1024 * 6];
    int r;
    while ((r = is.read(buffer)) >= 0) {
        cos.write(buffer, 0, r);
    }
    cos.close();a
    out.close();
    is.close();
  }
}

The code works well on PC JAVA Program, but not on android.The error "pad block corrupted" happended at 'cos.close();' LogCat shows:" 03-10 07:43:04.431: WARN/System.err(23765): java.io.IOException: pad block corrupted 03-10 07:43:04.460: WARN/System.err(23765): at javax.crypto.CipherOutputStream.close(CipherOutputStream.java:157) "

© Stack Overflow or respective owner

Related posts about android

Related posts about block