maythamfahmi / CryptoNet

CryptoNet is simple, fast and a lightweight asymmetric and symmetric encryption library.
https://github.com/maythamfahmi/CryptoNet
MIT License
97 stars 19 forks source link

Why can't I decrypt successfully without writing all the content in the same method? #49

Closed simphonydeveloper closed 4 months ago

simphonydeveloper commented 6 months ago

Environment: win10 .NET VERSION: .NET6 Application: WebAPI

These my generate key method.

           ICryptoNet cryptoNet = new CryptoNetRsa();
            string PrivateKeyFile = Path.Combine(AppContext.BaseDirectory, "privateKey");
            string PublicKeyFile = Path.Combine(AppContext.BaseDirectory, "publicKey.pub");
            cryptoNet.ExportKeyAndSave(new FileInfo(PrivateKeyFile), true);
            cryptoNet.ExportKeyAndSave(new FileInfo(PublicKeyFile), false);

encrypt code

 ICryptoNet cryptoNetPubKey = new CryptoNetRsa(new FileInfo(key));
            var encrypt = cryptoNetPubKey.EncryptFromString("1");

            return Convert.ToBase64String(encrypt);

decrypt code

 ICryptoNet cryptoNetPriKey = new CryptoNetRsa(new FileInfo(key));
 var decrypt = cryptoNetPriKey.DecryptToString(Convert.FromBase64String(data));
 return decrypt;
maythamfahmi commented 4 months ago

Hi, @simphonydeveloper Thanks for sharing your question.

you can not just Convert.ToBase64String(encrypt) and convert back to a string. It won't work how you think. I am not sure why you are doing this.

This will work

ICryptoNet cryptoNet = new CryptoNetRsa();
string PrivateKeyFile = Path.Combine(AppContext.BaseDirectory, "privateKey");
string PublicKeyFile = Path.Combine(AppContext.BaseDirectory, "publicKey.pub");
cryptoNet.ExportKeyAndSave(new FileInfo(PrivateKeyFile), true);
cryptoNet.ExportKeyAndSave(new FileInfo(PublicKeyFile), false);

ICryptoNet cryptoNetPubKey = new CryptoNetRsa(new FileInfo(key));
var encrypt = cryptoNetPubKey.EncryptFromString("1");
return encrypt;

ICryptoNet cryptoNetPriKey = new CryptoNetRsa(new FileInfo(key));
var decrypt = cryptoNetPriKey.DecryptToString(data);
return decrypt;

if you need to encrypt bytes and decrypt bytes you can do the following:

ICryptoNet cryptoNetPubKey = new CryptoNetRsa(new FileInfo(key));
var encrypt = cryptoNetPubKey.EncryptFromBytes("1");
return encrypt;

ICryptoNet cryptoNetPriKey = new CryptoNetRsa(new FileInfo(key));
var decrypt = cryptoNetPriKey.DecryptToBytes(data);
return decrypt;

However, if you need to do some base 64 conversion, you should encode that before the encryption process and after the decryption process. Here is an example:

for encryption part

var base64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("1"));
var encrypt = cryptoNetPubKey.EncryptFromString(base64);

for decryption part

var decrypt = cryptoNetPriKey.DecryptToString(data);
var output = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(decrypt));

Let me know 😃

maythamfahmi commented 4 months ago

@simphonydeveloper I am going to close this. Suppose it does solve your issue. If not create a new question.

simphonydeveloper commented 4 months ago

thanks you.