如何在 Oracle 中对字符串进行编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/934371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to encode string in Oracle?
提问by Micha? Niklas
I have problem with encoding data in Oracle database. I want to xor string with another string (mask) and then encode it with base64.
我在 Oracle 数据库中编码数据时遇到问题。我想用另一个字符串(掩码)对字符串进行异或,然后用 base64 对其进行编码。
In Python this looks like:
在 Python 中,这看起来像:
def encode_str(s, mask):
xor_mask = mask
while len(xor_mask) < len(s):
xor_mask += mask
chrs2 = []
for i in range(len(s)):
chrs2.append(chr(ord(s[i]) ^ ord(xor_mask[i])))
s2 = ''.join(chrs2)
return base64.b64encode(s2)
#return binascii.hexlify(s2).lower()
In PL/SQL I got:
在 PL/SQL 我得到:
create or replace function ht_encode(str in varchar2, mask in varchar2) return varchar2 as
xor_mask varchar2(2000);
result_s varchar2(2000);
i integer;
xx integer;
x char(10);
ch1 char(10);
ch2 char(10);
chrx varchar2(10);
begin
result_s := '';
xor_mask := mask;
while length(xor_mask) < length(str) loop
xor_mask := xor_mask || mask;
end loop;
for i in 1..length(str) loop
ch1 := substr(str, i, 1);
ch2 := substr(xor_mask, i, 1);
xx := BITXOR(ascii(ch1), ascii(ch2));
x := xx;
chrx := rawtohex(x);
--result_s := result_s || ':' || chrx;
--result_s := result_s || chrx;
-- HELP ME HERE!
end loop;
--return lower(utl_encode.base64_encode(result_s));
--return result_s || ' | ' || rawtohex(result_s);
-- HELP ME HERE!
return result_s;
end;
(bitxor comes from http://forums.oracle.com/forums/thread.jspa?threadID=496773)
(bitxor 来自http://forums.oracle.com/forums/thread.jspa?threadID=496773)
I don't know how to create "binary" string and then encode it to hex or preferable to base64.
我不知道如何创建“二进制”字符串,然后将其编码为十六进制或优于 base64。
回答by Quassnoi
SELECT UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.bit_xor(UTL_RAW.cast_to_raw('text'), UTL_RAW.cast_to_raw('mask'))))
FROM dual