C# Java的加密的各類折騰

24位加密ide

Java加密

public class DESUtil {

    private static final String KEY_ALGORITHM = "DESede";

    private static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";

    private static final String ENCODING = "UTF-8";

    /**
     *
     * @param data body的JSON字符串
     * @param key 騰住提供的DesSecret密鑰
     * @return
     */
    public static String encrypt(String data, String key) {
        byte[] encrypt = encrypt(data.getBytes(Charset.forName(ENCODING)), key);
        return encrypt == null ? null : Base64.encodeBase64String(encrypt);
    }

    private static byte[] encrypt(byte[] data, String key) {
        try {
            //實例化Des密鑰
            DESedeKeySpec dks = new DESedeKeySpec(key.getBytes());
            //實例化密鑰工廠
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
            //生成密鑰
            SecretKey secretKey = keyFactory.generateSecret(dks);
            //實例化
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            //初始化,設置爲加密模式
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return cipher.doFinal(data);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

對應的C#code

public static string Encrypt(string data, string key)
        {


            if (key.Length > 24)
                key = key.Substring(0, 24);

            TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider()
            {
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7,
                Key = Encoding.UTF8.GetBytes(key)
            };
            using (MemoryStream ms = new MemoryStream())
            {
                CryptoStream cStream = new CryptoStream(ms, tdsp.CreateEncryptor(), CryptoStreamMode.Write);
                var buffer = Encoding.UTF8.GetBytes(data);
                cStream.Write(buffer, 0, buffer.Length);
                cStream.FlushFinalBlock();
                cStream.Close();
                return Convert.ToBase64String(ms.ToArray());
            }
        }

C#8位解密ip

private string Des(string pToEncrypt, string key)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.ECB;
                //des.Mode = CipherMode.CBC;
                des.Padding = PaddingMode.PKCS7;
                var buffer = UTF8Encoding.UTF8.GetBytes(key).Take(8).ToArray();
                des.Key = des.IV = buffer;

                using (MemoryStream ms = new MemoryStream())
                {
                    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    return HttpUtility.UrlEncode(Convert.ToBase64String(ms.ToArray()));//  Encoding.UTF8.GetString(ms.ToArray());

                }
            }
相關文章
相關標籤/搜索