In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class encode_decode
{
public static string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes = System.Text.Encoding.UTF8.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
public static string DecodeFrom64(string encodedData)
{
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = System.Text.Encoding.UTF8.GetString(encodedDataAsBytes);
return returnValue;
}
}
In SQL server
CREATE FUNCTION [dbo].[fn_base64_encode_decode]
(
@in_mode INT ,
@in_input VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @encoded_decode_base64 VARCHAR(MAX),@source VARBINARY(MAX)
IF @in_mode = 1 --ENCODE
BEGIN
SET @source = CONVERT(VARBINARY(MAX), @in_input)
SELECT @encoded_decode_base64 = CAST(N'' AS
xml).value('xs:base64Binary(sql:variable("@source"))', 'VARCHAR(MAX)')
END
IF @in_mode = 2 --DECODE
BEGIN
SELECT @encoded_decode_base64 = CAST(N'' AS
xml).value('xs:base64Binary(sql:variable("@in_input"))', 'VARBINARY(MAX)')
END
RETURN @encoded_decode_base64
END
No comments:
Post a Comment