Пробовал портировать на c #, но возникли проблемы с расшифровкой
private static encryptPassword(string password, string encryptionPubKey, string encryptionKeyId)
{
byte[] passwordAsByte = Encoding.ASCII.GetBytes(password);
byte[] data = Convert.FromBase64String(encryptionPubKey);
string decoededPubKey = Encoding.UTF8.GetString(data);
decoededPubKey = decoededPubKey.Replace("-----BEGIN PUBLIC KEY-----", "");
decoededPubKey = decoededPubKey.Replace("-----END PUBLIC KEY-----", "");
SecureRandom random = new SecureRandom();
byte[] randKey = new byte[32];
random.NextBytes(randKey);
byte[] iv = new byte[12];
random.NextBytes(iv);
//String date = String.valueOf(new Date().getTime() / 1000);
long tsLong = DateTimeOffset.UtcNow.ToUnixTimeSeconds() / 1000;
string date = tsLong.ToString();
var header = new MemoryStream(2);
header.Write(BitConverter.GetBytes(Convert.ToInt32(1)));
header.Write(BitConverter.GetBytes(Convert.ToInt32(int.Parse(encryptionKeyId))));
MemoryStream timeAAD = new MemoryStream(10);
timeAAD.Write(Encoding.ASCII.GetBytes(date));
//////////////////////////////////////////////////////////////////
X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(Base64.decode(decoededPubKey, Base64.NO_WRAP));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(publicSpec);
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] rsaEncrypted = rsaCipher.doFinal(randKey);
MemoryStream sizeBuff = new MemoryStream(2);
sizeBuff.order(ByteOrder.LITTLE_ENDIAN);
sizeBuff.putChar((char) rsaEncrypted.Length);
///////////////////////////////////////////////////////////////////////
AeadParameters parameters = new AeadParameters(new KeyParameter(randKey), 128, iv, timeAAD.ToArray());
GcmBlockCipher gcmEngine = new GcmBlockCipher(new AesFastEngine());
gcmEngine.Init(true, parameters);
byte[] gcmText = new byte[gcmEngine.GetOutputSize(passwordAsByte.Length)];
int len = gcmEngine.ProcessBytes(passwordAsByte, 0, passwordAsByte.Length, gcmText, 0);
gcmEngine.DoFinal(gcmText, len);
byte[] encPass = Arrays.CopyOfRange(gcmText, 0, gcmText.Length - 16);
byte[] authTag = Arrays.CopyOfRange(gcmText, gcmText.Length - 16, gcmText.Length);
var a = header.Position + iv.Length + sizeBuff.Position + rsaEncrypted.Length + authTag.Length + encPass.Length;
MemoryStream result = new MemoryStream(a);
result.Write(header.ToArray());
result.Write(iv);
result.Write(sizeBuff.ToArray());
result.Write(rsaEncrypted);
result.Write(authTag);
result.Write(encPass);
//return new Pair(Convert.ToInt64(date), Base64.encodeToString(result.array(), Base64.NO_WRAP));
}
Это практически готовый код, если будут исправления, пишите в комментариях, я исправлю