B4J Library Encryption library and .NET

Discussion in 'B4J Libraries & Classes' started by Juanet, Jul 8, 2015.

  1. Juanet

    Juanet New Member Licensed User

    Hi,

    I have a problem when try to get the same results on B4J and .NET using the agraham Encryption library, if somebody can help me with this i will really appreciate.

    Here is my B4J code:
    Code:
    ' This one return: BBGWT0W/Tw8=
    Public Sub Encrypt As String
       
    Dim su As StringUtils 
       
    Dim c As Cipher
       c.Initialize(
    "DESEDE/CBC/PKCS5Padding")
       c.InitialisationVector = su.DecodeBase64(
    "yKoS+M16uDU=")
       
    Dim Kg As KeyGenerator
       Kg.Initialize(
    "DESEDE")
       Kg.KeyFromBytes(su.DecodeBase64(
    "QzWZyG+DImE="))
       
    Return su.EncodeBase64(c.Encrypt(su.DecodeBase64("1"), Kg.key, True))
    End Sub
    And here is my .NET code:
    Code:
    ' this one fails,
    Function Encrypt() As String
       
    Dim result As String = ""
       
    Dim data As Byte() = New UTF8Encoding(False).GetBytes("1")
       
    Dim des As New TripleDESCryptoServiceProvider()
       des.IV = Convert.FromBase64String(
    "yKoS+M16uDU=")
       des.Key = Convert.FromBase64String(
    "QzWZyG+DImE="' => here is the problem, key is to short
       result = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(data, 0, data.Length))
       des.Clear()
       
    Return result
    End Function
    Thank you so much.
     
    Last edited: Jul 9, 2015
  2. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.

    Your B4J code uses PKCS5Padding to pad the key as needed. You need to do the same thing in the .Net code.
     
  3. Juanet

    Juanet New Member Licensed User

    Hi Erel,

    First of thank you for yours apps that brings devs life more easy.

    I'm studying Java docs and compare with the MS docs for how to apply correctly the same algorithms, modes and padding with both technologies, also i see that BouncyCastle libs maybe can help me with that.

    Sorry for that, I go to edit my fisrt post to put the code tags.

    Thanks again.
     
Loading...