-
Notifications
You must be signed in to change notification settings - Fork 1
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
Comments
Would you like provide a little bit more information? :) |
Hmm... could you check this code? I trying to use this module '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.
|
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. 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? |
oh... my! goooood! thanks! |
oops, i founded same issue // It can be passed // But it can't be passed '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? |
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. |
@nanov |
No description provided.
The text was updated successfully, but these errors were encountered: