1 19 20 package com.maverick.crypto.encoders; 21 22 25 public class HexTranslator 26 { 27 private static final byte[] hexTable = 28 { 29 (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', 30 (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f' 31 }; 32 33 37 public int getEncodedBlockSize() 38 { 39 return 2; 40 } 41 42 public int encode( 43 byte[] in, 44 int inOff, 45 int length, 46 byte[] out, 47 int outOff) 48 { 49 for (int i = 0, j = 0; i < length; i++, j += 2) 50 { 51 out[outOff + j] = hexTable[(in[inOff] >> 4) & 0x0f]; 52 out[outOff + j + 1] = hexTable[in[inOff] & 0x0f]; 53 54 inOff++; 55 } 56 57 return length * 2; 58 } 59 60 64 public int getDecodedBlockSize() 65 { 66 return 1; 67 } 68 69 public int decode( 70 byte[] in, 71 int inOff, 72 int length, 73 byte[] out, 74 int outOff) 75 { 76 int halfLength = length / 2; 77 byte left, right; 78 for (int i = 0; i < halfLength; i++) 79 { 80 left = in[inOff + i * 2]; 81 right = in[inOff + i * 2 + 1]; 82 83 if (left < (byte)'a') 84 { 85 out[outOff] = (byte)((left - '0') << 4); 86 } 87 else 88 { 89 out[outOff] = (byte)((left - 'a' + 10) << 4); 90 } 91 if (right < (byte)'a') 92 { 93 out[outOff] += (byte)(right - '0'); 94 } 95 else 96 { 97 out[outOff] += (byte)(right - 'a' + 10); 98 } 99 100 outOff++; 101 } 102 103 return halfLength; 104 } 105 } 106 | Popular Tags |