Coder Social home page Coder Social logo

dart_des's Introduction

dart_des

This algorithm is a pure dart implementation of the DES and Triple DES algorithms (port of pyDES). Triple DES is either DES-EDE3 with a 24 byte key, or DES-EDE2 with a 16 byte key.

Example

import 'dart:convert';

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

main() {
  String key = '12345678'; // 8-byte
  String message = 'Driving in from the edge of town';
  List<int> encrypted;
  List<int> decrypted;
  List<int> iv = [1, 2, 3, 4, 5, 6, 7, 8];

  print('key: $key');
  print('message: $message');

  DES desECB = DES(key: key.codeUnits, mode: DESMode.ECB);
  encrypted = desECB.encrypt(message.codeUnits);
  decrypted = desECB.decrypt(encrypted);
  print('DES mode: ECB');
  print('encrypted: $encrypted');
  print('encrypted (hex): ${hex.encode(encrypted)}');
  print('encrypted (base64): ${base64.encode(encrypted)}');
  print('decrypted: $decrypted');
  print('decrypted (hex): ${hex.encode(decrypted)}');
  print('decrypted (utf8): ${utf8.decode(decrypted)}');

  key = '123456781234567812345678'; // 24-byte
  DES3 des3CBC = DES3(key: key.codeUnits, mode: DESMode.CBC, iv: iv);
  encrypted = des3CBC.encrypt(message.codeUnits);
  decrypted = des3CBC.decrypt(encrypted);
  print('Triple DES mode: CBC');
  print('encrypted: $encrypted');
  print('encrypted (hex): ${hex.encode(encrypted)}');
  print('encrypted (base64): ${base64.encode(encrypted)}');
  print('decrypted: $decrypted');
  print('decrypted (hex): ${hex.encode(decrypted)}');
  print('decrypted (utf8): ${utf8.decode(decrypted)}');
}

dart_des's People

Contributors

akash98sky avatar delletenebre avatar francisco-diniz avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dart_des's Issues

can dart_des 1.0.0 compatible 0.0.2 ?

hi sir,

I'am working on a project that phone communicate with stm32 MCU by bluetooth(BLE) ,
I encrypted byte arrays with dart_des 0.0.2 , it encrypted 8bytes data and got 8bytes encrypted data, I like it because I don't want the encrypted data length got too long, but now the dart_des 1.0.0 is different, it encrypt 8 bytes data and got 16 bytes data, it's hard to send data to stm32 mcu, so could I want to know it's possible to compatible with 0.0.2 version and also get null safety feature ?

Thanks in advance .

kitty

Padding mode

In the case that I'm working I need to use padding mode but I don't see how to use it in the example code. Can you tell me how to use it if I need to use PKCS7 padding mode?

16 byte key doesn't work

I'm having a problem regarding to the length of the key. At the case that I'm currently working on I have to use 16 byte key, but I keep getting an exception that the key must be 8 byte long even though that I've followed the exact same snippet from the documentation

RangeError (index): Invalid value: Not in inclusive range 0..63: 64

when my sourceStr is Chinese, it will be appeard this error。

mycode

`
test("register2", () async {
var map = "猫";
String encryptDart = await EncryptUtil.encryptDartDes(map.toString(), "");
print(encryptDart);
});

static String encryptDartDes(String srcStr, String dateStr) {
String keyStr = "12345678";
final key = keyStr.codeUnits;
final iv = keyStr.codeUnits;
DES desCBC = DES(key: key, mode: DESMode.CBC, iv: iv, paddingType: DESPaddingType.PKCS7);

final encrypted = desCBC.encrypt(srcStr.codeUnits);
return base64.encode(encrypted);

}`

errors:
`

dart:core List.[]
package:dart_des/dart_des.dart 1009:27 DES.xor
package:dart_des/dart_des.dart 1105:19 DES.crypt
package:dart_des/dart_des.dart 1126:7 DES.encrypt
package:project_flutter/utils/encrypt_util.dart 46:30 EncryptUtil.encryptDartDes
test\api_test.dart 40:44 main.
test\api_test.dart 38:21 main.

RangeError (index): Invalid value: Not in inclusive range 0..63: 64

`

encrypt error

DES3 des3CBC = DES3(key: 'PDXDzTttJ1F4lgEsoR4xPB9U'.codeUnits, mode: DESMode.CBC, iv: utf8.encode('OoWrs7by'));
final encryptData = des3CBC.encrypt('{"keyword":"hello","page":"1","userToken":""}'.codeUnits);
String encryptString = base64Encode(encryptData);
print(encryptString);

with this code, I get error

@delletenebre
please help me~ thanks so much!!

i have a problem,this result has wrong

image
top is right answer
image
"K�SQDUW_ryName":"天然气","changePrice":0.00000,"dayChgRange":0.00000,"endDate":1715702400000,"frequencyType":0,"monthChgRange":0.00000,"price":"1.84","quarterChgRange":0.00000,"remark":"","startDate":1536595200000,"targetId":9573,"targetName":"天然气门站沈阳","unit":"元/立方米","updateDate":1715702400000,"weekChgRange":0.00000,"yearBeginChgRange":0.00000,"yearChgRange":0.00000}������" this is package answer

Wrong decrypt result

Hi guys,

I can decrypt the message but it always has some strange characters at the end such as ��

I have function like this:

Future<String> decryptTDES(String data, String password) async {
      List<int> iv = [0, 0, 0, 0, 0, 0, 0, 0];
      List<int> decrypted;
      DES3 des3CBC = DES3(key: md5.convert(utf8.encode(password)).bytes, mode: DESMode.CBC, iv: iv);
      decrypted = des3CBC.decrypt(base64.decode(data));
      return utf8.decode(decrypted);
  }

And it also takes so long time (~30-45s) to decrypt a message 1200 characters (base64 format)
Can someone help me?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.