最近一直烦恼,java , c# 加密的不同,然后整理了一下,留个备份的轮子:
其中在 java、c#加密转换时,最重要的是 IV 的确定,我常常用如下方法使得java和c#之间的 IV 可以转换:
将其中某个 IV 数组转为 10进制后,在转换为相应的 byte数组就好了。10进制数组成为 java、c# 中IV转换的桥梁了。最后分别有java(我用myeclipse)和c#的编译器(SharpDevelop2很轻量级的编译器,但是运行代码后需要在后面写上 "Console.ReadLine(); " ,不然dos框会一闪而过。这个编译器还需要系统中安装有.net 2.0或者其他版本)多多实践,这样可以更加直接的看到结果。 下面实例中java与c#的代码都是一一对应的:当然,这些加密算法并不适合所有的加密结构,但是重要的是 IV 、key 的格式化。 网上查了资料,找到java转换为16进制的方法(已测)
Java代码
- // java格式化16进制输出
- public static void formatIntTo16(byte[] bytes) {
- String[] resultBytes = new String[bytes.length];
- for (int i = 0; i < bytes.length; i++) {
- resultBytes[i] = Integer.toHexString(bytes[i] & 0xFF);
- }
- }
Java代码
- //MD5加密
- public static String md5Encode(String strContent) {
- try {
- MessageDigest messageDigest = MessageDigest.getInstance("MD5");
- byte[] tempBytes = messageDigest.digest(strContent.getBytes("UnicodeLittleUnmarked"));
- StringBuffer stringBuffer = new StringBuffer();
- for(byte bytes: tempBytes) {
- String tempByte = Integer.toHexString(bytes & 0xFF);
- if(tempByte.length() == 1) {
- stringBuffer.append("0");
- }
- stringBuffer.append(tempByte);
- }
- return stringBuffer.toString();
- } catch(Exception e) {
- e.printStackTrace();
- }
- return null;
- }
C#代码
- //md5加密
- public static void MD5(string str) {
- string cl1 = str;
- string pwd = "";
- System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
- byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(cl1));
- for (int i = 0; i < s.Length; i++) {
- pwd = pwd + s[i].ToString("x");
- }
- Console.Write(pwd);
- Console.WriteLine();
- }
Java代码
- // 3DES加密 基方法
- public static byte[] des3EncodeCBC(byte[] data, byte[] key) throws Exception {
- Key deskey = null;
- DESedeKeySpec spec = new DESedeKeySpec(key);
- SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
- deskey = keyfactory.generateSecret(spec);
- Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
- IvParameterSpec ips = new IvParameterSpec(IV);
- cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
- return cipher.doFinal(data, 0, data.length);
- }
C#代码
- // 3DES加密算法
- private static bool Encrypt( byte[] key, byte[] input, out byte[] output )
- {
- output = null;
- try
- {
- TripleDESCryptoServiceProvider trippleDesProvider = new TripleDESCryptoServiceProvider();
- ICryptoTransform encryptObj = trippleDesProvider.CreateEncryptor(rkey, IV);
- output = encryptObj.TransformFinalBlock(input, 0, input.Length);
- trippleDesProvider.Clear();
- }
- catch (Exception)
- {
- return false;
- }
- return true;
- }
Java代码
- // 3DES 解密 基方法
- public static byte[] des3DecodeCBC(byte[] data, byte[] key) throws Exception {
- Key deskey = null;
- DESedeKeySpec spec = new DESedeKeySpec(key);
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("desede");
- deskey = keyFactory.generateSecret(spec);
- Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
- IvParameterSpec ips = new IvParameterSpec(IV);
- cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
- return cipher.doFinal(data, 0, data.length);
- }
C#代码
- // 3DES 解密 基方法
- private static bool Decrypt(byte[] key, byte[] input, out byte[] output)
- {
- output = null;
- try
- {
- TripleDESCryptoServiceProvider trippleDesProvider = new TripleDESCryptoServiceProvider();
- ICryptoTransform decryptObj = trippleDesProvider.CreateDecryptor(key, IV);
- output = decryptObj.TransformFinalBlock(input, 0, input.Length);
- trippleDesProvider.Clear();
- }
- catch (Exception ex)
- {
- return false;
- }
- return true;
- }
Java代码
- //DES加密
- public static String encode(byte[] key, byte[] iv, byte[] data) throws Exception {
- Key deskey = null;
- DESKeySpec spec = new DESKeySpec(key);
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
- deskey = keyFactory.generateSecret(spec);
- IvParameterSpec ips = new IvParameterSpec(iv);
- Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
- return new BASE64Encoder().encode(cipher.doFinal(data, 0, data.length));
- }
C#代码
-
- //DES加密
- public static string DESEncode(string encryptString, string encryptKey) {
- byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
- byte[] rgbIV = DESKeys;
- byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
- DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
- MemoryStream mStream = new MemoryStream();
- CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV),
- CryptoStreamMode.Write);
- cStream.Write(inputByteArray, 0, inputByteArray.Length);
- cStream.FlushFinalBlock();
- return Convert.ToBase64String(mStream.ToArray());
- }
时间: 2024-12-28 10:09:23