Answered by:
RSACryptoServiceProvider error with Windows Server 2016

Question
-
We migrate one Windows Server 2008 to server 2016.
Now I get an error at this script:
cls $key = (2,3,56,34,254
,222,1,1,2,23,42,54,33,233,1,34,2,7,6,5,35,43,6,6,6,6,6,6,31,33,60,23) $pass = Read-Host -AsSecureString $securepass = $pass |ConvertFrom-SecureString -Key $key $bytes = [byte[]][char[]]$securepass $csp = New-Object System.Security.Cryptography.CspParameters $csp.KeyContainerName = "SuperSecretProcessOnMachine" $csp.Flags = $csp.Flags -bor [System.Security.Cryptography.CspProviderFlags]::UseMachineKeyStore $rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider -ArgumentList 5120,$csp $rsa.PersistKeyInCsp = $true $encrypted = $rsa.Encrypt($bytes,$true) $encrypted | Export-Clixml 'C:\Temp\encrypted_ysastaginpro_PRE.txt' -Force
The error message is:
New-Object : Exception calling ".ctor" with "2" argument(s): "Object already exists. " At C:\Program Files\Staging\MESDI\Create_PSW_File_Poly.ps1:13 char:10 + ... $rsa = New-Object System.Security.Cryptography.RSACryptoServiceP ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "Encrypt" with "2" argument(s): "Bad Length. " At C:\Program Files\Staging\MESDI\Create_PSW_File_Poly.ps1:18 char:3 + $encrypted = $rsa.Encrypt($bytes,$true) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : CryptographicException
I hope somebody can help me, thx Horst
Thanks Horst MOSS 2007 Farm; MOSS 2010 Farm; TFS 2010; TFS 2013; IIS 7.5
- Edited by wuwu Monday, August 7, 2017 10:59 AM Add error message
Monday, August 7, 2017 10:57 AM
Answers
-
I found the solution.
Run the PS script as adminstrator
Thanks Horst MOSS 2007 Farm; MOSS 2010 Farm; TFS 2010; TFS 2013; IIS 7.5
- Marked as answer by wuwu Tuesday, August 8, 2017 6:03 AM
Tuesday, August 8, 2017 6:03 AM
All replies
-
It seems like this has to do with permissions.
Have a look at this thread, particularly the answer from mobile_scribe:
- Edited by FaustoNascimento Monday, August 7, 2017 1:19 PM
Monday, August 7, 2017 1:19 PM -
Also look at the solution presented here, which means you can easily code this into your script (answer from Steve Czetty):
https://stackoverflow.com/questions/4763628/object-already-exists-in-rsacryptoserviceprovider
Monday, August 7, 2017 1:22 PM -
I found the solution.
Run the PS script as adminstrator
Thanks Horst MOSS 2007 Farm; MOSS 2010 Farm; TFS 2010; TFS 2013; IIS 7.5
- Marked as answer by wuwu Tuesday, August 8, 2017 6:03 AM
Tuesday, August 8, 2017 6:03 AM