Encrypt() public static method

public static Encrypt ( byte bytes, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048 ) : byte[]
bytes byte
publicKey RSAParameters
rsaKeyLength RsaKeyLengths
return byte[]
Example #1
0
        public EncryptedMessage CreateEncryptedMessage(object request, string operationName, byte[] cryptKey, byte[] authKey, byte[] iv, string verb = null)
        {
            this.PopulateRequestMetadata(request);

            if (verb == null)
            {
                verb = HttpMethods.Post;
            }

            var cryptAuthKeys = cryptKey.Combine(authKey);

            var rsaEncCryptAuthKeys     = RsaUtils.Encrypt(cryptAuthKeys, PublicKey);
            var authRsaEncCryptAuthKeys = HmacUtils.Authenticate(rsaEncCryptAuthKeys, authKey, iv);

            var timestamp   = DateTime.UtcNow.ToUnixTime();
            var requestBody = timestamp + " " + verb + " " + operationName + " " + request.ToJson();

            var encryptedBytes     = AesUtils.Encrypt(requestBody.ToUtf8Bytes(), cryptKey, iv);
            var authEncryptedBytes = HmacUtils.Authenticate(encryptedBytes, authKey, iv);

            var encryptedMessage = new EncryptedMessage
            {
                KeyId = KeyId,
                EncryptedSymmetricKey = Convert.ToBase64String(authRsaEncCryptAuthKeys),
                EncryptedBody         = Convert.ToBase64String(authEncryptedBytes),
            };

            return(encryptedMessage);
        }
        public EncryptedMessage CreateEncryptedMessage(object request, string operationName, SymmetricAlgorithm aes, string verb = null)
        {
            this.PopulateRequestMetadata(request);

            var aesKeyBytes = aes.Key.Combine(aes.IV);

            var rsaEncAesKeyBytes = RsaUtils.Encrypt(aesKeyBytes, PublicKey);

            if (verb == null)
            {
                verb = HttpMethods.Post;
            }

            var timestamp = DateTime.UtcNow.ToUnixTime();

            var requestBody = timestamp + " " + verb + " " + operationName + " " + JsonServiceClient.ToJson(request);

            var encryptedMessage = new EncryptedMessage
            {
                EncryptedSymmetricKey = Convert.ToBase64String(rsaEncAesKeyBytes),
                EncryptedBody         = AesUtils.Encrypt(requestBody, aes.Key, aes.IV)
            };

            return(encryptedMessage);
        }
 public static string Decrypt(string privateKeyXml, string encryptedData, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     return(RsaUtils.Encrypt(encryptedData, privateKeyXml, rsaKeyLength));
 }
 public static string Encrypt(string publicKeyXml, string data, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     return(RsaUtils.Encrypt(data, publicKeyXml, rsaKeyLength));
 }