起因对接合作伙伴的系统,需要对数据进行AES加密

默认的使用了已经写好的帮助类中加密算法,发现结果不对,各种尝试改变加密模式改变向量等等折腾快一下午。最后网上查了下AES在JAVA里面的实现完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static String AesEncrypt(String content,String encyKey) {           
        try {                  
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");  
            secureRandom.setSeed(encyKey.getBytes());  
            kgen.init(128, secureRandom);
            Cipher cipher=Cipher.getInstance("AES");       
            cipher.init(Cipher.DECRYPT_MODE,new SecretKeySpec(kgen.generateKey().getEncoded(),"AES"));         
            byte[] byteContent = content.getBytes("utf-8");
            byte[] result = cipher.doFinal(byteContent);           
            BASE64Encoder  encode = new BASE64Encoder ();
            String strResult=encode.encode(result);
            return strResult;
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
  }

根据博主:http://www.cnblogs.com/amylis_chen/p/6107162.html#commentform  的经验有了思路

解决办法,通过JAVA代码生成AES的密钥,再通过C#代码进行AES加密

1
2
3
4
5
6
7
8
9
10
//JAVA代码生成密钥
        String encyKey="合作伙伴提供的密钥 ";
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        java.security.SecureRandom random = java.security.SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(encyKey.getBytes());
        kgen.init(128, random); 
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        BASE64Encoder coder = new BASE64Encoder();       
        System.out.println(coder.encode(enCodeFormat));

C#代码进行AES加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Program
   {
       static void Main(string[] args)
       {
           //默认密钥向量
           var result = AESEncode("123456""JAVA代码输出的密钥");
           Console.WriteLine(result);
           Console.ReadLine();
 
       }
       public static string AESEncode(string encryptString, string encryptKey)
       {
           if (string.IsNullOrEmpty(encryptString)) return null;
           Byte[] toEncryptArray = Encoding.UTF8.GetBytes(encryptString);
           RijndaelManaged rm = new RijndaelManaged
           {
               Key = Convert.FromBase64String(encryptKey),
               Mode = CipherMode.ECB,
               Padding = PaddingMode.PKCS7
           };
           ICryptoTransform cTransform = rm.CreateEncryptor();
           Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
           return Convert.ToBase64String(resultArray, 0, resultArray.Length);
       }
   }

C#对接JAVA系统遇到的AES加密坑的更多相关文章

  1. Java 环境下使用 AES 加密的特殊问题处理

    在 Java 环境下使用 AES 加密,在密钥长度和字节填充方面有一些比较特殊的处理. 1. 密钥长度问题 默认 Java 中仅支持 128 位密钥,当使用 256 位密钥的时候,会报告密钥长度错误 ...

  2. android开发 java与c# 兼容AES加密

    由于android客户端采用的是AES加密,服务器用的是asp.net(c#),所以就造成了不一致的加密与解密问题,下面就贴出代码,已经试验过. using System; using System. ...

  3. java使用RSA与AES加密解密

    首先了解下,什么是堆成加密,什么是非对称加密? 对称加密:加密与解密的密钥是相同的,加解密速度很快,比如AES 非对称加密:加密与解密的秘钥是不同的,速度较慢,比如RSA 先看代码(先会用在研究) 相 ...

  4. .NET 对接JAVA 使用Modulus,Exponent RSA 加密

    最近有一个工作是需要把数据用RSA发送给Java 虽然一开始标准公钥 net和Java  RSA填充的一些算法不一样 但是后来这个坑也补的差不多了 具体可以参考 http://www.cnblogs. ...

  5. JAVA的对称加密算法AES——加密和解密

    出自: http://blog.csdn.net/hongtashan11/article/details/6599645 http://www.cnblogs.com/liunanjava/p/42 ...

  6. 【java工具类】AES加密解密

    百度百科一下,AES:高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准 ...

  7. 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密

    你真的了解字典(Dictionary)吗?   从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...

  8. C#与Java AES 加密解密

    参考文档:https://www.cnblogs.com/xbzhu/p/7064642.html 前几天对接Java接口,需要C#加密参数,Java解密.奈何网上找了一堆大同小异的加解密方法都跟Ja ...

  9. AES加密解密通用版Object-C / C# / JAVA

    1.无向量 128位 /// <summary> /// AES加密(无向量) /// </summary> /// <param name="plainByt ...

随机推荐

  1. FreeSWITCH与FreeSWITCH对接

    (主机A ---> 主机B)192.168.100.A主机:修改/usr/local/freeswitch/conf/dialplan/default.xml 10         <ex ...

  2. linux do{} while(0)

    do{}while(0) 在linux中,经常会看到do{}while(0)这样的语句,许多人开始都会疑惑,认为do{}while(0)毫无意义,因为它只会执行一次,加不加do{}while(0)效果 ...

  3. 各机器学习方法代码(OpenCV2)

    #include <iostream> #include <math.h> #include <string> #include "cv.h" ...

  4. java 中异常处理示例并捕获完整异常内容

    public class Test { public static void main(String[] args) { try { int a = 1; int b = 0; int c = a/b ...

  5. zombodb 低级api 操作

    zombodb 低级api 允许直接从zombodb 索引中进行insert.delete 文档,同时保留了mvcc 的特性,但是数据没有存储在 pg 中,但是也带来数据上的风险,我们需要注意进行es ...

  6. windows openssh server 安装试用

    使用Windows的可能会知道win10 的已经包好了openssh 服务,但是对于其他机器win 7 windows 2008 ,就需要其他的方法了 还好powershell 团队开发了支持wind ...

  7. 虚拟化cpu

    vmware的虚拟机cpu [root@84-monitor ~]# lscpuArchitecture:          x86_64CPU op-mode(s):        32-bit, ...

  8. 创建一个dynamics 365 CRM online plugin (一) - Hello World Plugin

    源代码连接:https://github.com/TheMiao/Dynamics365CRM/blob/master/MyCRM/MyCRM/HelloWorld.cs 首先,我们需要创建一个.NE ...

  9. php 字符串固定长度,不够补充其他字符串

    <?php $input = '456'; $var= str_pad($input,5,10,STR_PAD_LEFT); w3school手冊:http://www.w3school.com ...

  10. Excel技巧--反向查询

    当要从左侧的表格,查询某人所在的部门时,那么需要逆向查询.VLOOKUP函数只能正向查询.可以使用Match和index函数: Match函数:查询某个值在指定区域所在的位置: Index函数:查询指 ...