konstantinullrich / crypton

A simple Dart library for asymmetric encryption and digital signatures
https://pub.dev/packages/crypton
MIT License
34 stars 12 forks source link

FormatException: Bad UTF-8 encoding #13

Closed murali-shris closed 4 years ago

murali-shris commented 4 years ago

Code:

void main() {
  var rsaKeypair = RSAKeypair.fromRandom();
  var message = 'test message';
  var signature = rsaKeypair.privateKey.createSHA256Signature(utf8.encode(message));
  var signatureStr = utf8.decode(signature);
  var verified = rsaKeypair.publicKey.verifySHA256Signature(utf8.encode(message), utf8.encode(signatureStr));
/*rsaKeypair.publicKey.verifySHA256Signature(utf8.encode(message), signature);This works fine. But I need the signature in  string format to be used in my application. */
  print(verified);
}

Output:

Unhandled exception:
FormatException: Bad UTF-8 encoding 0x8f (at offset 2)
#0      _Utf8Decoder.convert (dart:convert/utf.dart:532:13)
#1      Utf8Decoder.convert (dart:convert/utf.dart:329:13)
#2      Utf8Codec.decode (dart:convert/utf.dart:61:56)

I upgraded from version 1.0.6 to 1.1.0. I changed createSignature (deprecated) to createSHA256Signature.

konstantinullrich commented 4 years ago

Let me take a look

konstantinullrich commented 4 years ago

The reason for your problem is, that the result of createSHA256Signature is not a UTF-8 encoded string.

Here is your code bit working.

import 'dart:convert';
import 'package:crypton/crypton.dart';

void main() {
  var rsaKeypair = RSAKeypair.fromRandom();
  var message = 'test message';
  var signature = rsaKeypair.privateKey.createSHA256Signature(utf8.encode(message));
  var signatureStr = base64Encode(signature);
  var verified = rsaKeypair.publicKey.verifySHA256Signature(utf8.encode(message), base64Decode(signatureStr));
  print(verified);
}
konstantinullrich commented 4 years ago

I'll upload a fixed version of the deprecated RSA function createSignature and verifySignature to pub.dev

Does that resolve your issue?

murali-shris commented 4 years ago

The reason for your problem is, that the result of createSHA256Signature is not a UTF-8 encoded string.

Here is your code bit working.

import 'dart:convert';
import 'package:crypton/crypton.dart';

void main() {
  var rsaKeypair = RSAKeypair.fromRandom();
  var message = 'test message';
  var signature = rsaKeypair.privateKey.createSHA256Signature(utf8.encode(message));
  var signatureStr = base64Encode(signature);
  var verified = rsaKeypair.publicKey.verifySHA256Signature(utf8.encode(message), base64Decode(signatureStr));
  print(verified);
}

I'll upload a fixed version of the deprecated RSA function createSignature and verifySignature to pub.dev

Does that resolve your issue?

yes it does.. Thanks a lot for your quick response.

konstantinullrich commented 4 years ago

:)