All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.security.encryption.TripleDESEncryption Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alachisoft.ncache.security.encryption;

import com.alachisoft.ncache.runtime.encryption.Encryption;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

public class TripleDESEncryption implements Encryption {
    //private static TripleDESCryptoServiceProvider s_des;
    //private static string s_key = "a3$-se#";

    //private static String s_iv = "qwertyuiopasdfghjklmnbvc";
    // private static String s_key = "A413D3addasdj1daddhzsksd";
    private static String s_iv = "KKNWLCZU";
    private String algorithm = "DESede";
    private String transformation = "DESede/CBC/PKCS5Padding";
//    private byte[] tdesKeyData;

    //C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: private static Byte[] ConvertStringToByteArray(String s)
    public static byte[] ConvertStringToByteArray(String s) {
        try {
            return s.getBytes("ASCII");
        } catch (UnsupportedEncodingException ex) {
            return null;
        }
    }

    //C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
    ///#region IEncryption Members
    public final void Intialize(java.util.Map parameters) {
        //Convert.tob
    }

    //C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public byte[] Encrypt(byte[] data, string key)
    public final byte[] Encrypt(byte[] data, String key) {
        try {


            byte[] k = ConvertStringToByteArray(key);

            byte[] keyBytes;
            if (k.length == 16) {
                keyBytes = new byte[24];
                System.arraycopy(k, 0, keyBytes, 0, 16);
                System.arraycopy(k, 0, keyBytes, 16, 8);
            } else {
                keyBytes = k;
            }


            byte[] IV = ConvertStringToByteArray(s_iv);
            SecretKeySpec securityKey = new SecretKeySpec(keyBytes, algorithm);
            Cipher encrypter = Cipher.getInstance(transformation);
            encrypter.init(Cipher.ENCRYPT_MODE, securityKey, new IvParameterSpec(IV));

            return encrypter.doFinal(data);

        } catch (Exception e) {
            return data;
        }
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
        ///#endregion

    }

    /**
     * @param parameters
     */
    @Override
    public void Intialize(HashMap parameters) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public byte[] Decrypt(byte[] data, String key) {
        try {
            byte[] k = ConvertStringToByteArray(key);

            byte[] keyBytes;
            if (k.length == 16) {
                keyBytes = new byte[24];
                System.arraycopy(k, 0, keyBytes, 0, 16);
                System.arraycopy(k, 0, keyBytes, 16, 8);
            } else {
                keyBytes = k;
            }

            byte[] IV = ConvertStringToByteArray(s_iv);
            SecretKeySpec securityKey = new SecretKeySpec(keyBytes, algorithm);
            Cipher encrypter = Cipher.getInstance(transformation);
            encrypter.init(Cipher.DECRYPT_MODE, securityKey, new IvParameterSpec(IV));
            return encrypter.doFinal(data);
        } catch (Exception e) {
            return data;
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy