Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🔒 A set of high-level APIs over PointyCastle for two-way cryptography.

License

NotificationsYou must be signed in to change notification settings

leocavalcante/encrypt

Repository files navigation

Pub PackageDart CIDonate

A set of high-level APIs over PointyCastle for two-way cryptography.

Looking for password hashing? Please, visitpassword.

Secure random

You can generate cryptographically secure random keys and IVs for you project.

Activate the encrypt package:

pub global activate encrypt

Then use thesecure-random command-line tool:

$ secure-randomCBoaDQIQAgceGg8dFAkMDBEOECEZCxgMBiAUFQwKFhg=

You can set the length and the base output.

$ secure-random --help-l, --length       The length of the bytes                   (defaults to"32")-b, --base         Bytes represented as base 64 or base 16 (Hexdecimal)                   (defaults to"64")-h, --[no-]help    Show thishelp message

Algorithms

Current status is:

  • AES with PKCS7 padding
  • RSA with PKCS1 and OAEP encoding
  • Salsa20

Signing

  • SHA256 with RSA

Usage

Symmetric

AES

import'package:encrypt/encrypt.dart';voidmain() {final plainText='Lorem ipsum dolor sit amet, consectetur adipiscing elit';final key=Key.fromUtf8('my 32 length key................');final iv=IV.fromLength(16);final encrypter=Encrypter(AES(key));final encrypted= encrypter.encrypt(plainText, iv: iv);final decrypted= encrypter.decrypt(encrypted, iv: iv);print(decrypted);// Lorem ipsum dolor sit amet, consectetur adipiscing elitprint(encrypted.base64);// R4PxiU3h8YoIRqVowBXm36ZcCeNeZ4s1OvVBTfFlZRdmohQqOpPQqD1YecJeZMAop/hZ4OxqgC1WtwvX/hP9mw==}
Modes of operation

Default mode is SICAESMode.sic, you can override it using themode named parameter:

final encrypter=Encrypter(AES(key, mode:AESMode.cbc));
Supported modes are:
  • CBCAESMode.cbc
  • CFB-64AESMode.cfb64
  • CTRAESMode.ctr
  • ECBAESMode.ecb
  • OFB-64/GCTRAESMode.ofb64Gctr
  • OFB-64AESMode.ofb64
  • SICAESMode.sic
No/zero padding

To remove padding, passnull to thepadding named parameter on the constructor:

final encrypter=Encrypter(AES(key, mode:AESMode.cbc, padding:null));

Salsa20

import'package:encrypt/encrypt.dart';voidmain() {final plainText='Lorem ipsum dolor sit amet, consectetur adipiscing elit';final key=Key.fromLength(32);final iv=IV.fromLength(8);final encrypter=Encrypter(Salsa20(key));final encrypted= encrypter.encrypt(plainText, iv: iv);final decrypted= encrypter.decrypt(encrypted, iv: iv);print(decrypted);// Lorem ipsum dolor sit amet, consectetur adipiscing elitprint(encrypted.base64);// CR+IAWBEx3sA/dLkkFM/orYr9KftrGa7lIFSAAmVPbKIOLDOzGwEi9ohstDBqDLIaXMEeulwXQ==}
import'package:encrypt/encrypt.dart';import'dart:convert';voidmain() {final plainText='Lorem ipsum dolor sit amet, consectetur adipiscing elit';final key=Key.fromUtf8('my32lengthsupersecretnooneknows1');final b64key=Key.fromUtf8(base64Url.encode(key.bytes).substring(0,32));// if you need to use the ttl feature, you'll need to use APIs in the algorithm itselffinal fernet=Fernet(b64key);final encrypter=Encrypter(fernet);final encrypted= encrypter.encrypt(plainText);final decrypted= encrypter.decrypt(encrypted);print(decrypted);// Lorem ipsum dolor sit amet, consectetur adipiscing elitprint(encrypted.base64);// random cipher textprint(fernet.extractTimestamp(encrypted.bytes));// unix timestamp}

Asymmetric

RSA

import'dart:io';import'package:encrypt/encrypt.dart';import'package:pointycastle/asymmetric/api.dart';voidmain() {final publicKey=awaitparseKeyFromFile<RSAPublicKey>('test/public.pem');final privKey=awaitparseKeyFromFile<RSAPrivateKey>('test/private.pem');final plainText='Lorem ipsum dolor sit amet, consectetur adipiscing elit';final encrypter=Encrypter(RSA(publicKey: publicKey, privateKey: privKey));final encrypted= encrypter.encrypt(plainText);final decrypted= encrypter.decrypt(encrypted);print(decrypted);// Lorem ipsum dolor sit amet, consectetur adipiscing elitprint(encrypted.base64);// kO9EbgbrSwiq0EYz0aBdljHSC/rci2854Qa+nugbhKjidlezNplsEqOxR+pr1RtICZGAtv0YGevJBaRaHS17eHuj7GXo1CM3PR6pjGxrorcwR5Q7/bVEePESsimMbhHWF+AkDIX4v0CwKx9lgaTBgC8/yJKiLmQkyDCj64J3JSE=}

Signature and verification

RSA

final publicKey=awaitparseKeyFromFile<RSAPublicKey>('test/public.pem');final privateKey=awaitparseKeyFromFile<RSAPrivateKey>('test/private.pem');final signer=Signer(RSASigner(RSASignDigest.SHA256, publicKey: publicKey, privateKey: privateKey));print(signer.sign('hello world').base64);print(signer.verify64('hello world','jfMhNM2v6hauQr6w3ji0xNOxGInHbeIH3DHlpf2W3vmSMyAuwGHG0KLcunggG4XtZrZPAib7oHaKEAdkHaSIGXAtEqaAvocq138oJ7BEznA4KVYuMcW9c8bRy5E4tUpikTpoO+okHdHr5YLc9y908CAQBVsfhbt0W9NClvDWegs='));

[8]ページ先頭

©2009-2025 Movatter.jp