CipherGen constructor
CipherGen({
- required int seed,
Implementation
CipherGen({required int seed}){
//Calculate p and q
String password = seed.toString();
if(password.length == 0 || password == null){
throw CipherError.noPassword;
}else if(password.length == 1){
_pValue = int.parse(password);
_pValue = _pValue!.isPrime()? _pValue : OptimusPrime.primeAfter(_pValue!);
_qValue = (_pValue! + 1).isPrime()? (_pValue! + 1) : OptimusPrime.primeAfter(_pValue! + 1);
}else{
int midpoint = (password.length / 2).ceil();
_pValue = int.parse(password.substring(0,midpoint));
//Make it prime if it is not
_pValue = _pValue!.isPrime() ? _pValue : OptimusPrime.primeAfter(_pValue!);
//Parse q
_qValue = int.parse(password.substring(midpoint,password.length));
//Make it prime
_qValue = _qValue!.isPrime() ? _qValue : OptimusPrime.primeAfter(_qValue!);
//Make sure that p and q are never the same
if(_pValue == _qValue){
_qValue = OptimusPrime.primeAfter(_qValue!);
}
}
//print('p = $p \nq = $q');
//Find the largest value on the password
int largestNumber = _pValue! < _qValue! ? _qValue! : _pValue!;
int smallestNumber = _pValue! < _qValue! ? _pValue! : _qValue!;
//Calculate N
_N = _pValue! * _qValue!;
//print('N = $N');
//Calculate phi Φ
_phi = (_pValue! - 1) * (_qValue! - 1);
//print('phi = $phi');
//Calculate e(Int between 1 and phi)
//print('Largest int on the list is $largestNumber');
//Make sure that e is coprime with N and phi
List<int> possibleE = [];
for(int i = 2; i < phi!; i++){
if(i.coprimeWith(phi!) && i.coprimeWith(N!)){
possibleE.add(i);
}
}
//There are no numbers that can take the value of the variable e. None within range meet the criteria.
if(possibleE.length == 0){
throw CipherError.smallPassword;
}
int whichPosition = ((smallestNumber / largestNumber) * (possibleE.length - 1)).round();
_e = possibleE[whichPosition];
//print('e = $e');
//Choose a d
//Find the number n time where e * d (mod phi) == 1
_d = e!.modInverse(phi!);
//print('d = $d');
//End of key variables generation
}