首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java AES/GCM/NoPadding算法到C#的转换

Java AES/GCM/NoPadding算法到C#的转换
EN

Stack Overflow用户
提问于 2021-10-18 14:30:28
回答 2查看 132关注 0票数 0

我正在尝试转换这段java代码:

代码语言:javascript
运行
复制
package ffsd;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class GatewayEncryptUtil {
  public static String encrypt(String plainText, String key) throws Exception {
    return new String(Base64.getEncoder().encode(doCrypt(plainText.getBytes(), 1, key)));
  }
  
  public static byte[] doCrypt(byte[] inputText, int operation, String key) throws Exception {
    MessageDigest mDigest = MessageDigest.getInstance("SHA-512");
    byte[] secretKey = key.getBytes();
    byte[] digestSeed = mDigest.digest(secretKey);
    byte[] hashKey = Arrays.copyOf(digestSeed, 16);
    
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    SecretKeySpec skspec = new SecretKeySpec(hashKey, "AES");
    String cipherKey = new String(secretKey);
    GCMParameterSpec gcmParams = new GCMParameterSpec(128, cipherKey.substring(0, 16).getBytes());
    cipher.init(operation, skspec, gcmParams);
    return cipher.doFinal(inputText);
  }
  
  public static void main(String[] args) {
    try {
      System.out.println(encrypt("Password", "1234567890123456"));
    } catch (Exception e) {
      e.printStackTrace();
    } 
  }
}

要使用新的AesGcm类执行C# .NET 5.0,请执行以下操作:

代码语言:javascript
运行
复制
using System;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputText = "Password";
            string msgId = "1234567890123456";

            byte[] hashKey;
            byte[] secretKey = Encoding.ASCII.GetBytes(msgId);

            using (var hasher = SHA512.Create())
            {
                byte[] digestSeed = hasher.ComputeHash(secretKey);
                hashKey = new byte[16];
                Array.Copy(digestSeed, hashKey, hashKey.Length);
            }

            using (var aesGcm = new AesGcm(hashKey))
            {
                byte[] nonce = new byte[AesGcm.NonceByteSizes.MaxSize];
                byte[] plainText = Encoding.ASCII.GetBytes(inputText);
                byte[] authTag = new byte[AesGcm.TagByteSizes.MaxSize];
                byte[] cipherText = new byte[plainText.Length];

                aesGcm.Encrypt(nonce, plainText, cipherText, authTag);
                string cipherTextBase64 = Convert.ToBase64String(cipherText);
                string authTagBase64 = Convert.ToBase64String(authTag);

                Console.WriteLine(cipherTextBase64);
                Console.WriteLine(authTagBase64);
            }
        }
    }
}

但我不知道nonce应该是什么。在java代码中看不到这一点。有没有人能给我点建议?java代码的结果是:"gc1zTHlIPQusN5e+Rjq+veDoIYdU1nCQ“我的代码显然是不完整的,而且不是很接近。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-18 14:59:38

IV和Nonce的意思是一样的。

IV是GCMParameterSpec的第二个参数:

代码语言:javascript
运行
复制
GCMParameterSpec gcmParams = new GCMParameterSpec(128, cipherKey.substring(0, 16).getBytes());

.NET称此为现时值。

票数 0
EN

Stack Overflow用户

发布于 2021-10-19 14:21:46

我能够使用BouncyCastle将其转换为C#:

代码语言:javascript
运行
复制
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputText = "Password";
            string msgId = "1234567890123456";

            string value = Encrypt(inputText, msgId);
            Console.WriteLine(value);
        }

        public static string Encrypt(string plainText, string msgId)
        {
            const byte GcmTagSize = 16;

            byte[] hashKey;
            byte[] secretKey = Encoding.ASCII.GetBytes(msgId);

            using (var hasher = SHA512.Create())
            {
                byte[] digestSeed = hasher.ComputeHash(secretKey);
                hashKey = new byte[16];
                Array.Copy(digestSeed, hashKey, hashKey.Length);
            }

            var keyParameter = new KeyParameter(hashKey);
            var keyParameters = new AeadParameters(keyParameter, GcmTagSize * 8, secretKey);

            var cipher = CipherUtilities.GetCipher("AES/GCM/NoPadding");
            cipher.Init(true, keyParameters);

            var plainTextData = Encoding.ASCII.GetBytes(plainText);
            var cipherText = cipher.DoFinal(plainTextData);

            return Convert.ToBase64String(cipherText);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69617895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档