关于AES解密的问题

一个坚果 2015-08-07 05:21:13
很坑爹的问题,现在的项目在给某个OA系统做移动客户端,由于OA厂商不提供接口,所以只能自己去读取数据库,他们邮件的内容是加密的,用的AES加密,虽然OA厂商给了加解密的代码,但是无奈是用C#写的,不是很了解,我们项目开发用的是java语言,自己尝试用java写解密,各种不奏效。。求大神
...全文
153 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
一个坚果 2015-08-10
  • 打赏
  • 举报
回复
问题已解决,其实很简单。。代码写错了
public static String aesDecrypt(byte[] strBytes, String keyStr) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/NOPadding");
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128, new SecureRandom(keyStr.getBytes()));
        SecretKeySpec key = new SecretKeySpec(keyStr.getBytes(), "AES");

        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] bytes = cipher.doFinal(strBytes);
        return new String(bytes, "gb2312");
    }

    public static byte[] convertStrArrayToByteArray(String s){
        String[] ss = s.split(";");
        byte[] bs = new byte[ss.length];
        int index = 0;
        for (String byteStr : ss) {
            bs[index ++] = (byte) (Short.parseShort(byteStr));
        }
        return bs;
    }
本来一开始也是这样写,但是报了illegal Key Size的异常,就以为代码写错了, 后面查了查,是因为jdk默认不支持256位密钥的原因,需替换两个jar包 具体可看http://www.cnblogs.com/hnrainll/p/3577805.html 结贴了
whos2002110 2015-08-07
  • 打赏
  • 举报
回复
C#不懂,但jdk好像不支持PaddingMode.Zeros啊, 用第三方Bouncy Castle的实现试试
一个坚果 2015-08-07
  • 打赏
  • 举报
回复
自己尝试些的解密方法
public static String aesDecrypt(byte[] strBytes, String keyStr) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/NOPadding");
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128, new SecureRandom(keyStr.getBytes()));
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");

        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] bytes = cipher.doFinal(strBytes);
        return new String(bytes, "utf-8");
    }

    public static byte[] convertStrArrayToByteArray(String s){
        String[] ss = s.split(";");
        byte[] bs = new byte[ss.length];
        int index = 0;
        for (String byteStr : ss) {
            bs[index ++] = (byte) (Short.parseShort(byteStr) >> 8 & 0xff);
        }
        return bs;
    }

    public static void main(String[] args) throws Exception {
        byte b = (byte) 158;
        System.out.println(b);
        String enStr = "158;244;75;86;184;135;189;50;161;55;60;169;144;186;65;76;37;241;197;21;71;105;113;29;114;92;200;99;102;119;240;124;228;195;12;115;162;186;197;27;40;23;48;24;30;0;98;28;6;113;40;252;191;223;59;138;207;70;31;244;1;9;1;95;66;209;189;115;113;241;122;175;246;155;6;114;221;161;149;246;167;137;27;61;180;122;145;251;52;202;126;242;25;214;129;66;182;176;9;155;36;224;49;158;94;93;53;194;184;46;194;82;203;79;68;185;154;6;182;121;132;233;166;138;209;159;191;126;3;36;113;5;38;84;58;145;78;118;177;222;216;160;217;204;169;153;3;40;198;4;144;137;0;60;96;69;96;4;47;60;69;202;131;250;137;162;192;216;0;95;75;47;3;72;219;85;13;33;88;68;135;239;221;114;171;190;114;128;168;156;230;180;120;251;70;48;151;23;254;221;73;90;111;159;150;22;50;108;133;233;226;157;165;254;14;242;59;176;100;81;27;156;110;194;6;113;40;252;191;223;59;138;207;70;31;244;1;9;1;95;66;209;189;115;113;241;122;175;246;155;6;114;221;161;149;246;145;77;98;181;148;212;44;112;175;96;184;222;128;172;98;31;147;59;158;66;238;255;8;6;100;215;35;228;28;197;52;168;252;239;80;176;80;195;177;197;42;252;47;184;235;64;237;246";
        String key = "^_^b@_@b*_*b-_-b^_^b@_@b*_*b-_-b";
        System.out.println(aesDecrypt(convertStrArrayToByteArray(enStr), key));
    }
一个坚果 2015-08-07
  • 打赏
  • 举报
回复
加解密的C#代码
namespace YGL.Encrypt
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class SymmetricEncrypt
    {
        private byte[] cipherbytes;
        private byte[] finalbytes;
        private string plainTextValue;
        private static char seprate = ';';
        private SymmetricAlgorithm syma = Rijndael.Create();
        private string symmetricKeyValue = "^_^b@_@b*_*b-_-b^_^b@_@b*_*b-_-b";

        public SymmetricEncrypt()
        {
            this.Init();
        }

        public static string[] ConvertByteAryToStringAry(byte[] args)
        {
            string[] strArray = new string[args.Length];
            for (int i = 0; i < args.Length; i++)
            {
                strArray[i] = args[i].ToString();
            }
            return strArray;
        }

        public static byte[] ConvertStringAryToByteAry(string[] args)
        {
            byte[] buffer = new byte[args.Length];
            for (int i = 0; i < args.Length; i++)
            {
                buffer[i] = Convert.ToByte(args[i]);
            }
            return buffer;
        }

        public string Decrypt(string[] args)
        {
            return this.Decrypt(ConvertStringAryToByteAry(args));
        }

        public string Decrypt(string args)
        {
            string[] strArray = args.Split(new char[] { seprate });
            return this.Decrypt(ConvertStringAryToByteAry(strArray));
        }

        public string Decrypt(byte[] args)
        {
            MemoryStream stream = new MemoryStream(args);
            CryptoStream stream2 = new CryptoStream(stream, this.syma.CreateDecryptor(), CryptoStreamMode.Read);
            List<byte> list = new List<byte>();
            int num = 0;
            do
            {
                num = stream2.ReadByte();
                if (num != -1)
                {
                    list.Add(Convert.ToByte(num));
                }
            }
            while (num != -1);
            this.finalbytes = list.ToArray();
            this.plainTextValue = Encoding.Default.GetString(this.finalbytes);
            return this.plainTextValue;
        }

        public byte[] Encrypt()
        {
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, this.syma.CreateEncryptor(), CryptoStreamMode.Write);
            byte[] bytes = Encoding.Default.GetBytes(this.plainTextValue);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.Close();
            this.cipherbytes = stream.ToArray();
            stream.Close();
            return this.cipherbytes;
        }

        public string EncryptToString()
        {
            StringBuilder builder = new StringBuilder();
            string[] strArray = ConvertByteAryToStringAry(this.Encrypt());
            if (strArray != null)
            {
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (i == 0)
                    {
                        builder.Append(strArray[i]);
                    }
                    else
                    {
                        builder.Append(seprate.ToString()).Append(strArray[i]);
                    }
                }
            }
            return builder.ToString();
        }

        public string[] EncryptToStringAry()
        {
            return ConvertByteAryToStringAry(this.Encrypt());
        }

        public void Init()
        {
            byte[] bytes = Encoding.UTF8.GetBytes(this.symmetricKeyValue);
            this.syma.Key = bytes;
            this.syma.Mode = CipherMode.ECB;
            this.syma.Padding = PaddingMode.Zeros;
        }

        public string PlainText
        {
            get
            {
                return this.plainTextValue;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    Exception exception = new Exception("明文不能为空!");
                    throw exception;
                }
                this.plainTextValue = value;
            }
        }

        public string SymmetricKey
        {
            get
            {
                return this.symmetricKeyValue;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    Exception exception = new Exception("密钥不能为空!");
                    throw exception;
                }
                if (value.Length != 0x20)
                {
                    Exception exception2 = new Exception("密钥长度必须是32个字符!");
                    throw exception2;
                }
                this.symmetricKeyValue = value;
            }
        }
    }
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧