Forum Discussion

sferguson's avatar
sferguson
New Contributor
2 years ago

PGP Decryption Using BouncyCastle

Has anyone used BouncyCastle succesfully for PGP decryption? If so, would you mind sharing your BR?

Thank you!

 

 

    • sferguson's avatar
      sferguson
      New Contributor

      Thank you! Late yesterday, I was able to find a VB.Net solution that works within Visual Studio, but not within OneStream. I'm getting the following error on every line that declares a Stream type:

      Import of type 'Stream' from assembly or module 'System.Runtime.dll' failed. 


       

      • JackLacava's avatar
        JackLacava
        Community Manager

        There might be some naming clash, or some incompatibility of .Net versions.

        Worst case, you could build your BouncyCastle-requiring functionality as a separate executable, and execute it from the rule. Or build it as a DLL, then import and reference the DLL from the rule.

  • RobbSalzmann's avatar
    RobbSalzmann
    Valued Contributor II

    Here's a BR you can use for simple crypto that uses the .net framework crypto libraries.

    Imports System
    Imports System.Collections.Generic
    Imports System.Data
    Imports System.Data.Common
    Imports System.Globalization
    Imports System.IO
    Imports System.Linq
    Imports System.Windows.Forms
    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
    Imports System.Runtime.InteropServices
    Imports System.Security.Cryptography
    
    Namespace OneStream.BusinessRule.Extender.Crypto
    	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
    				Dim crypto As New Crypto()
    				Dim b64Key  As String = String.Empty 
                    Dim b64Vect As String = String.Empty 
    				Dim encrypted As String = crypto.AesEncrypt("RobbSalzmann", b64Key, b64Vect)
    				Dim decrypted As String = crypto.AesDecrypt(encrypted, b64Key, b64Vect)
    				
    				BRApi.ErrorLog.LogMessage(si, $"RobbSalzmann: {encrypted}")
    				BRApi.ErrorLog.LogMessage(si, $"{decrypted}: {encrypted}")
    				Return Nothing
    			Catch ex As Exception
    				Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
    			End Try
    		End Function
    	End Class
    
    	Public Class Crypto
    		Public Function AesEncrypt(plainText As String, <Out> ByRef aesKey As String, <Out> ByRef aesInitVect As String) As String
    			Using aes As Aes = Aes.Create()
    				aesKey = Convert.ToBase64String(aes.Key)
    				aesInitVect = Convert.ToBase64String(aes.IV)
    				Dim cryptpoXform As ICryptoTransform = aes.CreateEncryptor()
    				Dim cryptoBytes As Byte()
    
    				Using memoryStream As MemoryStream = New MemoryStream()
    					Using cryptoStream As CryptoStream = New CryptoStream(memoryStream, cryptpoXform, CryptoStreamMode.Write)
    						Using streamWriter As StreamWriter = New StreamWriter(cryptoStream)
    							streamWriter.Write(plainText)
    						End Using
    						cryptoBytes = memoryStream.ToArray()
    					End Using
    				End Using
    
    				Return Convert.ToBase64String(cryptoBytes)
    			End Using
    		End Function
    		
    		Public Function AesDecrypt(encrypted As String, b64Key As String, b64Vect As String) As String
    		    Using aes As Aes = Aes.Create()
    		        aes.Key = Convert.FromBase64String(b64Key)
    		        aes.IV = Convert.FromBase64String(b64Vect)
    		        Dim cryptoXform As ICryptoTransform = aes.CreateDecryptor()
    		        Dim cipherBytes As Byte() = Convert.FromBase64String(encrypted)
    
    		        Using memoryStream As MemoryStream = New MemoryStream(cipherBytes)
    		            Using cryptoStream As CryptoStream = New CryptoStream(memoryStream, cryptoXform, CryptoStreamMode.Read)
    		                Using streamReader As StreamReader = New StreamReader(cryptoStream)
    		                    Return streamReader.ReadToEnd()
    		                End Using
    		            End Using
    		        End Using
    		    End Using
    		End Function
    	End Class
    End Namespace