垫一块解密过程中损坏(Android版) [英] pad block corrupted during decryption(Android)

查看:126
本文介绍了垫一块解密过程中损坏(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加密在Java中的文件和加密文件和私钥发送至Android设备。不过,虽然在解密Android的文件,它给垫块损坏错误。
顺便说一句,相同的解密code适用于PC

下面是加密:

 公共无效加密(INF文件,文件OUTF,文件公钥,INT用户id,诠释RESOURCEID)抛出ArServerConnectionException {
    //加密BEGIN
    尝试{
        pkCipher = Cipher.getInstance(RSA);
    }赶上(抛出:NoSuchAlgorithmException E){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(NoSuchPaddingException E){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    //创建共享AES密钥密码
    尝试{
        aesCipher = Cipher.getInstance(AES);
    }赶上(抛出:NoSuchAlgorithmException E){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(NoSuchPaddingException E){
        // TODO自动生成catch块
        e.printStackTrace();
    }    尝试{
        makeKey();
    }赶上(抛出:NoSuchAlgorithmException E){
        // TODO自动生成catch块
        e.printStackTrace();
    }
            //文件操作            尝试{
        saveKey(新文件(System.getProperty(的user.home)+/+用户id
                +/密钥文件),公钥);
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(GeneralSecurityException E){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    //文件操作    尝试{
        encryptFiles(INF,OUTF);
    }赶上(InvalidKeyException将E){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }    // /加密END
}    公共无效saveKey(文件时,文件publicKeyFile)抛出IOException异常,
        GeneralSecurityException {
    //读取公共密钥用于加密AES密钥
    字节[] EN codeDKEY =新的字节[(INT)publicKeyFile.length()];
    新的FileInputStream(publicKeyFile).read(EN codeDKEY);    //创建公钥
    X509En codedKeySpec publicKeySpec =新X509En codedKeySpec(EN codeDKEY);
    KF的KeyFactory = KeyFactory.getInstance(RSA);
    公钥PK = kf.generatePublic(publicKeySpec);    //写AES密钥
    pkCipher.init(Cipher.ENCRYPT_MODE,PK);
    CipherOutputStream OS =新CipherOutputStream(
            新的FileOutputStream(出),pkCipher);
    os.write(aesKey);
    os.close();
}
    公共无效makeKey()抛出抛出:NoSuchAlgorithmException {
    的KeyGenerator kgen = KeyGenerator.getInstance(AES);
    kgen.init(AES_Key_Size);
    SecretKey的关键= kgen.generateKey();
    aesKey = key.getEn codeD();
    aeskeySpec =新SecretKeySpec(aesKey,AES);
}

下面是部分解密:

 公共类FileDecrypt {
    公共静态最终诠释AES_Key_Size = 256;    密码pkCipher,aesCipher;
    字节[] aesKey;
    SecretKeySpec aeskeySpec;
    私人字符串pubKeyPath = NULL;
    私人字符串prvKeyPath = NULL;
    私人字符串keyFilePath = NULL;
    私人字符串encFilePath = NULL;
    私人字符串unencFilePath = NULL;
    公共字符串getEncFilePath(){
        返回encFilePath;
    }    公共无效setEncFilePath(字符串encFilePath){
        this.encFilePath = encFilePath;
    }    公共字符串getUnencFilePath(){
        返回unencFilePath;
    }    公共无效setUnencFilePath(字符串unencFilePath){
        this.unencFilePath = unencFilePath;
    }    公共字符串getPubKeyPath(){
        返回pubKeyPath;
    }    公共无效setPubKeyPath(字符串pubKeyPath){
        this.pubKeyPath = pubKeyPath;
    }    公共字符串getPrvKeyPath(){
        返回prvKeyPath;
    }    公共无效setPrvKeyPath(字符串prvKeyPath){
        this.prvKeyPath = prvKeyPath;
    }    公共字符串getKeyFilePath(){
        返回keyFilePath;
    }    公共无效setKeyFilePath(字符串keyFilePath){
        this.keyFilePath = keyFilePath;
    }
    公共无效解密(){
        Log.i(\"DECRYPT\",\"**************************************************DECRYPT&*******************\");
        Log.i(encFilePath,encFilePath);
        Log.i(pubKeyPath,pubKeyPath);
        Log.i(prvKeyPath,prvKeyPath);
        Log.i(keyFilePath,keyFilePath);
        Log.i(unencFilePath,unencFilePath);
        Log.i(解密,************ *** DECRYPT&安培; *******************);
        尝试{
            pkCipher = Cipher.getInstance(RSA);
        }赶上(抛出:NoSuchAlgorithmException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(NoSuchPaddingException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        尝试{
            aesCipher = Cipher.getInstance(AES);
        }赶上(抛出:NoSuchAlgorithmException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(NoSuchPaddingException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        //解密BEGIN        文件PKF =新的文件(pubKeyPath);
        字节[] EN codeDKEY =新的字节[(INT)pkf.length()];
        尝试{
            新的FileInputStream(PKF).read(EN codeDKEY);
            //创建公钥
            X509En codedKeySpec publicKeySpec =新X509En codedKeySpec(EN codeDKEY);
            KF的KeyFactory = KeyFactory.getInstance(RSA);
            公钥PK = kf.generatePublic(publicKeySpec);            //写AES密钥
            pkCipher.init(Cipher.ENCRYPT_MODE,PK);
        }赶上(FileNotFoundException异常E1){
            // TODO自动生成catch块
            e1.printStackTrace();
        }赶上(IOException异常E1){
            // TODO自动生成catch块
            e1.printStackTrace();
        }赶上(抛出:NoSuchAlgorithmException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(InvalidKeyException将E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(InvalidKeySpecException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        尝试{
            LOADKEY(新文件(keyFilePath),新的文件(prvKeyPath));
        }赶上(GeneralSecurityException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }        尝试{
            解密(新文件(encFilePath),新的文件(unencFilePath));
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }        //解密END
    }    / **
     *从解密的文件使用RSA私钥的AES密钥
     * /
    公共无效LOADKEY(文件,文件privateKeyFile)
            抛出GeneralSecurityException,IOException异常{
        //读取私钥被用于解密的AES密钥
        字节[] EN codeDKEY =新的字节[(INT)privateKeyFile.length()];
        新的FileInputStream(privateKeyFile).read(EN codeDKEY);        //创建私有密钥
        PKCS8En codedKeySpec privateKeySpec =新PKCS8En codedKeySpec(EN codeDKEY);
        KF的KeyFactory = KeyFactory.getInstance(RSA);
        PrivateKey PK = kf.generatePrivate(privateKeySpec);        //阅读AES密钥
        pkCipher.init(Cipher.DECRYPT_MODE,PK);
        aesKey =新的字节[AES_Key_Size / 8]。
        CipherInputStream是=新CipherInputStream(新的FileInputStream(中)
                pkCipher);
        is.read(aesKey);
        aeskeySpec =新SecretKeySpec(aesKey,AES);
    }    / **
     *解密,然后复制给定文件的内容。
     * /
    公共无效解密(文件,文件输出)抛出IOException异常
         {
        尝试{
            aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec);
        }赶上(InvalidKeyException将E){
            Log.i(EXCEPTION,无效的密钥异常);
            e.printStackTrace();
        }        CipherInputStream是=新CipherInputStream(新的FileInputStream(中)
                aesCipher);
        FileOutputStream中OS =新的FileOutputStream(出);        副本(是,OS);        is.close();
        os.close();
    }    / **
     *副本流。
     * /
    私人无效副本(InputStream为,OutputStream的OS)抛出IOException
        INT I;
        字节[] B =新的字节[2048];
        而((ⅰ= is.​​read(二))!= - 1){
            os.write(B,0,I);
        }
    }
}


解决方案

我们也面临着同样的问题,那也只是在Android设备上,方太做加密部分解决(使用Android SDK),然后解密它的设备。发现该问题没有其他正当的理由。

I'm encrypting a file in java and send the encrypted file and private key to android device. But while decrypting the file in Android, it gives pad block corrupted error. By the way, same decryption code works on PC

Here is the encryption:

public void encrypt(File inf, File outf, File publicKey, int userId, int resourceId) throws ArServerConnectionException {
    // ENCRYPTION BEGIN
    try {
        pkCipher = Cipher.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // create AES shared key cipher
    try {
        aesCipher = Cipher.getInstance("AES");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        makeKey();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
            // File operation

            try {
        saveKey(new File(System.getProperty("user.home") + "/" + userId
                + "/keyfile"), publicKey);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // File operation

    try {
        encryptFiles(inf, outf);
    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // /ENCRYPTION END
}

    public void saveKey(File out, File publicKeyFile) throws IOException,
        GeneralSecurityException {
    // read public key to be used to encrypt the AES key
    byte[] encodedKey = new byte[(int) publicKeyFile.length()];
    new FileInputStream(publicKeyFile).read(encodedKey);

    // create public key
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PublicKey pk = kf.generatePublic(publicKeySpec);

    // write AES key
    pkCipher.init(Cipher.ENCRYPT_MODE, pk);
    CipherOutputStream os = new CipherOutputStream(
            new FileOutputStream(out), pkCipher);
    os.write(aesKey);
    os.close();
}
    public void makeKey() throws NoSuchAlgorithmException {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(AES_Key_Size);
    SecretKey key = kgen.generateKey();
    aesKey = key.getEncoded();
    aeskeySpec = new SecretKeySpec(aesKey, "AES");
}

Here is the decryption part:

public class FileDecrypt {
    public static final int AES_Key_Size = 256;

    Cipher pkCipher, aesCipher;
    byte[] aesKey;
    SecretKeySpec aeskeySpec;
    private String pubKeyPath=null;
    private String prvKeyPath=null;
    private String keyFilePath=null;
    private String encFilePath=null;
    private String unencFilePath=null;


    public String getEncFilePath() {
        return encFilePath;
    }

    public void setEncFilePath(String encFilePath) {
        this.encFilePath = encFilePath;
    }

    public String getUnencFilePath() {
        return unencFilePath;
    }

    public void setUnencFilePath(String unencFilePath) {
        this.unencFilePath = unencFilePath;
    }

    public String getPubKeyPath() {
        return pubKeyPath;
    }

    public void setPubKeyPath(String pubKeyPath) {
        this.pubKeyPath = pubKeyPath;
    }

    public String getPrvKeyPath() {
        return prvKeyPath;
    }

    public void setPrvKeyPath(String prvKeyPath) {
        this.prvKeyPath = prvKeyPath;
    }

    public String getKeyFilePath() {
        return keyFilePath;
    }

    public void setKeyFilePath(String keyFilePath) {
        this.keyFilePath = keyFilePath;
    }
    public void decrypt() {
        Log.i("DECRYPT","**************************************************DECRYPT&*******************");
        Log.i("encFilePath",encFilePath);
        Log.i("pubKeyPath",pubKeyPath);
        Log.i("prvKeyPath",prvKeyPath);
        Log.i("keyFilePath",keyFilePath);
        Log.i("unencFilePath",unencFilePath);
        Log.i("DECRYPT","********************************************DECRYPT&*******************");
        try {
            pkCipher = Cipher.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            aesCipher = Cipher.getInstance("AES");
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //DECRYPTION BEGIN

        File pkf=new File(pubKeyPath);
        byte[] encodedKey = new byte[(int) pkf.length()];
        try {
            new FileInputStream(pkf).read(encodedKey);
            // create public key
            X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
            KeyFactory kf = KeyFactory.getInstance("RSA");
            PublicKey pk = kf.generatePublic(publicKeySpec);

            // write AES key
            pkCipher.init(Cipher.ENCRYPT_MODE, pk);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            loadKey(new File(keyFilePath), new File(prvKeyPath));
        } catch (GeneralSecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            decrypt(new File(encFilePath), new File(unencFilePath));
        }  catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //DECRYPTION END
    }

    /**
     * Decrypts an AES key from a file using an RSA private key
     */
    public void loadKey(File in, File privateKeyFile)
            throws GeneralSecurityException, IOException {
        // read private key to be used to decrypt the AES key
        byte[] encodedKey = new byte[(int) privateKeyFile.length()];
        new FileInputStream(privateKeyFile).read(encodedKey);

        // create private key
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey pk = kf.generatePrivate(privateKeySpec);

        // read AES key
        pkCipher.init(Cipher.DECRYPT_MODE, pk);
        aesKey = new byte[AES_Key_Size / 8];
        CipherInputStream is = new CipherInputStream(new FileInputStream(in),
                pkCipher);
        is.read(aesKey);
        aeskeySpec = new SecretKeySpec(aesKey, "AES");
    }

    /**
     * Decrypts and then copies the contents of a given file.
     */
    public void decrypt(File in, File out) throws IOException
         {
        try {
            aesCipher.init(Cipher.DECRYPT_MODE, aeskeySpec);
        } catch (InvalidKeyException e) {
            Log.i("EXCEPTION","INVALID KEY EXCEPTION");
            e.printStackTrace();
        }

        CipherInputStream is = new CipherInputStream(new FileInputStream(in),
                aesCipher);
        FileOutputStream os = new FileOutputStream(out);

        copy(is, os);

        is.close();
        os.close();
    }

    /**
     * Copies a stream.
     */
    private void copy(InputStream is, OutputStream os) throws IOException {
        int i;
        byte[] b = new byte[2048];
        while ((i = is.read(b)) != -1) {
            os.write(b, 0, i);
        }
    }
}

解决方案

We were also facing the same issue, It was solved only on doing the encrypting part too on android side(Using android SDK) and then decrypting it on device. No other correct reasons were found for the issue..

这篇关于垫一块解密过程中损坏(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆