0

I have the following JAVA and I am trying to convert into C# using ChilKat (or BouncyCastle) I have a working version in ChilKat, but not sure how to validate Here is the JAVA code:

private SecretKey symmKey = null;
public String encrypt(String strToEncrypt) throws  Exception
 {

     String symmEncryptMode = "DESede";
     String encString= null;

     KeyGenerator keyGen = KeyGenerator.getInstance(symmEncryptMode);
     symmKey = keyGen.generateKey();

     byte dataToEncrypt[] = strToEncrypt.getBytes();
     Cipher symmCipher = Cipher.getInstance(symmEncryptMode);
     symmCipher.init(Cipher.ENCRYPT_MODE, symmKey);
     byte[] encrypted  = symmCipher.doFinal(dataToEncrypt);
     encString= new String(Base64.encode(encrypted));
     encString = URLEncoder.encode(encString, "UTF-8");
     return(encString);
} //end method create Signature

Here is what I have so far (It returns a value, but I don't know how to validate as this is one of three steps of my encyption process - step 3 works, 1 and 2 are suspect, so I figured I'd ask one at a time...) This uses ChilKat and it returns a value, but I am not sure if it is correct:

  private static string EncryptStringSymmetric(string data2Encrypt, ref string passKey)
        {
            //Init Encryptor
            Crypt2 encryptor = new Crypt2();
            bool success = encryptor.UnlockComponent("Anything for 30 - day trial");
            if (success != true)
            { throw (new Exception("Crypt component unlock failed")); }

            //Encryptor Settings
            encryptor.CryptAlgorithm = "3des";
            encryptor.KeyLength = 192;
            encryptor.EncodingMode = "base64";
            encryptor.PaddingScheme = 0;
            encryptor.Charset = "utf-8";
            encryptor.CipherMode = "ecb";
            encryptor.RandomizeKey();
            passKey = encryptor.GetEncodedKey("base64");

            string eStr;

            //byte[] bytesToEncrypt = Encoding.ASCII.GetBytes(data2Encrypt);
            //eStr = encryptor.EncryptBytesENC(bytesToEncrypt);//??
            eStr = encryptor.EncryptStringENC(data2Encrypt);
            return eStr;
        }
Danimal111
  • 1,976
  • 25
  • 31
  • 1
    As an obvious first step, see if one component can decrypt what the other encrypts. The Java code unfortunately uses defaults for charsets, and defaults for the mode and padding parts of the encryption transformation. Never use defaults if they are not portable, and thesre defaults aren't. Also, DESede is a two-key variants of triple DES which isn't likely to match what your doing in C#. – President James K. Polk Oct 25 '16 at 00:07
  • It de-crypts just fine... it's just that I have a 3 step process that I need to follow... this is step 1 and I think you have hit on a huge issue.. trying http://stackoverflow.com/questions/29341828/c-sharp-triple-des-encryption-with-two-keys now... it's checked as correct, but no up votes.. hopefully I will be able to add an up vote! – Danimal111 Oct 25 '16 at 00:17

0 Answers0