Json请求加密和解密

时间:2018-10-03 22:25:37

标签: encryption flutter flutter-layout

我有这个经过基本身份验证的Json请求,该请求不断返回此错误:引发了另一个异常: E / flutter(19791):[错误:flutter / shell / common / shell.cc(181)] Dart错误:未处理的异常: E / flutter(19791):FormatException:意外字符(在字符1处) E / flutter(19791):无法将JSON解组为DecryptRequest”。有人遇到同样的问题,如何解决?

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:http/http.dart' as http;
import 'package:pointycastle/api.dart';
import 'package:pointycastle/padded_block_cipher/padded_block_cipher_impl.dart';
import 'package:pointycastle/paddings/pkcs7.dart';
import 'package:pointycastle/block/aes_fast.dart';
import 'package:pointycastle/block/modes/cbc.dart'

Future<http.Response> post() async {
    var url = 'http:';
    String password = "";
    String username = "";

    var bytes = utf8.encode("$username:$password");
    Uint8List key = Uint8List.fromList(
      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
    );
    Uint8List iv = Uint8List.fromList(
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
    );
    String Json  =
    jsonEncode({'account': _account, 'phone': _mobile, 'amount': _amount});

    Uint8List plainText = Uint8List.fromList(utf8.encode(Json));

    PaddedBlockCipher cipher = PaddedBlockCipherImpl(
      PKCS7Padding(),
      CBCBlockCipher(AESFastEngine()),
    );
    cipher.init(
      true,
      PaddedBlockCipherParameters<CipherParameters, CipherParameters>(
        ParametersWithIV<KeyParameter>(KeyParameter(key), iv),
        null,
      ),
    );
    Uint8List cypherText = cipher.process(plainText);

    var credentials = base64.encode(bytes);
    var headers = {
      "Content-Type": "application/json",
      "Authorization": "Basic $credentials"
    };



    http.Response response =
    await http.post(url, body:  cypherText.toString(), headers: headers);
    var responseJson = json.decode(response.body);
    print(Utf8Codec().decode(response.bodyBytes));

    print("Body: " + responseJson);
  }

0 个答案:

没有答案