OBJECT Codeunit 50099 RSA Management { OBJECT-PROPERTIES { Date=17/10/20; Time=12:20:31; Modified=Yes; Version List=RSA; } PROPERTIES { OnRun=VAR RSAcspPri@1000000000 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Security.Cryptography.RSACryptoServiceProvider"; RSAcspPub@1000000001 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Security.Cryptography.RSACryptoServiceProvider"; Txt2Encrypt@1000000002 : Text; TxtEncrypted@1000000003 : Text; TxtDecrypted@1000000004 : Text; Path2PEMFile@1000000006 : Text; BEGIN RSAcspPri := RSAcspPri.RSACryptoServiceProvider; Path2PEMFile := 'D:'; // Here the path to your PEM file ReadPEM(Path2PEMFile + '\private_unencrypted.pem', 0, RSAcspPri); Txt2Encrypt := 'INIT Text to encrypt ABCDE abcde - 1234567890 $%&/()¨?* ‚¡¢£ „‰‹” ¥¤€‡° END'; TxtEncrypted := RSAEncrypt(RSAcspPri, Txt2Encrypt); TxtDecrypted := RSADecrypt(RSAcspPri, TxtEncrypted); MESSAGE('Text to encrypt (%4): %1\\'+ 'Encrypted text (%5): %2\\'+ 'Text decrypted (%6): %3', Txt2Encrypt, TxtEncrypted, TxtDecrypted, STRLEN(Txt2Encrypt), STRLEN(TxtEncrypted), STRLEN(TxtDecrypted)); END; } CODE { PROCEDURE ReadPEM@1000000000(FilePath@1000000010 : Text;Type@1000000007 : 'Private,Public';VAR RSAcsp@1000000016 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Security.Cryptography.RSACryptoServiceProvider"); VAR PemReader@1000000003 : DotNet "'BouncyCastle.Crypto, Version=1.8.8.0, Culture=neutral, PublicKeyToken=0e99375e54769942'.Org.BouncyCastle.OpenSsl.PemReader"; KeyPair@1000000004 : DotNet "'BouncyCastle.Crypto, Version=1.8.8.0, Culture=neutral, PublicKeyToken=0e99375e54769942'.Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair"; DotNetUtilities@1000000005 : DotNet "'BouncyCastle.Crypto, Version=1.8.8.0, Culture=neutral, PublicKeyToken=0e99375e54769942'.Org.BouncyCastle.Security.DotNetUtilities"; reader@1000000006 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamReader"; RSAKeyParameters@1000000011 : DotNet "'BouncyCastle.Crypto, Version=1.8.8.0, Culture=neutral, PublicKeyToken=0e99375e54769942'.Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters"; FileMgt@1000000012 : Codeunit 419; ServerFile@1000000013 : Text; RSAPrivateParameters@1000000015 : DotNet "'BouncyCastle.Crypto, Version=1.8.8.0, Culture=neutral, PublicKeyToken=0e99375e54769942'.Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters"; RSAParameters@1000000008 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Security.Cryptography.RSAParameters"; BEGIN ServerFile := FileMgt.ServerTempFileName(''); ServerFile := FileMgt.UploadFileSilentToServerPath(FilePath, ServerFile); reader := reader.StreamReader(ServerFile); PemReader := PemReader.PemReader(reader); KeyPair := PemReader.ReadObject(); IF Type = Type::Private THEN BEGIN RSAPrivateParameters := KeyPair.Private; RSAParameters := DotNetUtilities.ToRSAParameters(RSAPrivateParameters); END ELSE BEGIN RSAKeyParameters := KeyPair; RSAParameters := DotNetUtilities.ToRSAParameters(RSAKeyParameters); END; RSAcsp.ImportParameters(RSAParameters); END; PROCEDURE RSAEncrypt@1000000001(RSAcsp@1000000000 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Security.Cryptography.RSACryptoServiceProvider";Txt2Encrypt@1000000001 : Text) : Text; VAR DotNetArray@1000000003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array"; DotNetType@1000000002 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Type"; Length@1000000004 : Integer; Paddings@1000000007 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Security.Cryptography.RSAEncryptionPadding"; TxtEncrypted@1000000008 : Text; Convert@1000000011 : DotNet "'mscorlib'.System.Convert"; Encoding@1000000010 : DotNet "'mscorlib'.System.Text.Encoding"; BEGIN DotNetType := DotNetType.GetType('System.Byte',FALSE); Length := STRLEN(Txt2Encrypt); DotNetArray := DotNetArray.CreateInstance(DotNetType, Length); DotNetArray := Encoding.UTF8.GetBytes(Txt2Encrypt); DotNetArray := RSAcsp.Encrypt(DotNetArray, Paddings.Pkcs1); TxtEncrypted := Convert.ToBase64String(DotNetArray); EXIT(TxtEncrypted); END; PROCEDURE RSADecrypt@1000000002(RSAcsp@1000000000 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Security.Cryptography.RSACryptoServiceProvider";Txt2Decrypt@1000000001 : Text) : Text; VAR DotNetArray@1000000011 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array"; DotNetType@1000000010 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Type"; Length@1000000009 : Integer; Paddings@1000000006 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Security.Cryptography.RSAEncryptionPadding"; TxtDecrypted@1000000005 : Text; Convert@1000000002 : DotNet "'mscorlib'.System.Convert"; Encoding@1000000003 : DotNet "'mscorlib'.System.Text.Encoding"; BEGIN DotNetType := DotNetType.GetType('System.Byte',FALSE); Length := STRLEN(Txt2Decrypt); DotNetArray := DotNetArray.CreateInstance(DotNetType, Length); DotNetArray := Convert.FromBase64String(Txt2Decrypt); DotNetArray := RSAcsp.Decrypt(DotNetArray, Paddings.Pkcs1); TxtDecrypted := Encoding.UTF8.GetString(DotNetArray); EXIT(TxtDecrypted); END; BEGIN END. } }
-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAs1ftQXhqoh328GUxLzsSRyteshWrZqQrqgrmpW+kKMgxOSe0 Bt6YpoUcSQCwIAqqvArsSwsqaLmTShC6XS+pTq+FAfyJa0ECwDRCYm9efwFF6O2L QlF518uCoc4LRPmFEOLi9dIwNfxKoCfVEgdQLmsUEkufBA474SJ8NrUZ39W8kYss QSndcOQlyfeF7ergYvfGML5YpX9L9uWAmjVAWPFF605AyqULBQoh7tpwFUzQqYXd HYimVJj4tYM5BXmOnvS/PFb/wtnfIr2sjqnQ99tSYNt1VvppBQ0vbMkJgteJp5o9 1l4eHDpPfFDqPBvnsCGCbsNaRaHWrLYEH9l8YQIDAQABAoIBADnwCeHAuRIug/wd dMpBVyJOzITTo0/Bcam5Y9HGQ045fk1smigt+TqyffTHmB6B0650COolbUnvybl/ tfUs1JFq+pktsGdJ1U2qlFegivKUwrUBKKAAGlCwmj8ThWh/A5H0+kVX5eNK4gDj XV45ppmYVh+i9ngk8QtGTMKPtxAsPYqnYsDb2Elq4q1eXN5/cwJS4WcZ8BFJWqJe 9rTJDsfEmij6CHS769CqV+eEwF84LNM0hdrpt6Igyk+c5aSNGgq48G5k1T/CZXvD N9YXtsw0+0csBKBjWJUnMnGZuALkDzcb6VyxfBmcbRMAnZnB7+Yy04DDa76AnLTE Gf0fHdECgYEA6cbLXJHrMVT90DlSDi/5Un/hMx/+ldoXNvpLmGgtJhP22ekzg/FC MT88CVbQw8CP1MUkZj5Mn3wTuUVe+pFVsVItyYxgsGeRBJIq3vqwxS2/wSegzC+W Cp0Xkakl/F2HUjZQ6oO+nRQcn5rnnENbK2C7/goufUBMsWTZk5j3x88CgYEAxGRx x7/mS1BWlwyYYcYETUwdo6GECgv1KnbmHlHb8y4PcA1GTETA6lrj/7YdBVMJ7vAF RDEGsmapkLAK1jU7Pfw2TtVhyHURXPspwefw9UzkMdgq5V1FmK5JMnqzCnpvrV5K C5bIzH5EZUJQ32D8l6WyTSoGCHp4XY/R0GvhVM8CgYEA3mXDk4Yx9FWBvPS7Dkov a3/Cswtt/AtlaGUHQ69fvEjj3vzOiGgda5JXwB66Lm/jpSwDa806wcKdVPUc3U0j 87RVYNdOKKPuPyX66PQe9QaH1O6pN3bRy7kU8fqz63jiwqBvrBq3XOIENQZ97pfZ hNPbJFt1TKy+CUkDADpXBtsCgYBBhmnYBZTmx/U8DH3DEXhZ+e1hrQALAhZYhrcc op9KMbIA5szvoXjkOFMd/DsOjbgguoBnUhwz5cDpf3L+KWCMu9u6zcMESNr0NPCn u6VPksoIP2GAiXAxEuVlLD8UXelo5K670yFH6VEUn92vygI87pHg4QaGSaZjQRr1 +SiDTQKBgQCKoZqtKosfd9W2ep/HKTb9pBdQnKfBYb85EFmj49/ZTqaNe2NpQqy3 qR0Ia22dYzD48ne/iTemzfzVIlq3/rZWZeSBSJs9mFL37V/2FkOgbTM1DvJVpRS8 5MPRRcUfDUUFcVif0+KCPQxpnnAA5ue2/mO7jB7dA+9eNurv3e0PJw== -----END RSA PRIVATE KEY-----
openssl genrsa -des3 -out private.pem 2048 openssl rsa -in private.pem -out private_unencrypted.pem -outform PEM
Answers
If you need to encrypt/decrypt the password using a private/public key you must to use an external program like gnuPG's Gpg4win (https://gnupg.org/index.html), and call it from NAV2016.
Regards.
Can you please explain how to use..
Srinivas.
Could you elaborate a little about this.
The remote site give to you a "public key" and you give to them another "public key" or how is the real process .
Regards.
HI @ftornero ,
We don't provide any kind of keys. we will get the keys from remote site/system and we need to use the same key for encryption or decryption.
Thanks.
Srinivas.
Ok, maybe you need to calculate a data hash with this key and SHA1 o similar.
Have you any documentation that you can share in this forum?
Regards.
Please check https://www.devglan.com/online-tools/rsa-encryption-decryption
I need to encrypt or decrypt in the same way.
Thanks.
Srinivas.
In the previous example you can't encrypt and decrypt with the same key, you encrypt with the public key and decrypt with the private key like I said in my first message.
So for this you need to generate a public/private key pair in your system and send the public key to the other part
They must to do the same and send to you the public key, with this public key you encrypt the information that you send to them and with your public key they encrypt the information that they send to you.
The private keys, that are not interchangend, are to decrypt de information in both sides.
Could you confirm please.
Regards.
Hi @ftornero ,
We have Public Key which is provided by the remote site. The same key is shown in the above screenshot.
Thanks.
Srinivas.
Try to use in NAV RSACryptoServiceProvider
https://docs.microsoft.com/ru-ru/dotnet/api/system.security.cryptography.rsacryptoserviceprovider?view=netframework-4.8
Here there is an example using a RSA private key to Encrypt and Decrypt.
You need an external DLL (BouncyCastle) that you can get here:
http://www.bouncycastle.org/csharp/
Or you can create a new one using OpenSSL and this commands:
Regards
Hi @ftornero ,
Thanks for your help.
We have Successfully Encrypted the Password using the above Code.
But we have Public Key so we have changed the key type to public.
Srinivas.
Well in this case, with only the public key, you will not being able to decrypt.
Regards.
Hi @ftornero ,
In my case just encryption is required and decryption will be done by remote party.
They just provided public key so I need to encrypt the data.
Thanks.
Srinivas.
Hello @ftornero , I have just stumbled upon this but I'm getting an error while trying to run the codeunit with sample pem file provided.
Error is as below stating "Cannot create instance of the following .NET object...and then proceeds to point out the PemReader variable". Kindly assist
Do you have get the DLL like it's pointed out ?:
Regards.
Let me provide information on what I have done:
1. Downloaded the DLL and placed it inside the NAV 2017 Add-ins folder (C:\Program Files (x86)\Microsoft Dynamics NAV\100\RoleTailored Client\Add-ins)
2. Compiled the codeunit you've provided within NAV 2017 and it compiled successfully after step 1 above.
3.
4. Run the codeunit after step 2 but then faced with the error shared.
P.S I have already downloaded the pem file shared and when codeunit is running, it breaks when it gets to this line highlighted below:
What could I be missing? Thank you for your help @ftornero
The DLL is running in the server so you need also to copy it in the folder "C:\Program Files\Microsoft Dynamics NAV\100\Service\Add-ins".
Regards
Your suggestion has worked. How can I extend this to encrypt a file as my requirement involves encrypting an xml file that is to be shared to a bank
I gess that the bank have some kind of standard encryption that you have to do, could you share more information about it ?.
Regards.