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 NewCredCraftKey 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 NewCredCraftEncrypt 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 GetCredCraftDecrypt with proper parameter value to decrypt the Encrypted String provided in step 2
 
.EXAMPLE
    #Creating Key
    NewCredCraftKey -KeyPath "C:\MyFolder\MyCredentials\CredCraft" -KeyName "myKey.key"
 
.EXAMPLE
    # Encryption File Generation
    NewCredCraftEncrypt -KeyPath "C:\MyFolder\MyCredentials\CredCraft" -KeyName "mKey.key" -EncryptedPath "C:\MyFolder\MyCredentials\CredCraft" -EncryptedFileName "uMyUser" -StringToEncrypt "user555"
    NewCredCraftEncrypt -KeyPath "C:\MyFolder\MyCredentials\CredCraft" -KeyName "mKey.key" -EncryptedPath "C:\MyFolder\MyCredentials\CredCraft" -EncryptedFileName "pMyPass" -StringToEncrypt "xyz@7116Ut"
.EXAMPLE
    #Decryption Process
    $GetUser = GetCredCraftDecrypt -KeyPath "C:\MyFolder\MyCredentials\CredCraft" -KeyName "mKey.key" -EncryptedPath "C:\MyFolder\MyCredentials\CredCraft" -EncryptedFileName "uMyUser"
    $GetPassword = GetCredCraftDecrypt -KeyPath "C:\MyFolder\MyCredentials\CredCraft" -KeyName "mKey.key" -EncryptedPath "C:\MyFolder\MyCredentials\CredCraft" -EncryptedFileName "pMyPass"
 
#>



function NewCredCraftKey
{
  param(        
       [parameter(Position=0, Mandatory=$true,HelpMessage="Enter a Key Name of your choice with .key Extension")]
       [string]$KeyName,
       [parameter(Position=1,Mandatory=$true,HelpMessage="Enter Only the physical Path of the Key File to be generated")]
       [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 NewCredCraftEncrypt
{
    param(
       [parameter(Position=0, Mandatory=$true,HelpMessage="Enter Only the physical Path of the Key File residing")]
       [string]$KeyPath,
       [parameter(Position=1, Mandatory=$true,HelpMessage="Enter Only the Key File Name with extension")]
       [string]$KeyName,
       [parameter(Position=2, Mandatory=$true,HelpMessage="Enter Only the physical Path where Encrypted File will reside")]
       [string]$EncryptedPath,
       [parameter(Position=3, Mandatory=$true,HelpMessage="Enter Only the Name of the Encrypted File to be generated")]
       [string]$EncryptedFileName,
       [parameter(Position=4, Mandatory=$true,HelpMessage="Enter the text value (Password or UserId,etc) to Encrypt")]
       [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 File Generated in the path: $EncryptedPath"
}



function GetCredCraftDecrypt
{
   param( 
    [parameter(Position=0, Mandatory=$true,HelpMessage="Enter Only the physical Path of the Key File residing")]
    [string]$KeyPath,
    [parameter(Position=1, Mandatory=$true,HelpMessage="Enter Only the Key File Name with extension")]
    [string]$KeyName,
    [parameter(Position=2, Mandatory=$true,HelpMessage="Enter Only the Encrypted File Path")]
    [string]$EncryptedPath,
    [parameter(Position=3, Mandatory=$true,HelpMessage="Enter Only the Encrypted File Name with Extension that needs to be Decrypted")]
    [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 NewCredCraftKey,NewCredCraftEncrypt,GetCredCraftDecrypt