本文介绍了[UWP]使用C#在UWP应用程序中解密文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在下面用于解密的其他(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) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;抛出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 |电子邮件:[email protected] | [email protected] 解决方案 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 ApplicationNot 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:[email protected]| [email protected] 解决方案 这篇关于[UWP]使用C#在UWP应用程序中解密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 23:59