Coder Social home page Coder Social logo

Comments (3)

leocavalcante avatar leocavalcante commented on August 17, 2024

When not padded, inputs should be multiples of the block size. Can you share your input or an input that reproduces the error?

from encrypt.

Jethro87 avatar Jethro87 commented on August 17, 2024

Hi @leocavalcante 👋

Sure thing. I'm trying to encrypt a String of 'String'.

var encrypted = crypto.encrypt('String');
print(encrypted.base16);

I get this error: flutter: Another exception was thrown: RangeError (index): Index out of range: index should be less than 6: 6

On a higher level, what I'm trying to do is be able to encrypt/decrypt in my Flutter app (using encrypt) and my web app (using aesjs). Aesjs (using Aes.ctr), does not require padding as per their example below:

var key = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];

// Convert text to bytes
var text = 'Text may be any length you wish, no padding is required.';
var textBytes = aesjs.utils.utf8.toBytes(text);

// The counter is optional, and if omitted will begin at 1
var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));
var encryptedBytes = aesCtr.encrypt(textBytes);

// To print or store the binary data, you may convert it to hex
var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
console.log(encryptedHex);
// "a338eda3874ed884b6199150d36f49988c90f5c47fe7792b0cf8c7f77eeffd87
//  ea145b73e82aefcf2076f881c88879e4e25b1d7b24ba2788"

// When ready to decrypt the hex string, convert it back to bytes
var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);

// The counter mode of operation maintains internal state, so to
// decrypt a new instance must be instantiated.
var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));
var decryptedBytes = aesCtr.decrypt(encryptedBytes);

// Convert our bytes back into text
var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
console.log(decryptedText);
// "Text may be any length you wish, no padding is required."

If I encrypt the string 'String' in aesjs and print the base16 result, I get this:

0d957ea17dad

If I encrypt the string 'String' in encrypt without setting padding: null, I get this:

0d957ea17dad88c50c817dac320c1bbc

But if I set padding: null, I get this error: flutter: Another exception was thrown: RangeError (index): Index out of range: index should be less than 6: 6

Would you mind elaborating on your point, with respect to what I'm trying to do as per above? I'm quite new to encryption. Thanks for your time.

from encrypt.

Jethro87 avatar Jethro87 commented on August 17, 2024

I managed to figure this out. Ended up using CBC instead of CTR, with padding. Closing this issue.

from encrypt.

Related Issues (20)

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.