用c#加密文件并在flutter中解密 [英] Encrypt a file in c# and decrypt in flutter

查看:165
本文介绍了用c#加密文件并在flutter中解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用System.Security.Cryptography中可用的RijndaelManaged用c#代码加密了一个文件.该文件需要转移到使用dart/flutter开发的移动应用中,我需要使用dart代码对其进行解密并将其呈现给用户.该怎么办?

I have encrypted a file in c# code using RijndaelManaged which is available in System.Security.Cryptography. This file needs to be transferred to a mobile app developed using dart/flutter and I need it to be decrypted using dart code and present it to the user. How can this be done?

下面显示的是在c#中进行加密的代码:

Below shown is the code to do the encryption in c#:

            string password = keyPhrase; // Your Key Here
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] key = UE.GetBytes(password);

            string cryptFile = outputFile;
            FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

            RijndaelManaged RMCrypto = new RijndaelManaged();

            CryptoStream cs = new CryptoStream(fsCrypt,
                RMCrypto.CreateEncryptor(key, key),
                CryptoStreamMode.Write);

            FileStream fsIn = new FileStream(inputFile, FileMode.Open);

            int data;
            while ((data = fsIn.ReadByte()) != -1)
                cs.WriteByte((byte)data);


            fsIn.Close();
            cs.Close();
            fsCrypt.Close();

谢谢

推荐答案

.Net中Rijndael的默认模式是128位块大小-与AES兼容.除非您使用的是非标准块大小,否则最好使用.Net的AesManaged.

The default mode of Rijndael in .Net is 128 bit block size - compatible with AES. Unless you are using a non-standard block size, prefer .Net's AesManaged.

您尚未指定要使用的填充或模式. .Net默认值似乎是CBC,因此我们假设是.尚不清楚它是否默认为特定的填充模式.

You haven't specified which padding or mode you are using. The .Net default seems to be CBC, so we'll assume that. It's not clear whether it defaults to a certain padding mode.

(请注意,您同时将密钥用作IV和密钥.对于加密例程的每次调用,IV都应该是唯一的.TLDR-使用AesManaged的方式是不安全的-请勿在现实生活.)

(Note that you are using the key both as the IV and the key. The IV should be unique for each invocation of the encryption routine. TLDR - the way you are using AesManaged is insecure - don't use this code in real life.)

此外,您正在从字符串中解码密钥. AES的密钥长度必须恰好是128位或256位(或更特殊的一位).除非您正确选择了字符串,否则不太可能将UTF-8编码为确切的密钥长度.另外,通过使用字符串,您仅在键中仅使用碰巧是字符的字节.通常,要将字符串用作密码,您可以使用密钥派生算法(例如PBKDF2)将其转换为密钥,而不仅仅是使用UTF-8编码.

Also, you are decoding the key from a string. The key length of AES must be exactly 128 or 256 bits (or one of the more unusual ones). Unless you have chosen your string well, it is unlikely to UTF-8 encode to an exact key length. Also, by using a string you are only using bytes in the key that happen to be characters. Typically, to use a string as a password you would convert it to a key using a key derivation algorithm (e.g. PBKDF2) rather than just UTF-8 encoding it.

话虽如此,如果您的密码正好是16个(或32个长),并且文件是16个字节的正好倍数(如果不是,则需要决定如何填充它),您应该可以解密像这样:

With all that said, if your password is exactly 16 (or 32 long) and your file is an exact multiple of 16 bytes (if it is not, you need to decide how to pad it) you should be able to decrypt it like this:

import 'dart:convert';
import 'dart:io';

import 'package:pointycastle/export.dart';

main() async {
  var key = utf8.encode('abcdefghijklmnop');

  var cipher = CBCBlockCipher(AESFastEngine())
    ..init(false, ParametersWithIV<KeyParameter>(KeyParameter(key), key));

  var cipherText = await File('encryptedFile').readAsBytes();
  var plainText = cipher.process(cipherText);

  await File('decryptedFile').writeAsBytes(plainText, flush: true);
}

这篇关于用c#加密文件并在flutter中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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