Exemplo n.º 1
0
        public static string ViettelEncryption(string inputText, string viettelPublicKey, string customerPrivateKey)
        {
            string encryptedSMSText = "";

            try
            {
                //string privateKeyConek = @"MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQg1OGD/UuZ3FO9u6e60TmOJne47RWWYGqH7FIzRvxxhruhRANCAAQKJjarvVgOWJz6eg5ncv5WucbRBR2/qJqXpe5Yo9a9NQ7gzOgRBvejgj2q2NFNo9lgZgeFXl4fuoqOiLCGawaO";
                //string publicKeyViettel = @"MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEe9YVVfiuEbTC8MLFcmettM9z6i1bh49kM97NZJ0yLCZcDQtWhuQ230W/nackSySWO8tErAzDWiMdvEACaHevaA==";

                //PublicKey Viettel
                var base64PubKeyStr = viettelPublicKey; // "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEe9YVVfiuEbTC8MLFcmettM9z6i1bh49kM97NZJ0yLCZcDQtWhuQ230W/nackSySWO8tErAzDWiMdvEACaHevaA==";
                var publicKeyBytes  = Convert.FromBase64String(base64PubKeyStr);
                ECPublicKeyParameters otherPartyPublicKey = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(publicKeyBytes);
                Console.WriteLine(otherPartyPublicKey.ToString());

                //PrivateKey
                var base64PrivateKeyStr           = customerPrivateKey; // "MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQgnFkCkg26s2pwWa1oV0cYFUUqU0sCDeYP2wRpUQcu74WhRANCAASDBA7GheF0EFqZY8xhQSXhYog6pEEtOBIo64eFBT2JZsPZysxk5i+PQXfVhkvib/tTLag6MuNiL451+u9qPbg5";
                var privateKeyBytes               = Convert.FromBase64String(base64PrivateKeyStr);
                ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(privateKeyBytes);
                Console.WriteLine(privateKey.ToString());

                //Make share key
                IBasicAgreement aKeyAgree = AgreementUtilities.GetBasicAgreement("ECDH");
                aKeyAgree.Init(privateKey);
                BigInteger sharedSecret      = aKeyAgree.CalculateAgreement(otherPartyPublicKey);
                byte[]     sharedSecretBytes = sharedSecret.ToByteArrayUnsigned();
                var        keyparam          = ParameterUtilities.CreateKeyParameter("AES", sharedSecretBytes);

                var    plainText     = inputText; // "Hello! This is an banksms message!";
                byte[] plainTextByte = Encoding.UTF8.GetBytes(plainText);
                var    bankSmsIv     = "Banksms@Viettel!";
                byte[] iv            = Encoding.UTF8.GetBytes(bankSmsIv);

                var parametersWithIV = new ParametersWithIV(keyparam, iv);
                Console.WriteLine(Encoding.ASCII.GetString(parametersWithIV.GetIV()));
                Console.WriteLine(parametersWithIV.ToString());

                var cipher = CipherUtilities.GetCipher("AES/CBC/PKCS5Padding");
                cipher.Init(true, parametersWithIV);
                byte[] output = cipher.DoFinal(plainTextByte);

                Console.WriteLine(string.Concat(output.Select(b => b.ToString("X2"))));
                encryptedSMSText = string.Concat(output.Select(b => b.ToString("X2")));  // Nội dung sau khi được mã hóa
            } catch (Exception ex)
            {
                Console.WriteLine("Exception ex = " + ex.Message);
            }


            return(encryptedSMSText);
        }