current position:Home>Can you change this des encrypted C version to Java version

Can you change this des encrypted C version to Java version

2022-02-03 01:31:48 CSDN Q & A

    private const string KEY = "VZCDFKBG";    /// <summary>    /// DES encryption     /// </summary>    /// <param name="pToEncrypt"></param>    /// <returns></returns>    public static string DESEncrypt(string pToEncrypt)    {        try        {            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);            des.Key = ASCIIEncoding.ASCII.GetBytes(KEY);            des.IV = ASCIIEncoding.ASCII.GetBytes(KEY);            MemoryStream ms = new MemoryStream();            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);            cs.Write(inputByteArray, 0, inputByteArray.Length);            cs.FlushFinalBlock();            StringBuilder ret = new StringBuilder();            foreach (byte b in ms.ToArray())                ret.AppendFormat("{0:X2}", b);            ret.ToString();            return ret.ToString();        }        catch (Exception)        {            return "";        }    }



Refer to the answer 1:

DES Tool class :

public class AesUtils {    public static final String SECRETKEY = "secretKey";    public static String encryptStr(String srcStr, String password) {        byte[] encryptResult = encryptData_AES(srcStr, password);        String encryptResultStr = parseByte2HexStr(encryptResult);        return encryptResultStr;    }    public static String decryptStr(String srcStr, String password) {        String returnValue = "";        try {            byte[] decryptFrom = parseHexStr2Byte(srcStr);            byte[] decryptResult = decryptData_AES(decryptFrom, password);            returnValue = new String(decryptResult, "utf-8");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        return returnValue;    }    private static String parseByte2HexStr(byte buf[]) {        StringBuffer sb = new StringBuffer();        for (int i = 0; i < buf.length; i++) {            String hex = Integer.toHexString(buf[i] & 0xFF);            if (hex.length() == 1) {                hex = '0' + hex;            }            sb.append(hex.toUpperCase());        }        return sb.toString();    }    private static byte[] encryptData_AES(String content, String password) {        try {            SecretKey secretKey = getKey(password);            byte[] enCodeFormat = secretKey.getEncoded();            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");            Cipher cipher = Cipher.getInstance("AES");            byte[] byteContent = content.getBytes("utf-8");            cipher.init(Cipher.ENCRYPT_MODE, key);            byte[] result = cipher.doFinal(byteContent);            return result;        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        } catch (NoSuchPaddingException e) {            e.printStackTrace();        } catch (InvalidKeyException e) {            e.printStackTrace();        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (IllegalBlockSizeException e) {            e.printStackTrace();        } catch (BadPaddingException e) {            e.printStackTrace();        } catch (GeneralSecurityException e) {            e.printStackTrace();        }        return null;    }    private static byte[] parseHexStr2Byte(String hexStr) {        if (hexStr.length() < 1)            return null;        byte[] result = new byte[hexStr.length() / 2];        for (int i = 0; i < hexStr.length() / 2; i++) {            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),                    16);            result[i] = (byte) (high * 16 + low);        }        return result;    }    private static SecretKey getKey(String secret) throws GeneralSecurityException {        try {                         KeyGenerator _generator = KeyGenerator.getInstance("AES");                SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");                secureRandom.setSeed(secret.getBytes());                _generator.init(128,secureRandom);                return _generator.generateKey();            }  catch (Exception e) {                 throw new RuntimeException("");          }      }    private static byte[] decryptData_AES(byte[] content, String password) {        try {            SecretKey secretKey = getKey(password);            byte[] enCodeFormat = secretKey.getEncoded();            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");            Cipher cipher = Cipher.getInstance("AES");            cipher.init(Cipher.DECRYPT_MODE, key);            byte[] result = cipher.doFinal(content);            return result;         } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        } catch (NoSuchPaddingException e) {            e.printStackTrace();        } catch (InvalidKeyException e) {            e.printStackTrace();        } catch (IllegalBlockSizeException e) {            e.printStackTrace();        } catch (BadPaddingException e) {            e.printStackTrace();        } catch (GeneralSecurityException e) {            e.printStackTrace();        }        return null;    }}



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030131471285.html

Random recommended