Base64 decode in db2 sql - sql

Is it possible to decode a Base64 encoded string in DB2 database?
I was able to decode the string in SQL Server by casting to XML.
I am running db2 in Linux server

z/OS:
BASE64ENCODE and BASE64DECODE
Last Updated: 2022-08-23
The BASE64ENCODE and BASE64DECODE helper REST functions complete Base64 encoding or decoding of the provided text.
Tip: The sample HTTP user-defined functions are intended to be used within Db2 SQL applications to access remote non-Db2 REST-based services through SQL statements. Do not confuse them with Db2 native REST services, which supports using a REST-based interface to interact with Db2 data from web, mobile, and cloud applications.
The schema is DB2XML.
text
Specifies the text to encode or decode. For BASE64ENCODE, this argument is provided as a VARCHAR(2732) value and the function returns a Base64-encoded string. For BASE64DECODE, this argument is provided as a Base64-encoded VARCHAR(4096) value and the function returns the data as binary.
IBMi:
BASE64DECODE scalar function Last Updated: 2022-05-03
The BASE64DECODE function returns a character string that has been Base64 decoded. Base64 encoding is widely used to represent binary data as a string.
The schema is SYSTOOLS.
character-string
A character string in CCSID 1208 that is currently Base64 encoded. The length cannot exceed 4096 characters.
The result of the function is a varying length character for bit data string that contains character-string after being Base64 decoded.
Example
Decode a binary string that was originally X'1122334455'. The result is the original value.
VALUES SYSTOOLS.BASE64DECODE('ESIzRFU=');

-- encoding
values regexp_replace (xmlserialize (xmlelement (name "a", blob (X'1122334455')) as varchar (20)), '^<a>(.*)</a>$', '$1')
1
ESIzRFU=
-- decoding (hex function use is just to get a string representation of a binary value)
values hex (xmlcast (xmltext ('ESIzRFU=') as blob (20)))
1
1122334455

Related

How to convert string with German characters to Blob in Firebird?

I want to convert a string into a blob with the f_strblob(CSTRING) function of FreeAdhocUDF. At this point I do not find a way to get my special characters like ß or ä shown in the blob.
The result of f_strblob('Gemäß') is Gem..
I tried to change the character set to UTF8 of my variables, but that does not help.
Is there a masking option which I did not find?
You don't need that function, and the FreeAdhocUDF documentation also marks it as obsolete for that reason.
In a lot of situations, Firebird will automatically convert string literals to blobs (eg in statements where a string literal is assigned to a blob value), and otherwise you can explicitly cast using cast('your string' as blob sub_type text).

EncryptByPassPhrase returns special characters

I'm trying to encrypt with EncryptByPassPhrase in SQL Server 2012 but when I execute this function I get values like "öK{8+¨´¡¿" ... maybe someone can help me?.
This is the code that i'm using:
IF(#MODE = 1)
BEGIN
SET #RESUL = convert(varchar(100),ENCRYPTBYPASSPHRASE('Prueba','200000'))<br>
PRINT 'ENCRYPT'+ (CAST(#RESUL AS varchar(20)))
END
Let's break it down. According to the documentation, the output of ENCRYPTBYPASSPHRASE() is varbinary. You're CONVERTing that to varchar. According to the documenation for convert, if you don't provide a style, convert "Translates ASCII characters to binary bytes or binary bytes to ASCII characters. Each character or byte is converted 1:1.". If you're looking for something more like 0x123abc, pass an additional parameter (1) to CONVERT to make it do that.
All that said, unless you need a human to be able to transcribe the encrypted content (or otherwise interpret it), I'd leave it in its varbinary representation. Less room for error on the decryption side. Specifically:
DECLARE #resul VARBINARY(8000);
SET #RESUL = ENCRYPTBYPASSPHRASE('Prueba','200000');
SELECT CAST(DECRYPTBYPASSPHRASE('Prueba', #resul) AS VARCHAR(50));

Inserting string as regular string in mongodb

The pymongo documentation says that BSON strings are UTF-8 encoded so PyMongo must ensure that any strings it stores contain only valid UTF-8 data. Unicode strings (<type ‘unicode’>) are encoded UTF-8 first. The reason our example string is represented in the Python shell as u’Mike’ instead of ‘Mike’ is that PyMongo decodes each BSON string to a Python unicode string, not a regular str.
So I understand that to get rid of the Unicode literal 'u', I will have to call json.dumps() on the document returned by the query.
The documentation also says that Regular strings (<type ‘str’>) are validated and stored unaltered. And I am assuming that the query result also throws it back as a regular string and not a Unicode string.
I created a dictionary with regular string types and inserted it in DB and when I retrieve it, I get the strings as Unicode. Any idea on how do I do it? The purpose is to avoid calling json.dumps() on the query result. I need to fetch large number of documents from the DB and json.dumps() is taking quite some time. The strings that I am storing contain ASCII data so I don't need Unicode strings.
The assumption that the regular string is returned back as regular string was not correct. It is stored unaltered and not encoded to UTF-8 because it is already UTF-8. While decoding during the query, everything is converted back to Unicode.
Source:
Automatic string to unicode object conversion
How can I get pymongo to always return str and not unicode?

How to generate a GUID string in the same format as Advantage Database Server?

I have an advantage database V11 in which I currently store a unique identifier which is a GUID string generated 22 characters long using NewIDString('F').
The snippet from the ADS help is as follows :-
"F or File – A GUID encoded as a 22-byte string using File and URL Safe base64 encoding with the format of xxxxxxxxxxxxxxxxxxxxxx. Base64 encoded strings are case sensitive and should not be stored in case insensitive string fields."
Is it possible to generate the same format GUID string in Microsoft SQL?
If not, the ADS Help file lists other formats as follows :-
NEWIDSTRING
Returns a GUID formatted as a string. If the format parameter is not specified, the GUID string is formatted as a hexadecimal string with the following pattern xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. The format parameter can be the following values:
M or MIME – A GUID encoded as a 24-byte string using MIME base64 encoding with the format of xxxxxxxxxxxxxxxxxxxxxxxx. Base64 encoded strings are case sensitive and should not be stored in case insensitive string fields.
F or File – A GUID encoded as a 22-byte string using File and URL Safe base64 encoding with the format of xxxxxxxxxxxxxxxxxxxxxx. Base64 encoded strings are case sensitive and should not be stored in case insensitive string fields.
N or Numbers – A GUID encoded as a 32-byte hexadecimal string value with a format of xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
D or Delimited – A GUID encoded as a 32-byte hexadecimal string with a format of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
B or Bracketed – A GUID encoded as a 32-byte hexadecimal string with a format of [xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx]
P or Parenthesis – A GUID encoded as a 32-byte hexadecimal string with a format of (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
C or Curlybraces – A GUID encoded as a 32-byte hexadecimal string with a format of {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
Can MS-SQL generate one of these?
Regards
Mike
In SQL SerVer the NEWID function creates new GUIDs (the SQL Server data type is called UNIQUEIDENTIFIER). A GUID is a binary value, the various formats you mention in your question are just that, merely different string representations of the binary value. In the MSDN page for NEWID example A shows the standard string representation of a GUID as XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
Creationg BASE64 encoded strings in SQL-Server is not straight forward, but possible.
So everything you need to create a GUID in the form File format from ADS can be produced by SQL-Server.
NEWIDSTRING(M):
-- Generate a GUID in the format "1Jg/YZ7tNkG7PQr1wVe7KA=="
SELECT
CAST(N'' AS XML).value(
'xs:base64Binary(xs:hexBinary(sql:column("bin")))'
, 'VARCHAR(MAX)'
) AS "NEWIDSTRING_M"
FROM (
SELECT CAST(NEWID() AS VARBINARY) AS bin
) AS bin_sql_server_temp;
NEWIDSTRING(F):
-- Generate a GUID in the format "1Jg/YZ7tNkG7PQr1wVe7KA"
SELECT
LEFT(CAST(N'' AS XML).value(
'xs:base64Binary(xs:hexBinary(sql:column("bin")))'
, 'VARCHAR(MAX)'
), 22) AS "NEWIDSTRING_F"
FROM (
SELECT CAST(NEWID() AS VARBINARY) AS bin
) AS bin_sql_server_temp;
Where F is just M with the last two characters (which are always "==") removed.

Convert EBCDIC to UTF8 in vb.net

I want to convert an ebcdic string to a utf8 string. I use the below online tool to test this, which is very good, for conversion related stuffs.
http://kanjidict.stc.cx/recode.php
I want to convert £ˆ‰¢‰¢¤£†ø which is in EBCDIC to UTF8 string, which is thisisutf8 You can use the above link to test.
I refered the below article on how to read EBCDIC data in .net
http://www.codeproject.com/KB/vb/EasyEbcdicToAscii.aspx
Then I used the same method to read the ebcdic data
Dim encoding As System.Text.Encoding = _
System.Text.Encoding.GetEncoding(37)
However I am not getting the expected data
Here is my code, to get the result into a atring, a.
Dim a As String = System.Text.Encoding.UTF8.GetString(System.Text.Encoding.GetEncoding(37).GetBytes("£ˆ‰¢‰¢¤£†ø"))
I want to convert £ˆ‰¢‰¢¤£†ø
That's too late, looks like you already converted it to a string. You'll need to read bytes instead. Then use Encoding.GetString() to convert the ebcdic encoded bytes to a .NET string. Then use Encoding.UTF8.GetBytes() to convert that back to bytes.
Alternatively, and more practically, use the StreamReader(string, Encoding) overload to open the ebcdic file, passing the ebcdic Encoding. And StreamWriter(string) to write it back, it already defaults to utf-8.