I am not expert in encryption. We have requirement to encrypt the data in c# and decrypt the same in java. We were able to encrypt and decrypt the data for same key in c# and java. When we try to encrypt the string in c# and decrypt the same in java we are getting
com.nimbusds.jose.JOSEException: AES/GCM/NoPadding decryption failed: Tag mismatch!
Can some one please help.
Below is the code C# code to encrypt data.
Code to Decrypt string in C#
public class JWK
{
public string alg { get; set; }
public int? created_at { get; set; }
public string crv { get; set; }
public int? expired_at { get; set; }
public string kid { get; set; }
public string kty { get; set; }
public string use { get; set; }
public string x { get; set; }
public string y { get; set; }
public string d { get; set; }
}
public static string stringkey = @" {
""y"": ""AGum7Ok3MsKuMfrGtYUoeRq09oXwuO5yiMcxDjPbZglLKaZK-Z_kGt3ZcakVavLx46ieXw4agldm9qV6KmBCf6d7"",
""kty"": ""EC"",
""d"": ""AaDyXhcUwH90OvzbYJvocSl7uYlZNRZvgj8yesGjtqUWfxmN50NIFfnBpKohNc3wkYqviScjevczATzibw0Cnlgp"",
""kty"": ""EC"",
""crv"": ""P-521"",
""x"": ""AeyTJd9u-0k3ihg5T1OKqc7xlfn-a5WWTwp0yeQLz03lTHotMhiuKYQGez5KgeuODtOyZNmNdcKN6WjlFuMmVlTd""
}";
static void Main(string[] args)
{
string secretMessage = "Hello world";
string encryptedValue = Encrypt(secretMessage);
Console.WriteLine("Encrypted Value: "+encryptedValue);
string decryptedValue = Decrypt(encryptedValue);
Console.WriteLine("Decrypted Value: "+decryptedValue);
string encryptedValuewithByteCode = EncryptWithByteCode(secretMessage);
Console.WriteLine("Decrypted Value: " + Decrypt(encryptedValuewithByteCode));
}
public static string Encrypt(string encryptValue) {
JsonTextReader reader = new JsonTextReader(new StringReader(stringkey));
var jwk = JsonSerializer.CreateDefault().Deserialize<JWK>(reader);
var publicECCKey = EccKey.New(WebEncoders.Base64UrlDecode(jwk.x), WebEncoders.Base64UrlDecode(jwk.y), usage: CngKeyUsages.KeyAgreement);
string token = Jose.JWT.Encode(encryptValue, publicECCKey, JweAlgorithm.ECDH_ES, JweEncryption.A256GCM);
return token;
}
public static string EncryptWithByteCode(string encryptValue)
{
JsonTextReader reader = new JsonTextReader(new StringReader(stringkey));
var jwk = JsonSerializer.CreateDefault().Deserialize<JWK>(reader);
var publicECCKey = EccKey.New(WebEncoders.Base64UrlDecode(jwk.x), WebEncoders.Base64UrlDecode(jwk.y), usage: CngKeyUsages.KeyAgreement);
string token = Jose.JWT.EncodeBytes(System.Text.Encoding.UTF8.GetBytes(encryptValue), publicECCKey, JweAlgorithm.ECDH_ES, JweEncryption.A256GCM);
return token;
}
public static string Decrypt(string encryptedValue)
{
JsonTextReader reader = new JsonTextReader(new StringReader(stringkey));
var jwk = JsonSerializer.CreateDefault().Deserialize<JWK>(reader);
var publicECCKey = EccKey.New(WebEncoders.Base64UrlDecode(jwk.x), WebEncoders.Base64UrlDecode(jwk.y), WebEncoders.Base64UrlDecode(jwk.d), usage: CngKeyUsages.KeyAgreement);
string token = Jose.JWT.Decode(encryptedValue, publicECCKey, JweAlgorithm.ECDH_ES, JweEncryption.A256GCM);
return token;
}
}
public class JWK
{
public string alg { get; set; }
public int? created_at { get; set; }
public string crv { get; set; }
public int? expired_at { get; set; }
public string kid { get; set; }
public string kty { get; set; }
public string use { get; set; }
public string x { get; set; }
public string y { get; set; }
public string d { get; set; }
}
Java decrypt string Generation:
String encryptedFromDotNet= "eyJhbGciOiJFQ0RILUVTIiwiZW5jIjoiQTI1NkdDTSIsImVwayI6eyJrdHkiOiJFQyIsIngiOiJBWEVBdzR6MmpnRmkzRktXZ3IwNHQwS3VwOEVOeGpJaWE3aDNVQ1BZSGpwODdwMXpxMHBHVUxXVUxNaFUzOVdubERJLTFFMEl5Mzd6WmJ3LVYzQ0pURFpsIiwieSI6IkFIMUtWOWpONG9JTEpzLThGYlJmVDNVWXF3MnJDVFVZd3k5MEYwQXFFa0VZam1KOXlyekdKVC0zTGQtOUZPRUQwZV9tcE5iYlhRVk12Q25GTEQwQTdSNzIiLCJjcnYiOiJQLTUyMSJ9fQ..AXWgvsa6PwWPSS1l.UxfvMIekKvBAMbs.1n9dqJvPZQfM8v2XEkLLQg";
String decryptedValue = getDecryptedValue(encryptedFromDotNet);
System.out.print(decryptedValue);
}
public static String getDecryptedValue(String encryptedFromDotNet ) {
try {
String ecdhKey = "{\n"
+ "\t\"kty\": \"EC\",\n"
+ "\t\"d\": \"AaDyXhcUwH90OvzbYJvocSl7uYlZNRZvgj8yesGjtqUWfxmN50NIFfnBpKohNc3wkYqviScjevczATzibw0Cnlgp\",\n"
+ "\t\"crv\": \"P-521\",\n"
+ "\t\"x\": \"AeyTJd9u-0k3ihg5T1OKqc7xlfn-a5WWTwp0yeQLz03lTHotMhiuKYQGez5KgeuODtOyZNmNdcKN6WjlFuMmVlTd\",\n"
+ "\t\"y\": \"AGum7Ok3MsKuMfrGtYUoeRq09oXwuO5yiMcxDjPbZglLKaZK-Z_kGt3ZcakVavLx46ieXw4agldm9qV6KmBCf6d7\"\n"
+ "}";
ECKey ecKey = ECKey.parse(ecdhKey);
JWEObject jweObjectServer = EncryptedJWT.parse(encryptedFromDotNet);
ECDHDecrypter decrypter = new ECDHDecrypter(ecKey.toECPrivateKey());
jweObjectServer.decrypt(decrypter);
return jweObjectServer.getPayload().toString();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}