Connecting to Coinex API issue - rest

I try to connect to Coinex API through a Java program
I follow exact patter that mentioned in below link for authorisation
https://github.com/coinexcom/coinex_exchange_api/wiki/012security_authorization
I MD5 has whole query string that is like below , and put result in authorization in request header parameter
tonce=1635504041595&access_id=XXXX&secret_key=YYYY
My intentions is to get account balance so my Get request URL is
https://api.coinex.com/v1//balance/info?tonce=1635504041595&access_id=XXXX
but server return below error
{"code": 25, "data": {}, "message": "Signature Incorrect"}
Anybody can advice what is the issue , thanks AndyJ

well this is my encode method:
public static String encode(String str) {
try {
// Generate a summary of MD5 encryption calculations
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes("UTF-8"));
// digest() finally determines to return the md5 hash value, returning a value of 8 as a string. Because the md5 hash value is a 16-bit hex value, it is actually an 8-bit character.
// The BigInteger function converts an 8-bit string into a 16-bit hex value, represented by a string; gets a hash value in the form of a string
String md5 = new BigInteger(1, md.digest()).toString(16);
//BigInteger will omit 0 and need to be completed to 32 bits
return fillMD5(md5).toUpperCase();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
and one more thing,
your timestmap should have 10 digits, remove the last 3 "000" like this:
Integer.parseInt(String.valueOf(timestamp).substring(0, 10));

Related

Dart is not printing hex string

I got the string \x01\x01 from a tcp/ip socket, when I try to print it to console, no output is coming
void main() {
var out = "\x01\x01";
print("printing out as --> $out <--");
final runes = out.runes.toList();
print(runes);
}
It gives the output as
printing out as --> <--
[1, 1]
dart pad link: https://dartpad.dev/?id=854e4479bfec03d7e8fd40621c845567
I tried to use hex package and it gives Non-hex character detected error.
Questions.
How do I print these types of strings to the console?
If some conversion is needed, how do I know data belongs to these type ?
my socket client is like the following
socket.listen(
// handle data from the server
(Uint8List data) async {
var serverResponse = String.fromCharCodes(data);
print('Server: $serverResponse');
final runes = serverResponse.runes.toList();
print(runes);
},
EDIT
The socket server is the x0vnc server, on reading the input with wire shark I can see the server sent 01 01
To display a hexa, you have to escape the characters like this:
var out = '\\x01\\x01';
this will work.
I suspect you have misunderstood what the server is sending.
Given you've not stated the server language I'm going to guess that ` ab = b'\x01\x01' generates a array with two bytes both with the value 1.
If you treat this as an ASCII value then 1 is a non printable character.
As such you need to iterate over the array and convert each byte into a suitable visual format.
This might mean that when you see a 1 you print x01.
Edit:
actually dart will convert an int to a string for you:
void main() {
final bytes = <int>[1, 2, 3];
for (final byte in bytes) {
print(byte.toString());
}
}

Trying to print hex string with CryptoJS

I am trying to use CryptoJS to encrypt something and then generate a hexadecimal string of the encrypted text.
function EncryptAES(text, key) {
var encrypted = CryptoJS.AES.encrypt(text, key);
return CryptoJS.enc.Hex.stringify(encrypted);
}
var encrypted = EncryptAES("Hello, World!", "SuperSecretPassword");
console.log(encrypted);
However, instead of a hexadecimal string, a blank line is printed to the console. What am I doing wrong?
CryptoJS.AES.encrypt() returns a CipherParams object that encapsulates several data, including the ciphertext as WordArray (s. here). By default, .toString() returns the hex encoded data for a WordArray:
function EncryptAES(text, key) {
var encrypted = CryptoJS.AES.encrypt(text, key);
return encrypted.ciphertext.toString()
}
var encrypted = EncryptAES("Hello, World!", "SuperSecretPassword");
console.log(encrypted);
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
Note that in your example the key material is passed as string and therefore interpreted as passphrase (s. here), inferring key and IV via a key derivation function in conjunction with a random 8 bytes salt, which is why the ciphertext changes each time for the same input data.
Therefore, decryption requires not only the ciphertext but also the salt, which is also encapsulated in the CipherParams object.
For a CipherParams object, .toString() returns the data in the Base64 encoded OpenSSL format consisting of the ASCII encoding of Salted__ followed by the 8 bytes salt and the actual ciphertext, and thus contains all the information needed for decryption.

How to - SHA1 hash of plaintext using SHA1CryptoServiceProvider in C++/CLI?

I want to hash a string but looking at the example on MSDN I got stuck on the DATA_SIZE. What is this? and how do I know ahead of time what the size of the array is if the plaintext can vary in length?
Also I need to return the result as a vector (consuming method expects this)
Code from MSDN
array<Byte>^ data = gcnew array<Byte>( DATA_SIZE );
array<Byte>^ result;
SHA1^ sha = gcnew SHA1CryptoServiceProvider;
// This is one implementation of the abstract class SHA1.
result = sha->ComputeHash( data );
My method so far looks like
std::vector<byte> sha1(const std::string& plaintext)
{
//#define SHA1_BUFFER_SIZE ????
//array<System::Byte>^ data = gcnew array<System::Byte>(DATA_SIZE);
//convert plaintext string to byte array
array<System::Byte>^ result;
SHA1^ sha = gcnew SHA1CryptoServiceProvider;
result = sha->ComputeHash(data);
//return result as a vector<byte>
}
First, answering your questions:
On the referred MSDN page, it was stated before the example:
This example assumes that there is a predefined constant DATA_SIZE.
So, it was assumed that there was a #define or a enum constant predefined
Of course you don't know the size of the plaintext. But you'll see that in this approach you don't need the DATA_SIZE constant
If you want your function to receive as input a std:string and to output a std::vector, there are a lot of steps to be done. It would look something like that:
std::vector<unsigned char> sha1(const std::string& message)
{
// Steps #1 and #2: convert from std::string to managed string
// and convert from a string to bytes (UTF-8 as example)
array<Byte>^data = Encoding::UTF8->GetBytes(gcnew String(message.c_str() ));
// Steps #3 and #4: perform hash operation
// and store result in a byte array
SHA1^ sha = gcnew SHA1CryptoServiceProvider;
array<Byte>^ array_result = sha->ComputeHash(data);
// Step #5: convert from a managed array
// to unmanaged vector
std::vector<unsigned char> vector_return(array_result->Length);
Marshal::Copy(array_result, 0, IntPtr(&vector_return[0]), array_result->Length);
return vector_return;
}
don't forget to declare the namespaces you're using:
using namespace System;
using namespace System::Text;
using namespace System::Security::Cryptography;
using namespace System::Runtime::InteropServices;
int main(array<System::String ^> ^args)
{
std::string str_test("This is just a test.");
std::vector<unsigned char> vec_digest( sha1(str_test) );
return 0;
}
Final thoughts:
It's a bit misleading to name the string you want to hash as plaintext. More accurate terms would be message (for the input data) and digest (for the hash value)
Avoid mixing the use of std::string and managed String^ if you can.

Convert a string to a byte array in PowerShell version 2

What I'm trying to do is use SHA1 UTF-8 encryption and then base64 encoding and on a password string value. However, I needed to do the encryption first, then the encoding, but I did it the other way around.
Here is the code:
# Create Input Data
$enc = [system.Text.Encoding]::UTF8
$string1 = "This is a string to hash"
$data1 = $enc.GetBytes($string1)
# Create a New SHA1 Crypto Provider
$sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
$# Now hash and display results
$result1 = $sha.ComputeHash($data1)
So, when I went to do the hashing I realized I had to have a byte[] from the string and I'm not sure how to do that. I'm thinking there is a simple way from the .Net libraries, but couldn't find an example.
So if I have a string, like:
$string = "password"
How do I convert that into a byte array that I can use on :: ComputeHash($string)?
So what I have to end up with is an encrypted SHA-1 and base 64 encoded UTF-8 password, which the code above does, but it's coming back different than when I coded this same thing in java, where I encrypted it first, then converted that result to base 64 encoding.
I'm making the assumption that while encrypting a string directly isn't supported in the api, there may be a work-around that will allow you to do this. That is what I'm attempting to do.
So I'm assuming my issue with the code is that I had to encrypt it first and then encode it to get the correct value. Correct or am I missing something here?
Here is the pertinent java code that does work:
//First method call uses a swing component to get the user entered password.
String password = getPassword();
//This line is where the work starts with the second and third methods below.
String hashed = byteToBase64(getHash(password));
//The second method call here gets the encryption.
public static byte[] getHash(String password) {
MessageDigest digest = null;
byte[] input = null;
try {
digest = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
digest.reset();
try {
input = digest.digest(password.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return input;
}
//Then the third method call here gets the encoding, FROM THE ENCRYPTED STRING.
public static String byteToBase64(byte[] data){
return new String(Base64.encodeBase64(data));
When I run the java code with the password string of "password" I get
[91, -86, 97, -28, -55, -71, 63, 63, 6, -126, 37, 11, 108, -8, 51, 27, 126, -26, -113, -40]
which is the encryption.
Then I when the encoding in java I get this:
W6ph5Mm5Pz8GgiULbPgzG37mj9g=
but when I run it in PowerShell I get this because it's encoded first for UTF8:
91 170 97 228 201 185 63 63 6 130 37 11 108 248 51 27 126 230 143 216
Then when I run this line of code to convert it I get an error:
$base64 = [System.Convert]::FromBase64String($result)
Exception calling "FromBase64String" with "1" argument(s): "Invalid length for a Base-64 char array."
At line:1 char:45
However, if I run the new line of code to make it hex from below I get:
$hexResult = [String]::Join("", ($result | % { "{0:X2}" -f $_}))
PS C:\Program Files (x86)\PowerGUI> Write-Host $hexResult
5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8
but I need to end up with this value:
W6ph5Mm5Pz8GgiULbPgzG37mj9g=
Again, this may not even be possible to do, but I'm trying to find a work-around to see.
You most likely just need to convert your hash to base64 after the last line.
$enc = [system.Text.Encoding]::UTF8
$string1 = "This is a string to hash"
$data1 = $enc.GetBytes($string1)
# Create a New SHA1 Crypto Provider
$sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
# Now hash and display results
$result1 = $sha.ComputeHash($data1)
[System.Convert]::ToBase64String($result1)
Text->Bytes->Encrypt/Hash->Base64
That's a very common pattern for sending cryptographic data in a text format.
It looks like you're on the right track. You have to pick a character encoding to convert between a string and a byte array. You picked UTF-8 above, but there are other options (e.g. ASCII, UTF-16, etc.).
Encrypting a string directly is not supported.
The problem seems to be that in first bytearray, you are have signed bytes (-86 = 10101010) and in the second one unsigned bytes (170 = 10101010).

Zend Filter Encryption / Decryption adds nullbytes when decrypting

I am trying to do some symmetric encryption on some data with the Zend_Filter_Encrypt function. The problem is if i encrypt some data and then later decrypt it, there are nullbytes behind the decrypted data and I have no idea why.
For instance:
Plaintext: test Encrypted: ����pk� Decrypted: test����
It seems to be padding nullbytes at the end of the decrypted text to make it's length equal to some 2^n (a string with 11 characters is padded to fit 16 => 2^4). The most obvious thing would be to just strip these characters but I want to know why this is happening...
This is the code I'm using, which is different than how the documentation wants you to do it because their code just doesn't work for me (see: http://framework.zend.com/manual/en/zend.filter.set.html)
define('VECTOR','EXfPCW23'); //example, not the actual used VECTOR / KEY
$key = 'lolwatlolwat';
public function encryptPassword($password, $key)
{
$filter = new Zend_Filter_Encrypt();
$filter->setEncryption(array('key' => $key));
$filter->setVector(VECTOR);
return $filter->filter($password);
}
public function decryptPassword($password, $key)
{
$filter = new Zend_Filter_Decrypt();
$filter->setEncryption(array('key' => $key));
$filter->setVector(VECTOR);
return $filter->filter($password);
}
You have to use rtrim function on Decrypt string.
Example:
public function decryptPassword($password, $key)
{
$filter = new Zend_Filter_Decrypt();
$filter->setEncryption(array('key' => $key));
$filter->setVector(VECTOR);
return rtrim($filter->filter($password);
}
Does the Zend documentation tell you how to specify the padding? If so then specify a reversible padding; as you have found, zero padding is not reversible. Otherwise, find out what size padded plaintext Zend is expecting and add PKCS7 padding yourself. This is easily recognisable and removable afterwards.