'解密
Private Function Decode6Bit(ByVal bytes() As Byte) As Byte()
Dim i As Integer, j As Integer, m As Integer = 0
Dim bLen As Integer = bytes.Length Mod 4
If bLen <> 0 Then
bLen = 4 - bLen
ReDim Preserve bytes(bytes.Length + bLen - 1)
End If
Dim _sBytes((bytes.Length * 3) / 4 - 1) As Byte
For i = 0 To bytes.Length - 1 Step 4
'将数组里的每一数减去 &H3C(60)
For j = i To i + 3
If bytes(j) >= &H3C Then
bytes(j) = bytes(j) - &H3C
End If
Next
'四个字节的数据解密后为3个字节,
'例:未解密的字符串:Ysap
' 解密后的字符串:wyt
'方法:
'w = Y << 2 + (s << 2) >> 6
'y = s << 4 + (a << 2) >> 4
't = a << 6 + (p << 2) >> 2
_sBytes(m) = (bytes(i) << 2) + ((bytes(i + 1) << 2) >> 6)
_sBytes(m + 1) = (bytes(i + 1) << 4) + ((bytes(i + 2) << 2) >> 4)
_sBytes(m + 2) = (bytes(i + 2) << 6) + ((bytes(i + 3) << 2) >> 2)
m += 3
Next
Return _sBytes
End Function
'加密
Private Function Encode6Bit(ByVal bytes() As Byte) As Byte()
Dim i As Integer, m As Integer = 0
Dim _Bytes() As Byte = bytes
'计算_Bytes数组的长度是否等于3的倍数
'假设_Bytes的长度 = 10 ,则 n = 2 ,_Bytes.Length + n = 3的倍数
Dim n As Integer = _Bytes.Length Mod 3
If n <> 0 Then
n = 3 - n
'重新定义数组的元素个数,加入 Preserve 关键字 使原始数据不受影响
ReDim Preserve _Bytes(_Bytes.Length + n - 1)
End If
Dim _EnBytes((_Bytes.Length * 4) / 3 - 1) As Byte
'将字符串以3个字节为一段,每个字节8位,共24位,将其分成4组,每一组有6位,在每组首部加2位后
'变成4组8位的,共32位
'然后每一组再加 &H3C 后,得到了加密后的字符串
'例如:未加密的字符串:123
' 加密后的字符串:HODo
For i = 0 To _Bytes.Length - 1 Step 3
_EnBytes(m) = (_Bytes(i) >> 2) + &H3C
_EnBytes(m + 1) = ((_Bytes(i) << 6) >> 2) + (_Bytes(i + 1) >> 4) + &H3C
_EnBytes(m + 2) = ((_Bytes(i + 1) << 4) >> 2) + (_Bytes(i + 2) >> 6) + &H3C
_EnBytes(m + 3) = ((_Bytes(i + 2) << 2) >> 2) + &H3C
m += 4
Next
ReDim Preserve _EnBytes(_EnBytes.Length - n - 1)
Return _EnBytes
End Function
浙公网安备 33010602011771号