I have the following c# code but need the exact same algorithm in Java. Whether the c# code is quality, I can do nothing about. I only care if the Java encoded string is equivalent.
//Run GetToken to get encrypted string
var token = GetToken(AMessage, account_name, api_key);
private static byte[] GetBytes(string value, byte[] salt, int length)
{
var deriveBytes = new Rfc2898DeriveBytes(value, salt);
return deriveBytes.GetBytes(length);
}
private static string GetToken(string session, string accountName, string apiKey)
{
byte[] encrypted;
using (AesManaged aes = new AesManaged())
{
var salt = Encoding.Default.GetBytes(apiKey);
var key = GetBytes(accountName, salt, aes.KeySize / 8);
var vector = GetBytes(accountName, salt, aes.BlockSize / 8);
aes.Key = key;
aes.IV = vector;
var memoryStream = new MemoryStream();
using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(session);
}
}
}
encrypted = memoryStream.ToArray();
}
return Convert.ToBase64String(encrypted);
}
Aucun commentaire:
Enregistrer un commentaire