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

com.alachisoft.ncache.licensing.EncryptionUtil 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.licensing;

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

/**
 * @author moiz_rauf
 */
class EncryptionUtil {

    private static String algorithm = "DESede";
    private static String transformation = "DESede/CBC/PKCS5Padding";
    private static String s_key = "A41'D3a##asd[1-a;d zs[s`";
    private static String s_iv = "i09-0038";//"KKNWLCZUNFKNSKZOJSZLEKIJUYGAXTMVZWHPBZVIOPBFYSJHKCDOOAFZCDBNWZXT";
    //moiz: originally it was c# ticks
    private static java.util.Random random = new java.util.Random((int) System.currentTimeMillis());

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

    // Encrypt the string.
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static byte[] Encrypt(string PlainText)
    public static byte[] Encrypt(String PlainText) {
        if (PlainText == null) {
            return null;
        }
        try {
            byte[] data = ConvertStringToByteArray(PlainText);

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

            return encrypter.doFinal(data);

        } catch (Exception e) {
            return null;
        }
    }

    // Decrypt the byte array.
    //C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
    //ORIGINAL LINE: public static string Decrypt(byte[] CypherText)
    public static String Decrypt(byte[] CypherText) {
        if (CypherText == null) {
            return new String();
        }
        try {
            byte[] k = ConvertStringToByteArray(s_key);
            byte[] IV = ConvertStringToByteArray(s_iv);
            SecretKeySpec securityKey = new SecretKeySpec(k, algorithm);
            Cipher encrypter = Cipher.getInstance(transformation);
            encrypter.init(Cipher.DECRYPT_MODE, securityKey, new IvParameterSpec(IV));
            return new String(encrypter.doFinal(CypherText), "ASCII");
        } catch (Exception ex) {
            return null;
        }
    }

    private static String RandomString(int size) {
        StringBuilder builder = new StringBuilder();
        char ch;
        for (int i = 0; i < size; i++) {
            ch = (char) (int) Math.floor(26 * random.nextDouble() + 65);
            builder.append(ch);
        }

        return builder.toString();
    }


    /**
     * Encrypt user provided key with the default key stored; This key is obfuscated
     *
     * @param key Key
     * @return encrypted string
     */
    //C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
    //ORIGINAL LINE: internal static byte[] EncryptData(byte[] data)
    public static byte[] EncryptData(byte[] data) {
        try {
            byte[] k = ConvertStringToByteArray(s_key);
            byte[] IV = ConvertStringToByteArray(s_iv);
            SecretKeySpec securityKey = new SecretKeySpec(k, algorithm);
            Cipher encrypter = Cipher.getInstance(transformation);
            encrypter.init(Cipher.ENCRYPT_MODE, securityKey, new IvParameterSpec(IV));

            return encrypter.doFinal(data);

        } catch (Exception e) {
            return null;
        }
    }

    //C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
    //ORIGINAL LINE: public static byte[] DecryptData(byte[] encodedData)
    public static byte[] DecryptData(byte[] encodedData) {
        if (encodedData == null) {
            return new byte[0];
        }
        try {
            byte[] k = ConvertStringToByteArray(s_key);
            byte[] IV = ConvertStringToByteArray(s_iv);
            SecretKeySpec securityKey = new SecretKeySpec(k, algorithm);
            Cipher encrypter = Cipher.getInstance(transformation);
            encrypter.init(Cipher.DECRYPT_MODE, securityKey, new IvParameterSpec(IV));
            return encrypter.doFinal(encodedData);
        } catch (Exception ex) {
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy