问题描述
在Oracle中解码Base64字符串(NVARCHAR2
)时,我试图获得与Java中相同的结果.
I am trying to get the same result I am getting in Java when decoding a Base64 String (NVARCHAR2
) in Oracle.
我确实具有以下字符串:N'AAECAwQFBgdNu+qx2ZcqsA=='
基本上是打包为Base64编码的加密密码.
I do have the following String: N'AAECAwQFBgdNu+qx2ZcqsA=='
which is basically a encrypted password packed into a Base64 encoding.
在Java中,我正在这样做:
In Java I am doing this:
byte[] bytes = Base64.decodeBase64(testValue.getBytes());
for(byte b: bytes){
System.out.print(String.valueOf(b & 0xFF) + " "); //Decimal (unsigned)
}
for(byte b: bytes){
System.out.print(Integer.toHexString(b & 0xFF) + " "); //Hex
}
其中显示以下内容:
Unsigned: 0 1 2 3 4 5 6 7 77 187 234 177 217 151 42 176
Hex: 0 1 2 3 4 5 6 7 4d bb ea b1 d9 97 2a b0
现在,我想像在Java中一样复制Base64解码.我正在尝试在PL/SQL函数中进行以下操作:
Now I want to replicate the Base64 decoding like it's made in Java. I am trying following in a PL/SQL-Function:
base64DecodedInput:= UTL_ENCODE.base64_decode(UTL_I18N.STRING_TO_RAW(valueToDecrypt, charset));
DBMS_OUTPUT.PUT_LINE ( 'Base64 decoded is: ' || base64DecodedInput);
字符集为:AL16UTF16而valueToDecrypt是我上面以NVARCHAR2(200)
The charset is: AL16UTF16and valueToDecrypt is the test String I mentioned above as NVARCHAR2(200)
DBMS_OUTPUT打印我:
The DBMS_OUTPUT prints me:
Base64 decoded is: 0000004200300105A8308FDD02FE02B11B7901EA02C0
错了!...
我想念什么. PL/SQL是否不使用URL安全解码或其他功能?
What did I miss. Is PL/SQL not using url safe decoding or whatever?
我被困住了.
该代码是PL/SQL函数的一部分:
The code is part of a PL/SQL-Function:
CREATE OR REPLACE FUNCTION decodeBase64
(valueToDecrypt IN NVARCHAR2)
RETURN NVARCHAR2
AS
--Declaration area
charset VARCHAR(100);
base64DecodedInput RAW(2000);
...
服务器/数据库设置为:
And Server/Database Settings are:
NLS_RDBMS_VERSION 12.1.0.2.0
NLS_NCHAR_CONV_EXCP FALSE
NLS_LENGTH_SEMANTICS BYTE
NLS_COMP BINARY
NLS_DUAL_CURRENCY $
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_FORMAT HH.MI.SSXFF AM
NLS_SORT BINARY
NLS_DATE_LANGUAGE AMERICAN
NLS_DATE_FORMAT DD-MON-RR
NLS_CALENDAR GREGORIAN
NLS_NUMERIC_CHARACTERS .,
NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_CHARACTERSET WE8MSWIN1252
NLS_ISO_CURRENCY AMERICA
NLS_CURRENCY $
NLS_TERRITORY AMERICA
NLS_LANGUAGE AMERICAN
推荐答案
看看 CAST_TO_RAW 文档:
您应该使用这个:
base64DecodedInput:= UTL_ENCODE.base64_decode(UTL_RAW.CAST_TO_RAW(TO_CHAR(valueToDecrypt)));
DBMS_OUTPUT.PUT_LINE ( 'Base64 decoded is: ' || base64DecodedInput);
00010203040506074DBBEAB1D9972AB0
(格式化的)应该等于期望的结果:
Which (formatted) should be equal to desired result:
00 01 02 03 04 05 06 07 4D BB EA B1 D9 97 2A B0
对于BASE64字符串使用NVARCHAR2
是完全没有用的. BASE64编码的主要目的是仅用ASCII字符表示任意数据,因此NVARCHAR2没有意义.由于BASE64仅包含ASCII,因此您不必担心CAST(... AS VARCHAR2(200))
处的任何字符编码.
It is quite useless to use NVARCHAR2
for a BASE64 string. The main purpose of BASE64 encoding is to represent arbitrary data only with ASCII characters, so NVARCHAR2 makes no sense. Since BASE64 contains only ASCII you don't have to worry about any character encoding at CAST(... AS VARCHAR2(200))
.
当然,对于解码的字符串数据类型,NVARCHAR2
可能非常有用.
Of course, for the decoded string data type NVARCHAR2
might be very useful.
这篇关于在PL/SQL中复制Java的Base64解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!