[UWP]使用C#在UWP应用程序中解密文件 [英] [UWP]Stuck in Decryption of file in UWP Application using C#

查看:85
本文介绍了[UWP]使用C#在UWP应用程序中解密文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面用于解密的其他(Java)应用程序开发人员。



该代码在stackoverflow上可用,但是当我尝试将此代码用于相同的uwp应用程序时



找不到任何帮助。



  private static final byte [] salt = {

        (字节)0x43,(字节)0x76,(字节)0x95,(字节)0xc7,

        (字节)0x5b,(字节)0xd7,(字节)0x45,(字节)0x17


    };


    private static Cipher makeCipher(String pass,Boolean decryptMode)抛出GeneralSecurityException {
$
       //使用KeyFactory从密码短语中导出相应的密钥:

        PBEKeySpec keySpec = new PBEKeySpec(pass.toCharArray());

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(" PBEWithMD5AndDES");

        SecretKey key = keyFactory.generateSecret(keySpec);

        //从salt创建参数和任意次数的迭代:

        PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt,42);

        / *将密钥转储到文件进行测试:* /

        FileEncryptor.keyToFile(key);

        //设置密码:

        Cipher cipher = Cipher.getInstance(" PBEWithMD5AndDES");

        //将密码模式设置为解密或加密:

        if(decryptMode){

            cipher.init(Cipher.ENCRYPT_MODE,key,pbeParamSpec);

        } else {

            cipher.init(Cipher.DECRYPT_MODE,key,pbeParamSpec);

        }
       返回密码;

    }


  public static void decryptFile(String fileName,String pass)

                            抛出GeneralSecurityException,IOException {
$
        byte [] encData;

        byte [] decData;

       文件inFile =新文件(fileName +" .encrypted");

        Cipher cipher = FileEncryptor.makeCipher(pass,false);
$
        FileInputStream inStream = new FileInputStream(inFile);

        encData = new byte [(int)inFile.length()];

        inStream.read(encData);

        inStream.close();

        //解密文件数据:

        decData = cipher.doFinal(encData);

        int padCount =(int)decData [decData.length - 1];

        //为了进行可靠的检查,请检查最后的padCount字节是否具有相同的值

        if(padCount> = 1&& padCount< = 8){

            decData = Arrays.copyOfRange(decData,0,decData.length - padCount);

        }
       /将解密数据写入新文件:

        FileOutputStream target = new FileOutputStream(new File(fileName +" .decrypted.txt"));
$
        target.write(decData);

   }



Arun Singh Rawat |流动性| UWP | WPF Skype:arun.rawat017 |电子邮件:arun.rawat017@hotmail.com | arun.rawat017@gmail.com

解决方案


嗨Ronald Eekelder,



< span style ="font-size:10.5pt; font-family:'Segoe UI',sans-serif;颜色:#454545">似乎Jave代码使用对称密钥来加密字符串。它使用MD5和DES生成密钥。所以你需要在UWP API中获得相同的算法。



我建议您查看
< a href ="https://docs.microsoft.com/en-us/uwp/api/Windows.Security.Cryptography.Core.SymmetricKeyAlgorithmProvider">
SymmetricKeyAlgorithmProvider类和
HashAlgorithmProvider类
,提供DES和MD5算法。





最好的问候,



Roy


In other (Java) application developer used below code for decryption.

that code is available on stackoverflow but when i try to use this code for same uwp Application

Not found any help for it.

 private static final byte[] salt = {
        (byte) 0x43, (byte) 0x76, (byte) 0x95, (byte) 0xc7,
        (byte) 0x5b, (byte) 0xd7, (byte) 0x45, (byte) 0x17
    };

    private static Cipher makeCipher(String pass, Boolean decryptMode) throws GeneralSecurityException{
       //Use a KeyFactory to derive the corresponding key from the passphrase:
        PBEKeySpec keySpec = new PBEKeySpec(pass.toCharArray());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(keySpec);
        //Create parameters from the salt and an arbitrary number of iterations:
        PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 42);
        /*Dump the key to a file for testing: */
        FileEncryptor.keyToFile(key);
        //Set up the cipher:
        Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
        //Set the cipher mode to decryption or encryption:
        if(decryptMode){
            cipher.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, key, pbeParamSpec);
        }
        return cipher;
    }

 public static void decryptFile(String fileName, String pass)
                            throws GeneralSecurityException, IOException{
        byte[] encData;
        byte[] decData;
        File inFile = new File(fileName+ ".encrypted");
        Cipher cipher = FileEncryptor.makeCipher(pass, false);
        FileInputStream inStream = new FileInputStream(inFile );
        encData = new byte[(int)inFile.length()];
        inStream.read(encData);
        inStream.close();
        //Decrypt the file data:
        decData = cipher.doFinal(encData);
        int padCount = (int)decData[decData.length - 1];
        //For robust check, check that padCount bytes at the end have same value
        if( padCount >= 1 && padCount <= 8 ) {
            decData = Arrays.copyOfRange( decData , 0, decData.length - padCount);
        }
       /Write the decrypted data to a new file:
        FileOutputStream target = new FileOutputStream(new File(fileName + ".decrypted.txt"));
        target.write(decData);
   }


Arun Singh Rawat | Mobility | UWP| WPF Skype: arun.rawat017 | Email:arun.rawat017@hotmail.com| arun.rawat017@gmail.com

解决方案

Hi Ronald Eekelder,

It seems the Jave code are using Symmetric keys to encrypt the string. And it is using MD5 and DES to generate the key. So you need to get the same algorithm in UWP APIs.

I suggest that you could check the SymmetricKeyAlgorithmProvider Class and HashAlgorithmProvider Class that provide DES and MD5 algorithm.

Best regards,

Roy


这篇关于[UWP]使用C#在UWP应用程序中解密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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