<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic encrypt credentials to create datasource - PowerShell in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/encrypt-credentials-to-create-datasource-PowerShell/m-p/2342114#M34435</link>
    <description>&lt;P&gt;I'm trying to automate gateway data source management using PowerShell. First I need to encrypt credentials. Here is my PowerShell script to do this;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;$segmentLength = 85
$encryptedLength = 128
$plaintTxt = '{"credentialData":[{"value":"'+$Username+'","name":"username"},{"value":"'+$Password+'","name":"password"}]}'
$rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider ($encryptedLength * 8)
$parameters = $rsa.ExportParameters($false)
$parameters.Exponent = [System.Convert]::FromBase64String($GatewayExponent)
$parameters.Modulus = [System.Convert]::FromBase64String($GatewayModulus)
$rsa.ImportParameters($parameters)
$plainTextArray = [System.Text.Encoding]::UTF8.GetBytes($plaintTxt)    
$hasIncompleteSegment = $plainTextArray.Length % $segmentLength -ne 0

$segmentNumber = If (-not $hasIncompleteSegment) {[int]($plainTextArray.Length / $segmentLength)} Else {[int]($plainTextArray.Length / $segmentLength) + 1}
$encryptedData = [System.Byte[]]::CreateInstance([System.Byte],$segmentNumber * $encryptedLength)
[int]$encryptedDataPosition = 0;

For ($i=0; $i -lt $segmentNumber; $i++) {
    $lengthToCopy = If ($i -eq ($segmentNumber - 1) -and $hasIncompleteSegment) {$plainTextArray.Length % $segmentLength} Else {$segmentLength}
    $segment = [System.Byte[]]::CreateInstance([System.Byte],$lengthToCopy)
    [System.Array]::Copy($plainTextArray,$i*$segmentLength,$segment,0,$lengthToCopy)
    $segmentEncryptedResult = $rsa.Encrypt($segment, $true)
    [System.Array]::Copy($segmentEncryptedResult,0,$encryptedData,$encryptedDataPosition,$segmentEncryptedResult.Length)
    $encryptedDataPosition += $segmentEncryptedResult.Length;
}

return [System.Convert]::ToBase64String($encryptedData)&lt;/LI-CODE&gt;&lt;P&gt;The solution described on this page stopped working - on the end of 2019&lt;/P&gt;&lt;P&gt;We're receiving the error that the&lt;BR /&gt;"Destination array was not long enough. Check destIndex and length, and the array's lower bounds"&lt;BR /&gt;on the line "Array.Copy(segmentEncryptedResult, 0, encryptedData, encryptedDataPosition, segmentEncryptedResult.Length);"&lt;/P&gt;&lt;P&gt;Do you have any fix writing in PowerShell? I have found sth in cs:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://github.com/microsoft/PowerBI-CSharp/commit/83ba6891704a412bfc22d7086b55a9c826454e5e" target="_blank" rel="noopener"&gt;microsoft/PowerBI-CSharp@83ba689&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I will be apricated for any hint. Thanks!&lt;/P&gt;</description>
    <pubDate>Wed, 16 Feb 2022 12:37:10 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2022-02-16T12:37:10Z</dc:date>
    <item>
      <title>encrypt credentials to create datasource - PowerShell</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/encrypt-credentials-to-create-datasource-PowerShell/m-p/2342114#M34435</link>
      <description>&lt;P&gt;I'm trying to automate gateway data source management using PowerShell. First I need to encrypt credentials. Here is my PowerShell script to do this;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;$segmentLength = 85
$encryptedLength = 128
$plaintTxt = '{"credentialData":[{"value":"'+$Username+'","name":"username"},{"value":"'+$Password+'","name":"password"}]}'
$rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider ($encryptedLength * 8)
$parameters = $rsa.ExportParameters($false)
$parameters.Exponent = [System.Convert]::FromBase64String($GatewayExponent)
$parameters.Modulus = [System.Convert]::FromBase64String($GatewayModulus)
$rsa.ImportParameters($parameters)
$plainTextArray = [System.Text.Encoding]::UTF8.GetBytes($plaintTxt)    
$hasIncompleteSegment = $plainTextArray.Length % $segmentLength -ne 0

$segmentNumber = If (-not $hasIncompleteSegment) {[int]($plainTextArray.Length / $segmentLength)} Else {[int]($plainTextArray.Length / $segmentLength) + 1}
$encryptedData = [System.Byte[]]::CreateInstance([System.Byte],$segmentNumber * $encryptedLength)
[int]$encryptedDataPosition = 0;

For ($i=0; $i -lt $segmentNumber; $i++) {
    $lengthToCopy = If ($i -eq ($segmentNumber - 1) -and $hasIncompleteSegment) {$plainTextArray.Length % $segmentLength} Else {$segmentLength}
    $segment = [System.Byte[]]::CreateInstance([System.Byte],$lengthToCopy)
    [System.Array]::Copy($plainTextArray,$i*$segmentLength,$segment,0,$lengthToCopy)
    $segmentEncryptedResult = $rsa.Encrypt($segment, $true)
    [System.Array]::Copy($segmentEncryptedResult,0,$encryptedData,$encryptedDataPosition,$segmentEncryptedResult.Length)
    $encryptedDataPosition += $segmentEncryptedResult.Length;
}

return [System.Convert]::ToBase64String($encryptedData)&lt;/LI-CODE&gt;&lt;P&gt;The solution described on this page stopped working - on the end of 2019&lt;/P&gt;&lt;P&gt;We're receiving the error that the&lt;BR /&gt;"Destination array was not long enough. Check destIndex and length, and the array's lower bounds"&lt;BR /&gt;on the line "Array.Copy(segmentEncryptedResult, 0, encryptedData, encryptedDataPosition, segmentEncryptedResult.Length);"&lt;/P&gt;&lt;P&gt;Do you have any fix writing in PowerShell? I have found sth in cs:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://github.com/microsoft/PowerBI-CSharp/commit/83ba6891704a412bfc22d7086b55a9c826454e5e" target="_blank" rel="noopener"&gt;microsoft/PowerBI-CSharp@83ba689&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I will be apricated for any hint. Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 12:37:10 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/encrypt-credentials-to-create-datasource-PowerShell/m-p/2342114#M34435</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-02-16T12:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: encrypt credentials to create datasource - PowerShell</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/encrypt-credentials-to-create-datasource-PowerShell/m-p/2350018#M34527</link>
      <description>&lt;P&gt;Hi @Anonymous&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See if this sample will help you:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/microsoft/PowerBI-Developer-Samples/blob/master/PowerShell%20Scripts/bindToGateway.ps1" target="_self"&gt;https://github.com/microsoft/PowerBI-Developer-Samples/blob/master/PowerShell%20Scripts/bindToGateway.ps1&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Liang&lt;BR /&gt;If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Feb 2022 05:48:08 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/encrypt-credentials-to-create-datasource-PowerShell/m-p/2350018#M34527</guid>
      <dc:creator>V-lianl-msft</dc:creator>
      <dc:date>2022-02-21T05:48:08Z</dc:date>
    </item>
    <item>
      <title>Re: encrypt credentials to create datasource - PowerShell</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/encrypt-credentials-to-create-datasource-PowerShell/m-p/3186355#M42177</link>
      <description>&lt;P&gt;Hello, here is my function, i found some code in the past available from internet.&lt;/P&gt;&lt;P&gt;In this function, it assumes that you already have imported the MicrosoftPowerBiMgmt modules&lt;/P&gt;&lt;P&gt;You need to download the Microsoft.PowerBI.Api.dll in order to call some code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;#
.Synopsis
   Encrypt the credentials against a DataGateway
#&amp;gt;
function Get-EncryptedCredentialDetails
{
    param
    (
        # The Username
        [parameter(Mandatory=$true)][string]$Username,
        # The Password
        [parameter(Mandatory=$true)][string]$Password,
        # The DataGateway ID
        [parameter(Mandatory=$true)][string]$DataGatewayId
    )

    [System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\Microsoft\Microsoft.PowerBI.Api.dll")

    $publicKey = (Invoke-PowerBIRestMethod -Url gateways/$($DataGatewayId) -Method Get | ConvertFrom-Json).publicKey
    $gatewayPublicKey = [Microsoft.PowerBI.Api.Models.GatewayPublicKey]::new($publicKey.exponent, $publicKey.modulus)
    $basicCredentials = [Microsoft.PowerBI.Api.Models.Credentials.BasicCredentials]::new($Username, $Password)
    $credentialsEncryptor = [Microsoft.PowerBI.Api.Extensions.AsymmetricKeyEncryptor]::new($gatewayPublicKey)

    [Microsoft.PowerBI.Api.Models.CredentialDetails]::new(
        $basicCredentials,
        [Microsoft.PowerBI.Api.Models.PrivacyLevel]::Organizational,
        [Microsoft.PowerBI.Api.Models.EncryptedConnection]::Encrypted,
        $credentialsEncryptor)
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2023 13:32:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/encrypt-credentials-to-create-datasource-PowerShell/m-p/3186355#M42177</guid>
      <dc:creator>Sébastien</dc:creator>
      <dc:date>2023-04-13T13:32:50Z</dc:date>
    </item>
  </channel>
</rss>

