PrimeiroPay
Search…
Examples for decrypting message
C#
Groovy
Java
Node.js
Php
Phyton
Ruby
Scala
1
using System;
2
using System.Linq;
3
using System.Text;
4
using Org.BouncyCastle.Crypto.Engines;
5
using Org.BouncyCastle.Crypto.Modes;
6
using Org.BouncyCastle.Crypto.Parameters;
7
8
namespace DecryptionExample
9
{
10
// You need to install bccrypto-csharp from BouncyCastle. Please see BouncyCastle page.
11
class Program
12
{
13
static void Main(string[] args)
14
{
15
string keyFromConfiguration = "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f";
16
17
// Data from server
18
string ivFromHttpHeader = "000000000000000000000000";
19
string authTagFromHttpHeader = "CE573FB7A41AB78E743180DC83FF09BD";
20
string httpBody = "0A3471C72D9BE49A8520F79C66BBD9A12FF9";
21
22
// Convert data to process
23
byte[] key = ToByteArray(keyFromConfiguration);
24
byte[] iv = ToByteArray(ivFromHttpHeader);
25
byte[] authTag = ToByteArray(authTagFromHttpHeader);
26
byte[] encryptedText = ToByteArray(httpBody);
27
byte[] cipherText = encryptedText.Concat(authTag).ToArray();
28
29
// Prepare decryption
30
GcmBlockCipher cipher = new GcmBlockCipher(new AesFastEngine());
31
AeadParameters parameters = new AeadParameters(new KeyParameter(key), 128, iv);
32
cipher.Init(false, parameters);
33
34
// Decrypt
35
var plainText = new byte[cipher.GetOutputSize(cipherText.Length)];
36
var len = cipher.ProcessBytes(cipherText, 0, cipherText.Length, plainText, 0);
37
cipher.DoFinal(plainText, len);
38
Console.WriteLine(Encoding.ASCII.GetString(plainText));
39
}
40
41
static byte[] ToByteArray(string HexString)
42
{
43
int NumberChars = HexString.Length;
44
byte[] bytes = new byte[NumberChars / 2];
45
for (int i = 0; i < NumberChars; i += 2)
46
{
47
bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
48
}
49
return bytes;
50
}
51
}
52
}
Copied!
1
import org.bouncycastle.jce.provider.BouncyCastleProvider
2
3
import javax.crypto.spec.IvParameterSpec
4
import javax.crypto.spec.SecretKeySpec
5
import java.security.Security
6
7
// For Java and JVM-based languages, you might need to install unrestricted policy file for JVM,
8
// which is provided by Sun. Please refer BouncyCastle FAQ if you get
9
// java.lang.SecurityException: Unsupported keysize or algorithm parameters or
10
// java.security.InvalidKeyException: Illegal key size.
11
12
// If you cannot install unrestricted policy file for JVM because of some reason, you can try with reflection: See here.
13
14
class Cipher {
15
static void main(String[] args) {
16
Security.addProvider(new BouncyCastleProvider())
17
18
// Data from configuration
19
def keyFromConfiguration = "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
20
21
// Data from server
22
def ivFromHttpHeader = "000000000000000000000000"
23
def authTagFromHttpHeader = "CE573FB7A41AB78E743180DC83FF09BD"
24
def httpBody = "0A3471C72D9BE49A8520F79C66BBD9A12FF9"
25
26
// Convert data to process
27
def key = keyFromConfiguration.decodeHex()
28
def iv = ivFromHttpHeader.decodeHex()
29
def authTag = authTagFromHttpHeader.decodeHex() as Byte[]
30
def encryptedText = httpBody.decodeHex() as Byte[]
31
32
// Unlike other programming language, We have to append auth tag at the end of encrypted text
33
def cipherText = encryptedText + authTag as Byte[]
34
35
// Prepare decryption
36
def keySpec = new SecretKeySpec(key, 0, 32, "AES")
37
def cipher = javax.crypto.Cipher.getInstance("AES/GCM/NoPadding")
38
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv))
39
40
// Decrypt
41
def result = cipher.doFinal(cipherText)
42
println(new String(result, "UTF-8"))
43
}
44
}
Copied!
1
import com.google.common.base.Charsets;
2
import org.apache.commons.lang3.ArrayUtils;
3
import org.bouncycastle.jce.provider.BouncyCastleProvider;
4
import javax.crypto.Cipher;
5
import javax.crypto.spec.IvParameterSpec;
6
import javax.crypto.spec.SecretKeySpec;
7
import javax.xml.bind.DatatypeConverter;
8
import java.security.Security;
9
10
// For Java and JVM-based languages, you might need to install unrestricted policy file for JVM,
11
// which is provided by Sun. Please refer BouncyCastle FAQ if you get
12
// java.lang.SecurityException: Unsupported keysize or algorithm parameters or
13
// java.security.InvalidKeyException: Illegal key size.
14
15
// If you cannot install unrestricted policy file for JVM because of some reason, you can try with reflection: See here.
16
17
public class Decryption
18
{
19
public static void main(String[] args) throws Exception
20
{
21
Security.addProvider(new BouncyCastleProvider());
22
23
// Data from configuration
24
String keyFromConfiguration = "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f";
25
26
// Data from server
27
String ivFromHttpHeader = "000000000000000000000000";
28
String authTagFromHttpHeader = "CE573FB7A41AB78E743180DC83FF09BD";
29
String httpBody = "0A3471C72D9BE49A8520F79C66BBD9A12FF9";
30
31
// Convert data to process
32
byte[] key = DatatypeConverter.parseHexBinary(keyFromConfiguration);
33
byte[] iv = DatatypeConverter.parseHexBinary(ivFromHttpHeader);
34
byte[] authTag = DatatypeConverter.parseHexBinary(authTagFromHttpHeader);
35
byte[] encryptedText = DatatypeConverter.parseHexBinary(httpBody);
36
37
// Unlike other programming language, We have to append auth tag at the end of encrypted text in Java
38
byte[] cipherText = ArrayUtils.addAll(encryptedText, authTag);
39
40
// Prepare decryption
41
SecretKeySpec keySpec = new SecretKeySpec(key, 0, 32, "AES");
42
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
43
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
44
45
// Decrypt
46
byte[] bytes = cipher.doFinal(cipherText);
47
System.out.println(new String(bytes, Charsets.UTF_8));
48
}
49
}
Copied!
1
var crypto = require("crypto");
2
3
// Data from configuration
4
var secretFromConfiguration = "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f";
5
6
// Data from server
7
var ivfromHttpHeader = "000000000000000000000000";
8
var authTagFromHttpHeader = "CE573FB7A41AB78E743180DC83FF09BD";
9
var httpBody = "0A3471C72D9BE49A8520F79C66BBD9A12FF9";
10
11
// Convert data to process
12
var key = new Buffer(secretFromConfiguration, "hex");
13
var iv = new Buffer(ivfromHttpHeader, "hex");
14
var authTag = new Buffer(authTagFromHttpHeader, "hex");
15
var cipherText = new Buffer(httpBody, "hex");
16
17
// Prepare descryption
18
var decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
19
decipher.setAuthTag(authTag);
20
21
// Decrypt
22
var result = decipher.update(cipherText) + decipher.final();
23
console.log(result);
Copied!
1
<?php
2
/* Php 7.1 or later */
3
$key_from_configuration = "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f";
4
$iv_from_http_header = "000000000000000000000000";
5
$auth_tag_from_http_header = "CE573FB7A41AB78E743180DC83FF09BD";
6
$http_body = "0A3471C72D9BE49A8520F79C66BBD9A12FF9";
7
8
$key = hex2bin($key_from_configuration);
9
$iv = hex2bin($iv_from_http_header);
10
$auth_tag = hex2bin($auth_tag_from_http_header);
11
$cipher_text = hex2bin($http_body);
12
13
$result = openssl_decrypt($cipher_text, "aes-256-gcm", $key, OPENSSL_RAW_DATA, $iv, $auth_tag);
14
print($result);
15
16
/* Php prior to 7.1 */
17
/* Please refer Using Libsodium in PHP Projects */
18
$key_from_configuration = "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f";
19
$iv_from_http_header = "000000000000000000000000";
20
$auth_tag_from_http_header = "CE573FB7A41AB78E743180DC83FF09BD";
21
$http_body = "0A3471C72D9BE49A8520F79C66BBD9A12FF9";
22
23
$key = hex2bin($key_from_configuration);
24
$iv = hex2bin($iv_from_http_header);
25
$cipher_text = hex2bin($http_body . $auth_tag_from_http_header);
26
27
$result = \Sodium\crypto_aead_aes256gcm_decrypt($cipher_text, NULL, $iv, $key);
28
print($result);
29
?>
Copied!
1
import os
2
import binascii
3
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
4
from cryptography.hazmat.backends import default_backend
5
6
# Data from configuration
7
key_from_configuration = "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
8
9
# Data from server
10
iv_from_http_header = "000000000000000000000000"
11
auth_tag_from_http_header = "CE573FB7A41AB78E743180DC83FF09BD"
12
http_body = "0A3471C72D9BE49A8520F79C66BBD9A12FF9"
13
14
# Convert data to process
15
key = binascii.unhexlify(key_from_configuration)
16
iv = binascii.unhexlify(iv_from_http_header)
17
auth_tag = binascii.unhexlify(auth_tag_from_http_header)
18
cipher_text = binascii.unhexlify(http_body)
19
20
# Prepare decryption
21
decryptor = Cipher(algorithms.AES(key), modes.GCM(iv, auth_tag), backend = default_backend()).decryptor()
22
23
# Decrypt
24
result = decryptor.update(cipher_text) + decryptor.finalize()
25
print(result)
Copied!
1
require("openssl")
2
3
# Convert hexadecimal string
4
def convert(hex)
5
return [hex].pack("H*")
6
end
7
8
# Create new decipher
9
def new_decipher(key, iv)
10
cipher = OpenSSL::Cipher.new("aes-256-gcm")
11
cipher.decrypt
12
cipher.key = key
13
cipher.iv = iv
14
15
return cipher
16
end
17
18
# Data from configuration
19
key_from_configuration = "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
20
21
# Data from server
22
iv_from_http_header = "000000000000000000000000"
23
auth_tag_from_http_header = "CE573FB7A41AB78E743180DC83FF09BD"
24
http_body = "0A3471C72D9BE49A8520F79C66BBD9A12FF9"
25
26
# Convert data to process
27
key = convert(key_from_configuration)
28
iv = convert(iv_from_http_header)
29
auth_tag = convert(auth_tag_from_http_header)
30
cipher_text = convert(http_body)
31
32
# Prepare decryption
33
decipher = new_decipher(key, iv)
34
decipher.auth_tag = auth_tag
35
36
# Decrypt
37
result = decipher.update(cipher_text) + decipher.final
38
puts result
Copied!
1
import java.nio.charset.Charset;
2
import java.security.Security
3
import java.security.SecureRandom;
4
import javax.crypto.spec.IvParameterSpec;
5
import javax.crypto.spec.SecretKeySpec;
6
import javax.xml.bind.DatatypeConverter;
7
import org.bouncycastle.jce.provider.BouncyCastleProvider
8
9
// For Java and JVM-based languages, you might need to install unrestricted policy file for JVM,
10
// which is provided by Sun. Please refer BouncyCastle FAQ if you get
11
// java.lang.SecurityException: Unsupported keysize or algorithm parameters or
12
// java.security.InvalidKeyException: Illegal key size.
13
14
// If you cannot install unrestricted policy file for JVM because of some reason, you can try with reflection: See here.
15
16
object Cipher {
17
def main(args: Array[String]) = {
18
Security.addProvider(new BouncyCastleProvider())
19
20
// Data from configuration
21
val keyFromConfiguration = "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"
22
23
// Data from server
24
val ivFromHttpHeader = "000000000000000000000000"
25
val authTagFromHttpHeader = "CE573FB7A41AB78E743180DC83FF09BD"
26
val httpBody = "0A3471C72D9BE49A8520F79C66BBD9A12FF9"
27
28
// Convert data to process
29
val key = hexToBin(keyFromConfiguration)
30
val iv = hexToBin(ivFromHttpHeader)
31
val authTag = hexToBin(authTagFromHttpHeader)
32
val encryptedText = hexToBin(httpBody)
33
34
// Unlike other programming language, We have to append auth tag at the end of encrypted text
35
val cipherText = encryptedText ++ authTag
36
37
// Prepare decryption
38
val keySpec = new SecretKeySpec(key, 0, 32, "AES")
39
val cipher = javax.crypto.Cipher.getInstance("AES/GCM/NoPadding")
40
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv))
41
42
// Decrypt
43
val result = cipher.doFinal(cipherText)
44
println(new String(result, "UTF-8"))
45
}
46
47
def hexToBin(hex: String) : Array[Byte] = {
48
return DatatypeConverter.parseHexBinary(hex)
49
}
50
}
Copied!
Last modified 1yr ago
Copy link