Henry.Yao 2017-04-14 12:14 采纳率: 0%
浏览 934
已结题

一段C#的AES加密代码,求高手指点对应的Java代码

以下C#的AES加密代码

// key = "05d870d9be50f4786ac2941363abdcea";
// raw = "65220b2f8fba485590615c0b1aaea872|79e68b4638214e0e848ab2757ef2dcb";
// return "JTJ5WkDFvunfCxh9D3+1jkzabeWkOPnqrJiIztFY1v+S9H+BH/c0XkauCGCt3fewjGcGU4mkYqgQynlw4bjMTg==" ;



         public static string Encrypt(string encryptStr, string key)
        {
            var keyArray = Encoding.UTF8.GetBytes(key);
            var toEncryptArray = Encoding.UTF8.GetBytes(encryptStr);
            var rDel = new RijndaelManaged
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.None
            };
            var cTransform = rDel.CreateEncryptor();
            var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

求对应的Java代码。以下是我找到的AES加密,但运行结果不一致,求高手指点:

 package cn.companyname.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
 * 加密解密EncryptAES.
 */
public class EncryptAES {

    private static final String transform = "AES/ECB/NoPadding";

    private static final String algorithm = "AES";

    public static String decrypt(String content, String key) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException {
        Cipher cipher = Cipher.getInstance(transform);
        byte[] encryptedBytes = Base64.decodeBase64(content.getBytes());
        SecretKeySpec keySpec = new SecretKeySpec(getKey(key), algorithm);
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, "UTF-8");
    }

    public static String encrypt(String content, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidKeyException {
        Cipher cipher = Cipher.getInstance(transform);
        SecretKeySpec keySpec = new SecretKeySpec(getKey(key), algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] output = cipher.doFinal(content.getBytes("UTF-8"));
        return new String(Base64.encodeBase64(output));
    }

    private static byte[] getKey(String strKey) throws UnsupportedEncodingException {
        byte[] arrBTmp = strKey.getBytes("UTF-8");
        byte[] arrB = new byte[16]; // 创建一个空的16位字节数组(默认值为0)
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }
        return arrB;
    }

    public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {

        // 以下数据是从C#程序的运行参数和结果,以期完全一致。 
        String key = "05d870d9be50f4786ac2941363abdcea";
        String raw = "65220b2f8fba485590615c0b1aaea872|79e68b4638214e0e848ab2757ef2dcb";
        String str = encrypt(raw, key);
        System.out.println(str);
        System.out.println("JTJ5WkDFvunfCxh9D3+1jkzabeWkOPnqrJiIztFY1v+S9H+BH/c0XkauCGCt3fewjGcGU4mkYqgQynlw4bjMTg==");  

    }

}

  • 写回答

1条回答 默认 最新

  • devmiao 2017-04-14 15:57
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿