CredCraft.psm1

<#
.SYNOPSIS
         Unique key generation File based on which one can Encrypt a string that will generate a file also.
         Based on the encrypted file one can decrypt the string with the same Key File .
 
.DESCRIPTION
        Unique key generation File based on which one can Encrypt a string (UserId, or Password, etc) that will generate a Encrypted file also.
        Based on the Encrypted file generated one can decrypt the string (UserID or Password) with the same Key File generated earlier which are all placed in the physical drive path of the server or machine.
        One time Generation of Key File and the Encrypted UserID and Password generation files.
        Use multiple Times to Decrypt the Encrypted String in various applications in Powershell,
        provided you should pass appropriate parameter values to Decrupt files created during Encryption and Key generation.
 
.NOTES
        Step 1: Invoke function New-CredCraftKey with parameter values to Generate a key file and will be saved in the folder path provided. [This can be prepared and saved one-time for future use]
        Step 2: Invoke Function New-CredCraftEncrypt with proper parameters values to Encrypt a string provided with parameter value 'StringToEncrypt' ,
                that will generated a physical Encrypted file in the path provided. [This can be prepared and saved one-time for future use]
        Step 3: Invoke function Get-CredCraftDecrypt with proper parameter value to decrypt the Encrypted String provided in step 2
 
.EXAMPLE
    #Creating Key
    New-CredCraftKey -KeyPath "C:\MyFolder\MyCredentials\CredCraft" -KeyName "myKey.key"
 
.EXAMPLE
    # Encryption File Generation
    New-CredCraftEncrypt -KeyPath "C:\MyFolder\MyCredentials\CredCraft" -KeyName "mKey.key" -EncryptedPath "C:\MyFolder\MyCredentials\CredCraft" -EncryptedFileName "uMyUser" -StringToEncrypt "user555"
    New-CredCraftEncrypt -KeyPath "C:\MyFolder\MyCredentials\CredCraft" -KeyName "mKey.key" -EncryptedPath "C:\MyFolder\MyCredentials\CredCraft" -EncryptedFileName "pMyPass" -StringToEncrypt "xyz@7116Ut"
.EXAMPLE
    #Decryption Process
    $GetUser = Get-CredCraftDecrypt -KeyPath "C:\MyFolder\MyCredentials\CredCraft" -KeyName "mKey.key" -EncryptedPath "C:\MyFolder\MyCredentials\CredCraft" -EncryptedFileName "uMyUser"
    $GetPassword = Get-CredCraftDecrypt -KeyPath "C:\MyFolder\MyCredentials\CredCraft" -KeyName "mKey.key" -EncryptedPath "C:\MyFolder\MyCredentials\CredCraft" -EncryptedFileName "pMyPass"
 
#>



function New-CredCraftKey
{
  param(        
       [parameter(Position=0, Mandatory=$true)]
       [string]$KeyName,
       [parameter(Position=1,Mandatory=$true)]
       [string]$KeyPath        
   )

    $ext = $KeyName.split('.')[1]
    
    if($ext -eq $null)
    {
       $KeyName     = $KeyName + ".key" 
    }    
    
    $KeyFilePath     = Join-Path -Path $($KeyPath) -ChildPath "$KeyName"
      

    # Create Key File in separate process [One time Run] ################################################################################
    $KeyFile = "$KeyFilePath"
    $Key = New-Object Byte[] 32
    [Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
    $Key | out-file $KeyFile -Force

    Write-Output "Encrypted Key File Generated in the path: $KeyPath "
    ######################################################################################################################################
}


function New-CredCraftEncrypt
{
    param(
       [parameter(Position=0, Mandatory=$true)]
       [string]$KeyPath,
       [parameter(Position=1, Mandatory=$true)]
       [string]$KeyName,
       [parameter(Position=2, Mandatory=$true)]
       [string]$EncryptedPath,
       [parameter(Position=3, Mandatory=$true)]
       [string]$EncryptedFileName,
       [parameter(Position=4, Mandatory=$true)]
       [string]$StringToEncrypt        
   )
    
    $extUsr = $EncryptedFileName.split('.')[1]

    if($extUsr -eq $null)
    {
       $EncryptedFileName = $EncryptedFileName + ".sol" 
    }

    # Encrypting the user/Password in separate process [One time Run]
    
    $vault   =  Join-Path -Path $($EncryptedPath) -ChildPath "$EncryptedFileName"   
    $KeyFile =  Join-Path -Path $($KeyPath) -ChildPath "$KeyName" 
   
    $Key = Get-Content $KeyFile  
    $manupulatedString = $StringToEncrypt 

    $manupulatedString

    # save the User.
    convertto-securestring -string $manupulatedString -asplaintext -force | convertfrom-securestring -Key $key | out-file $vault -Force
    
    Write-Output "Encrypted User File Generated in the path: $EncryptedPath"
}



function Get-CredCraftDecrypt
{
   param( 
    [parameter(Position=0, Mandatory=$true)]
    [string]$KeyPath,
    [parameter(Position=1, Mandatory=$true)]
    [string]$KeyName,
    [parameter(Position=2, Mandatory=$true)]
    [string]$EncryptedPath,
    [parameter(Position=3, Mandatory=$true)]
    [string]$EncryptedFileName            
   )

        $extKey = $KeyName.split('.')[1]
        $extEncryptedFileName = $EncryptedFileName.split('.')[1]
    
        if($extKey -eq $null)
        {
           $KeyName     = $KeyName + ".key" 
        }
        
        if($extEncryptedFileName -eq $null)
        {
           $EncryptedFileName     = $EncryptedFileName + ".sol" 
        }
                 
        $vault1   = Join-Path -Path $($EncryptedPath) -ChildPath "$EncryptedFileName"  
        $KeyFile  = Join-Path -Path $($KeyPath) -ChildPath "$KeyName"
                  

        $Key = Get-Content $KeyFile        

        # retrieve the User.
        $securestring1 = convertto-securestring -string (get-content $vault1) -Key $Key
        $bstr1 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securestring1)
        $EncryptedOne1 = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr1)
        return $EncryptedOne1       

}

Export-ModuleMember -Function 'New-CredCraftKey','New-CredCraftEncrypt','Get-CredCraftDecrypt'