I am currently implementing my own RSA encryption using modular exponentiation. Sometimes when testing, I noticed that if I decrypt a value and then re-encrypt it, the same thing doesn't come out again. Important: This only sometimes happens; however, I don't see any similarities to these values, so I can't see a pattern or any other determining factor.
My code looks like this:
import 'package:encrypt/encrypt.dart' as enc;
import 'package:nanodart/nanodart.dart';
enc.Encrypted textBookRSAEncryption(String message, RSAPublicKey publicKey) {
var messageInt = utils.decodeBigInt(message.codeUnits);
var mInt = NanoHelpers.byteToBigInt(Uint8List.fromList(message.codeUnits));
var cipherInt = mInt.modPow(publicKey.exponent!, publicKey.modulus!);
enc.Encrypted encrypted =
enc.Encrypted(NanoHelpers.bigIntToBytes(cipherInt));
return encrypted;
}
String asymmetricDecryptOwnY(enc.Encrypted encrypted) {
Uint8List cipherBytes = encrypted.bytes;
var decInt = NanoHelpers.byteToBigInt(cipherBytes);
var plainInt = decInt.modPow(
privateSignatureKey.privateExponent!, publicSignatureKey.modulus!);
Uint8List test = NanoHelpers.bigIntToBytes(plainInt);
String plainText = String.fromCharCodes(test.toList());
return plainText;
}
I always use a public and private RSA key for the exponents and modulars.
My test looks like the follow:
void testEncDec() {
String b64 = "+6enUUoaHdnwfF+thVlhjkUEAEzFX4EIELFJQMXGcU2gc5btvmkRLkdO1JYbJyoxrVvMglQ7hyr3jaMh7EFiorMZ/U7+fuhK7/FZ69gKaF/85oczUUafjS/peePweYswv2BzrjIu8YL3v7yXpu1aeC8zautgba412UNVlKeQxZDz+SfCq/sxY+Ebr2N35Q4E//C4e2FGtAdrTWhZg/bgCTB3auL0FEqdfAi5EWOZ6sMo926hWUGd0iqHy5C/0EJvjcCqiKKJ+NX0vleg2/JVDZcZBqMa3e7uJVeOLq3fyp4n7FxlpWyBUpq4fy+UnVX+bda7i6bo2lK1jOdj3sbcjA==";
String zwei = "7FmCm6p2dyGBZjEeT6FZu3hIXPMKFikySHH51F9XQhG23wa1PMPhrH2s4Gh2FG727nV40z9jZeQdSCLY/Yl+c3DtVPXlA9NpAnf7p3Xsum+K9Qo/oOYVNf+/sViCHJFLWkE4cnou6Ks8RSDVUjScUul+HdoaWhMJnQV9f5Jo8GDIgrHaeo63JUO8aencsHFtPHqAQCXq+TOPfmOtYTTX3+SHLs9L7uHeX8yQiRqLlVpK1Fy/+YpBiRCJ+bebWJTYHjotLnTwctDZo7MeI051ZCp3hFMamoCA8GUJm66yg3hHjkJb3tiY8sToEUQniOeuR8Mcr61DLg7pRpSrz82W9Q==";
enc.Encrypted y1 = enc.Encrypted.fromBase64(b64);
BigInt y_1 = NanoHelpers.byteToBigInt(y1.bytes);
enc.Encrypted y2 = enc.Encrypted.fromBase64(zwei);
BigInt y_2 = NanoHelpers.byteToBigInt(y2.bytes);
String x1 = asymmetricDecryptOwnY(y1);
String x2 = asymmetricDecryptOwnY(y2);
enc.Encrypted newY1 = textBookRSAEncryption(x1, publicSignatureKey);
enc.Encrypted newY2 = textBookRSAEncryption(x2, publicSignatureKey);
print("Are they the same?: ${newY2.bytes == y2.bytes && newY1.bytes == y1.bytes}");
}
For these two Base64 encoded strings the same value does not come out again. However, it works for example for the following:
String works = "Rzqnl9n5wBLE77AWYk5KjiwgrUtBHrbE9l9/HHgZ7Baey6k+vI7nIJRw5qSA/gVIQtx7LXp182YT5SOzpH9rAR52Dzkd+S1Pekt5ehkAMhgRNvczD4m/sDU4PZ42+vtLnivgMPvI5Mb82UB0al1OMuRuuWjjHEE61eH2VjudMoSRsxM/fy7clmg49wnJye4kiiTZ0Y52Ma41R2P48RTi/4MDHBSbxV9gWsyucQDUOQwWpPztiknYuHBthIhWaZotzfaPbbzGhWQCf73Gxe/P0TW7sDeo5++3JjJ7noJgJxDbUE935jHNd7NFeVmvBHxQjHLm35YFK87h4P9Hb7QpRA=="
I cannot find any patterns, so maybe anyone of you might finde some.
As requested in the comments, the Hex representation of y1 and x1.
y1 = 0xfba7a7514a1a1dd9f07c5fad8559618e4504004cc55f810810b14940c5c6714da07396edbe69112e474ed4961b272a31ad5bcc82543b872af78da321ec4162a2b319fd4efe7ee84aeff159ebd80a685ffce6873351469f8d2fe979e3f0798b30bf6073ae322ef182f7bfbc97a6ed5a782f336aeb606dae35d9435594a790c590f3f927c2abfb3163e11baf6377e50e04fff0b87b6146b4076b4d685983f6e00930776ae2f4144a9d7c08b9116399eac328f76ea159419dd22a87cb90bfd0426f8dc0aa88a289f8d5f4be57a0dbf2550d971906a31addeeee25578e2eaddfca9e27ec5c65a56c81529ab87f2f949d55fe6dd6bb8ba6e8da52b58ce763dec6dc8c
x1 = 0x92c25e3e643c053c5ab61ce0b675e42bc5c27c437583860311fe3a84250b640df032e08f3348a84a366e96d6e473081629efc1fc1b1d1666b0a2ede5f70c8932c5c7c18736f189b75479e223742f58379bd3a17949aed0d5a0b8c511f058364fc5d601f8f867531daacfcb10c67b08b53b5ca07d7b4432222f80f317dfb085397cf4d86ad52052c47fc0d2f848f51d2b5af3a12ee1bd180634d32dbec8dbc0342020ff240e579feabfa747890f2293ff05ab19f7c8d5d6ba9d6f1538960c73d2e61fdbd0747e7c8f21843f6ccd15d72b26f01dadfeb0654c1a466622268cc96b5a3e7de791d9f114b8fd5b9828f3db2a3990b90aa3918d13b77efbc220495299
After multiple tests I found, that every time the encrypted hex value starts with the digits 9-f (e.g.: y1 starts with f), the decryption is not correct. But when it starts with 0-8, everything is calculated correctly...
Kind regards, Bagheera