AES解密和加密问题

发布于 2025-01-04 21:19:47 字数 2543 浏览 0 评论 0原文

我已经使用 AES 进行了加密,但无法进行解密,我遇到了 javax.crypto.IllegalBlockSizeException: 最后一个块在解密中不完整.....我无法找出问题出在哪里.我很感激任何帮助。

    try {
                 in = new FileInputStream("/sdcard/Pic 1.txt");
            } catch (FileNotFoundException e) {

                e.printStackTrace();
            }
            File f=new File("/sdcard/Pic 1.txt");

            buf = new byte[(int) f.length()+2];


            try {
                in.read(buf);
            } catch (IOException e) {

                e.printStackTrace();
            }

            try {

                decryptedData = decrypt(key, buf);

                decryptedimage = BitmapFactory.decodeByteArray(
                        decryptedData, 0, decryptedData.length);

                loadedimage.setImageBitmap(decryptedimage);

            } catch (Exception e) {

                e.printStackTrace();
            }

        }
    });

}

public void Encription(Bitmap bm) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    b = baos.toByteArray();

    try {
        keyStart = ".....".getBytes();
        kgen = KeyGenerator.getInstance("AES");
        sr = SecureRandom.getInstance("SHA1PRNG");
        sr.setSeed(keyStart);
        kgen.init(128, sr); // 192 and 256 bits may not be available
        skey = kgen.generateKey();
        key = skey.getEncoded();

        encryptedData = encrypt(key, b);

        encryptedimage = BitmapFactory.decodeByteArray(encryptedData, 0,
                encryptedData.length);


         out = new FileOutputStream("/sdcard/"+"Pic "+i+".txt");
         out.write(encryptedData, 0, encryptedData.length);

            out.close();
    } catch (Exception e) {

    }
    i++;
}

private byte[] encrypt(byte[] raw, byte[] clear) throws Exception {

    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}
private byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
    long start=System.currentTimeMillis()/1000l;
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

02-19 08:27:06.972: W/System.err(12742): javax.crypto.IllegalBlockSizeException: last block incomplete in decryption

I have done an encryption using the AES but iam not able to do the decryption Iam gettin an javax.crypto.IllegalBlockSizeException: last block incomplete in decryption.....I cant trace out where is the problem.I appreciate any help.

    try {
                 in = new FileInputStream("/sdcard/Pic 1.txt");
            } catch (FileNotFoundException e) {

                e.printStackTrace();
            }
            File f=new File("/sdcard/Pic 1.txt");

            buf = new byte[(int) f.length()+2];


            try {
                in.read(buf);
            } catch (IOException e) {

                e.printStackTrace();
            }

            try {

                decryptedData = decrypt(key, buf);

                decryptedimage = BitmapFactory.decodeByteArray(
                        decryptedData, 0, decryptedData.length);

                loadedimage.setImageBitmap(decryptedimage);

            } catch (Exception e) {

                e.printStackTrace();
            }

        }
    });

}

public void Encription(Bitmap bm) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    b = baos.toByteArray();

    try {
        keyStart = ".....".getBytes();
        kgen = KeyGenerator.getInstance("AES");
        sr = SecureRandom.getInstance("SHA1PRNG");
        sr.setSeed(keyStart);
        kgen.init(128, sr); // 192 and 256 bits may not be available
        skey = kgen.generateKey();
        key = skey.getEncoded();

        encryptedData = encrypt(key, b);

        encryptedimage = BitmapFactory.decodeByteArray(encryptedData, 0,
                encryptedData.length);


         out = new FileOutputStream("/sdcard/"+"Pic "+i+".txt");
         out.write(encryptedData, 0, encryptedData.length);

            out.close();
    } catch (Exception e) {

    }
    i++;
}

private byte[] encrypt(byte[] raw, byte[] clear) throws Exception {

    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}
private byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
    long start=System.currentTimeMillis()/1000l;
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

02-19 08:27:06.972: W/System.err(12742): javax.crypto.IllegalBlockSizeException: last block incomplete in decryption

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文