Tag Archive for: pgp

Understanding PGP Encryption in OneStream

In today’s digital world, privacy is more important than ever. Whether you’re sending an email, storing sensitive files, or communicating securely, encryption plays a vital role. One of the most trusted methods for securing digital communication is PGP encryption—short for Pretty Good Privacy. But what exactly is PGP, and how does it work?

What Is PGP Encryption?

PGP is an encryption program that provides cryptographic privacy and authentication for data communication. It was created by Phil Zimmermann in 1991 and has since become a standard for secure email and file encryption.  PGP uses a combination of symmetric-key encryption and public-key encryption to protect data. This hybrid approach makes it both secure and efficient.

How PGP Encryption Works

PGP operates using two types of keys:

  • Public Key: Shared with others so they can encrypt messages intended for you.
  • Private Key: Kept secret and used to decrypt messages sent to you.

Here’s a simplified breakdown of the process:

Encryption:

The sender uses the recipient’s public key to encrypt the message.
Only the recipient’s private key can decrypt it.

Decryption:

The recipient uses their private key to decrypt the message.
Since only the recipient has the private key, no one else can read the message.

Digital Signatures:

PGP also allows users to digitally sign messages.
This verifies the sender’s identity and ensures the message hasn’t been tampered with.


Onestream Sample Code

 

' *******************************************************************************
' Name:
' Description: Helper Function to Decrypt/Encrypt a file using the private key
'
'
' *******************************************************************************

Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.Common
Imports System.Globalization
Imports System.IO
Imports System.Linq
Imports Microsoft.VisualBasic
Imports OneStream.Finance.Database
Imports OneStream.Finance.Engine
Imports OneStream.Shared.Common
Imports OneStream.Shared.Database
Imports OneStream.Shared.Engine
Imports OneStream.Shared.Wcf
Imports OneStream.Stage.Database
Imports OneStream.Stage.Engine

' *******************************************************************************
' Required Non Standard Libraries
' *******************************************************************************

Imports Didisoft.pgp
Imports DidiSoft.Pgp.Exceptions

Namespace OneStream.BusinessRule.Extender.PGPDEcrypt
Public Class MainClass
Public Function Main(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As ExtenderArgs) As Object
    Try
'Testing File for Function
    Dim configSettings As AppServerConfigSettings = AppServerConfig.GetSettings(si)
    Dim inputFileLocation As String =
    Dim outputFileLocation As String =
    
'Run the decryption using the Private Key
    decrypt(si,inputFileLocation,outputFileLocation)
    
'Run the decryption using the Private Key
    Encrypt(si,inputFileLocation,outputFileLocation)
    
    Catch e As IOException
    BRApi.ErrorLog.LogMessage(si, " error")
End Try
Return 1
End Function

Public Function Decrypt(ByVal si As SessionInfo, ByVal inputFileLocation As String,ByVal outputFileLocation As String) As Object
    
    Try
' create an instance of the library
    Dim configSettings As AppServerConfigSettings = AppServerConfig.GetSettings(si)
    Dim pgp As New PGPLib()
    Dim PGPKeyFile As String = "my Private key"
    Dim PGPPassphrase As String = "mypassphrase"
    Dim PGPKeyLocation As String = "location" & PGPKeyFile
    
    BRApi.ErrorLog.LogMessage(si, "Attempting To Decrypt - "& inputFileLocation & " To " & outputFileLocation)
    pgp.DecryptFile(inputFileLocation , PGPKeyLocation , PGPPassphrase, outputFileLocation)
    
    
    Catch e As IOException
    BRApi.ErrorLog.LogMessage(si, " Error reading input Or writing output")
    Catch e As PGPException
    If TypeOf e Is NonPGPDataException Then
        BRApi.ErrorLog.LogMessage(si, " the passed encrypted input Is Not a valid OpenPGP archive")
    ElseIf TypeOf e Is IntegrityCheckException Then
        BRApi.ErrorLog.LogMessage(si, " the passed encrypted input Is corrupted")
    ElseIf TypeOf e Is FileIsPBEEncryptedException Then
        BRApi.ErrorLog.LogMessage(si, " the passed encrypted input Is encrypted With a password but we try To decrypt it With a Private key")
    ElseIf TypeOf e Is WrongPrivateKeyException Then
        BRApi.ErrorLog.LogMessage(si, " the encrypted input was encrypted With a different Private key than the provided one")
    ElseIf TypeOf e Is WrongPasswordException Then
        BRApi.ErrorLog.LogMessage(si, " the password For the provided Private key Is wrong")
    Else
        BRApi.ErrorLog.LogMessage(si, "  general decryption Error Not among the above ones ")
    End If
End Try
Return 0
End Function

Public Function Encrypt(ByVal si As SessionInfo, ByVal inputFileLocation As String,ByVal outputFileLocation As String) As Object
    Try
    Dim configSettings As AppServerConfigSettings = AppServerConfig.GetSettings(si)
    Dim pgp As New PGPLib()
    Dim PGPKeyFile As String = "their Public key"
    Dim asciiArmor As Boolean = FALSE
    Dim withIntegrityCheck As Boolean = TRUE
    Dim PGPKeyLocation As String = "location" & PGPKeyFile
    
    BRApi.ErrorLog.LogMessage(si, "Attempting To Encrypt - "& inputFileLocation & " To " & outputFileLocation)
    pgp.EncryptFile(inputFileLocation , PGPKeyLocation , outputFileLocation,asciiArmor,withIntegrityCheck)
    
    If System.IO.File.Exists(outputFileLocation) Then
' File exists
        BRApi.ErrorLog.LogMessage(si, "Encryption Success: " & outputFileLocation)
    Else
        BRApi.ErrorLog.LogMessage(si, "Encryption Failure Output File does Not exist")
    End If
    
    Catch e As IOException
    BRApi.ErrorLog.LogMessage(si, " Error reading input Or writing output")
    Catch e As PGPException
    If TypeOf e Is NonPGPDataException Then
        BRApi.ErrorLog.LogMessage(si, " the passed encrypted input Is Not a valid OpenPGP archive")
    ElseIf TypeOf e Is IntegrityCheckException Then
        BRApi.ErrorLog.LogMessage(si, " the passed encrypted input Is corrupted")
    ElseIf TypeOf e Is FileIsPBEEncryptedException Then
        BRApi.ErrorLog.LogMessage(si, " the passed encrypted input Is encrypted With a password but we try To decrypt it With a Private key")
    ElseIf TypeOf e Is WrongPrivateKeyException Then
        BRApi.ErrorLog.LogMessage(si, " the encrypted input was encrypted With a different Private key than the provided one")
    ElseIf TypeOf e Is WrongPasswordException Then
        BRApi.ErrorLog.LogMessage(si, " the password For the provided Private key Is wrong")
    Else
        BRApi.ErrorLog.LogMessage(si, "  general decryption Error Not among the above ones ")
    End If
    
End Try
Return 0
End Function

End Class
End Namespace