gpt4 book ai didi

java - 从c#到java的AES加密/解密

转载 作者:行者123 更新时间:2023-11-30 10:59:07 25 4
gpt4 key购买 nike

我在我的应用程序中使用 AES 加密/解密算法。

在服务器端,我使用 c# 来加密/解密数据。

在客户端(android)我使用 java 来解密数据。

C#加解密代码

    static readonly string PasswordHash = "52";

static readonly string SaltKey = "dfkjsadfinewdfadsfkmeoinmsdflksdflk";

static readonly string VIKey = "@EUBRHDFBFG8867";

public static string Encrypt(string plainText)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash,Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);

var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding =PaddingMode.Zeros };

var encryptor = symmetricKey.CreateEncryptor(keyBytes,Encoding.ASCII.GetBytes(VIKey));
byte[] cipherTextBytes;

using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor,CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
cryptoStream.Close();
}
memoryStream.Close();
}
return Convert.ToBase64String(cipherTextBytes);
}

public static string Decrypt(string encryptedText)
{
byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);

byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash,Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);

var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding =PaddingMode.None }

var decryptor = symmetricKey.CreateDecryptor(keyBytes,Encoding.ASCII.GetBytes(VIKey));

var memoryStream = new MemoryStream(cipherTextBytes);

var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);

byte[] plainTextBytes = new byte[cipherTextBytes.Length];

int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);

memoryStream.Close();
cryptoStream.Close();

return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
}

Java解密方法

public String decrypt(String dataToDecrypt) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, UnsupportedEncodingException 
{

byte[] encryptedCombinedBytes = Base64.decodeBase64(dataToDecrypt.getBytes());

String saltKey = "dfkjsadfinewdfadsfkmeoinmsdflksdflk";
String password = "52";
String IVKey = "@EUBRHDFBFG8867";

PBKDF2Parameters p = new PBKDF2Parameters("HmacSHA256", "ASCII", saltKey.getBytes(), 8);

byte[] mEncryptedPassword = new PBKDF2Engine(p).deriveKey(password);


byte[] ivbytes = Arrays.copyOfRange(IVKey.getBytes(), 0, 16);

SecretKeySpec mSecretKeySpec = new SecretKeySpec(mEncryptedPassword, "AES");

Cipher mCipher = Cipher.getInstance("AES/CBC/NoPadding");

mCipher.init(Cipher.DECRYPT_MODE, mSecretKeySpec, new IvParameterSpec(ivbytes));

byte[] encryptedTextBytes = Arrays.copyOfRange(encryptedCombinedBytes, 16, encryptedCombinedBytes.length);

byte[] decryptedTextBytes = mCipher.doFinal(encryptedTextBytes);

return new String(decryptedTextBytes, "UTF-8");
}

C#解密方法工作正常并给出结果字符串。

我无法找出 Java 解密代码中的问题。它运行并给我一些垃圾值。

编辑

  1. 我无法在服务器端编辑任何内容。我只需要在 java 解密中复制解密。
  2. 我不知道如何使用 passwordHash、saltKey 和 IVkey

最佳答案

首先,您已经调换了密码和盐。

其次,PBKDF2 默认使用 HMAC/SHA-1。据我所知,这也是 Rfc2898DeriveBytes 的默认设置:

Implements password-based key derivation functionality, PBKDF2, by using a pseudo-random number generator based on HMACSHA1.

您也不应在未指定 Java 字符集的情况下调用 getBytes,但这可能不是您当前运行时的问题。


这些只是对代码的评论; 不要在没有完整性/真实性保护的情况下通过网络连接使用 CBC

关于java - 从c#到java的AES加密/解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32008047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com