Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrypt failed? #1

Open
hmmhmmhm opened this issue May 11, 2018 · 7 comments
Open

Decrypt failed? #1

hmmhmmhm opened this issue May 11, 2018 · 7 comments

Comments

@hmmhmmhm
Copy link

hmmhmmhm commented May 11, 2018

No description provided.

@nanov
Copy link
Member

nanov commented May 11, 2018

Would you like provide a little bit more information? :)

@hmmhmmhm
Copy link
Author

hmmhmmhm commented May 11, 2018

Hmm... could you check this code?

I trying to use this module
but factor.js throw error like this

'Could not factor n for use in FPE, prime numbers cannot be used as modulu'

Sadly I am not good at math :p

▼ Here is my codes.

import fe1 from 'node-fe1-fpe'
class InnerFPE {
	static encrypt(min, max, current, privateTweak, publicTweak){
		// for avoid prime number crash issue
		try{
                         // (1) this code works
			return fe1.encrypt(max+1-min, current+1, privateTweak, publicTweak)+min
		} catch(e){
                        // (2) it also works
			return fe1.encrypt((max+2-min), current+2, privateTweak, publicTweak)+min
		}
	}
	static decrypt(min, max, current, privateTweak, publicTweak){
		// for avoid prime number crash issue
		try{
                        // (3) it also  works
			return fe1.decrypt(max+1-min, current, privateTweak, publicTweak)-1+min
		} catch(e){
                        // (4) i don't guess here
		}
	}
}

// it connected (4)
let encrypted = InnerFPE.encrypt(9, 15, 1, 'my-secret-key', 'my-non-secret-tweak')
let decrypted = InnerFPE.decrypt(9, 15, encrypted, 'my-secret-key', 'my-non-secret-tweak')

@hmmhmmhm hmmhmmhm reopened this May 11, 2018
@nanov
Copy link
Member

nanov commented May 11, 2018

The way i see it you have two options here :

Something like this :

const fe1 = require('node-fe1-fpe');

class InnerFPE {
	static encrypt(min, max, current, privateTweak, publicTweak) {
		if (current < min || current > max)
			throw new Error(`Current numebr must be between ${min} and ${max}`);
		const modulu = max - min + 1;
		return fe1.encrypt(modulu, current - min, privateTweak, publicTweak) + min;
	}
	static decrypt(min, max, current, privateTweak, publicTweak) {
		const modulu = max - min + 1;
		return fe1.decrypt(modulu, current - min, privateTweak, publicTweak) + min;
	}
}

const encrypted = InnerFPE.encrypt(9, 16, 10, 'my-secret-key', 'my-non-secret-tweak');
const decrypted = InnerFPE.decrypt(9, 16, encrypted, 'my-secret-key', 'my-non-secret-tweak');

Or the far better, in my opinion, option :

const fe1 = require('node-fe1-fpe');

class InnerFPE {
	constructor(min, max, privateTweak, publicTweak) {
		this.min = min;
		this.max = max;
		this.privateTweak = privateTweak;
		this.publicTweak = publicTweak;
		this.modulu = max - min + 1;
	}

	encrypt(current) {
		// invalid number
		if (current < this.min || current > this.max)
			throw new Error(`Current numebr must be between ${this.min} and ${this.max}`);
		return fe1.encrypt(this.modulu, current - this.min, this.privateTweak, this.publicTweak) + this.min;
	}
	decrypt(current) {
		// invalid number
		if (current < this.min || current > this.max)
			throw new Error(`Current numebr must be between ${this.min} and ${this.max}`);
		return fe1.decrypt(this.modulu, current - this.min, this.privateTweak, this.publicTweak) + this.min;
	}
}

const innerFPE = new InnerFPE(9, 16, 'my-secret-key', 'my-non-secret-tweak');
const encrypted = innerFPE.encrypt(10);
const decrypted = innerFPE.decrypt(encrypted);

Please bear in mind, that the modulu cannot be a prime number ( a number that can be divided only by itself and 1 ), and the ranges you are experimenting are probably too low. And the specific range that you want ( 9 to 15, 15 - 9 + 1 = 7, which is a prime number ) is invalid.
This could be probably solved with some kind of shift when the modulu is prime.

The check that I've added here to see if the number falls in the defined range is a something that I've missed in the library and will be added, so this check will be redundant.

This should be also probably documented, but because of time limitanions it is still to be done.

Is it a little bit clearer now?

@hmmhmmhm
Copy link
Author

hmmhmmhm commented May 11, 2018

oh... my! goooood! thanks!

@hmmhmmhm
Copy link
Author

@nanov

oops, i founded same issue

// It can be passed
const innerFPE = InnerFPE.encrypt(9, 16, 10, 'my-secret-key', 'my-non-secret-tweak');

// But it can't be passed
const innerFPE = InnerFPE.encrypt(1, 83, 1, 'my-secret-key', 'my-non-secret-tweak');

'Could not factor n for use in FPE, prime numbers cannot be used as modulu'

Same error printed in my console. can you check detail?

@nanov
Copy link
Member

nanov commented May 12, 2018

Well, 83-1+1 = 83 which is a prime number. As said modulu cannot be a prime number, this is an algorithm limitation.

If only the incoming number must be in that range you could change the modulu ( to lets say 100, this means that the decrypted result will be between 0-99, the format of max. two digits is preserved ), then you could check the range before encrypting.

Let me put it this way, although the modulu can be used ( with some limitations ) be used in the way you are trying, it is intended to be used to enforce the format, not the range. This means that it is intended to be 10the number of digits in your format.

For example if you are encrypting a credit card number which consists of 15 digits ( when the checksum is removed ), your modulu would be 1015.

Going back to your case, your format ( judging from the range ) is two digits, this would make your modulu 102 = 100. Enforcing the range of the incoming number is totally up to you, while the range of the encrypted number would be different, but the format will be preserved, which is what this algorithm is intended for, hence the name Format Preserving Algorithm.

@hmmhmmhm
Copy link
Author

hmmhmmhm commented Jun 9, 2023

@nanov
Hi, I know it's been a while, but this library is needed again and I've developed an additional library that exceptionally handles this issue! Thanks again for your open source development...! 😊
https://github.com/hmmhmmhm/pseudo-shuffle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants