TNB Library
TnbBytesCipher.h
[詳解]
1#pragma once
13#include "TnbStr.h"
14#include "TnbSlideCompressor.h"
15
16
17
18//TNB Library
19namespace TNB
20{
21
22
23
39{
40 CAscii m_ascKey;
41 bool m_boIsCompressionEnable;
42
50 bool m_Add(ICollectionT<BYTE>& _out, const IConstCollectionT<BYTE>& in, bool boIsAdd = true)
51 {
52 if ( m_ascKey.IsEmpty() )
53 {
54 ASSERT0(false, "CBytesCipher", "暗号、復号キーが指定されていません。");
55 throw CEmptyException();
56 }
57 bool r = true;
58 const BYTE* B = reinterpret_cast<const BYTE*>(LPCSTR(m_ascKey));
59 size_t iKeySize = m_ascKey.GetLength();
60 size_t base = _out.GetSize();
61 _out.SetSize(base + in.GetSize());
62 if ( boIsAdd )
63 {
64 loop ( i, in.GetSize() )
65 {
66 r &= _out.Set(base + i, static_cast<BYTE>(in.At(i) + B[i % iKeySize]));
67 }
68 }
69 else
70 {
71 loop ( i, in.GetSize() )
72 {
73 r &= _out.Set(base + i, static_cast<BYTE>(in.At(i) - B[i % iKeySize]));
74 }
75 }
76 return r;
77 }
78
79public:
80
82 CBytesCipher(void) : m_boIsCompressionEnable(false)
83 {
84 }
85
92 void SetKey(const CAscii& ascKey)
93 {
94 m_ascKey = ascKey;
95 }
96
104 void Compression(bool boIsEnable)
105 {
106 m_boIsCompressionEnable = boIsEnable;
107 }
108
119 {
120 EXCLUSIVE2(&_out, &in);
121 bool r = false;
122 if ( m_boIsCompressionEnable )
123 {
124 CSlideCompressor comp;
125 CByteVector vb;
126 if ( comp.Encode(vb, in) )
127 {
128 r = m_Add(_out, vb);
129 }
130 }
131 else
132 {
133 r = m_Add(_out, in);
134 }
135 return r;
136 }
137
146 {
147 CByteVector vb;
148 if ( ! Encode(vb, c) )
149 {
150 vb.Invalid();
151 }
152 return vb;
153 }
154
165 {
166 EXCLUSIVE2(&_out, &in);
167 bool r = false;
168 if ( m_boIsCompressionEnable )
169 {
170 CSlideCompressor comp;
171 CByteVector vb;
172 if ( m_Add(vb, in, false) )
173 {
174 r = comp.Decode(_out, vb);
175 }
176 }
177 else
178 {
179 r = m_Add(_out, in, false);
180 }
181 return r;
182 }
183
192 {
193 CByteVector vb;
194 if ( ! Decode(vb, c) )
195 {
196 vb.Invalid();
197 }
198 return vb;
199 }
200};
201
202
203
204};//TNB
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
圧縮情報関係のヘッダ
文字列管理関係のヘッダ
情報暗号復号クラス
bool Decode(ICollectionT< BYTE > &_out, const IConstCollectionT< BYTE > &in)
[設定] 復号化
bool Encode(ICollectionT< BYTE > &_out, const IConstCollectionT< BYTE > &in)
[設定] 暗号化
void SetKey(const CAscii &ascKey)
[設定] キー設定.
CBytesCipher(void)
コンストラクタ
CByteVector Encode(const IConstCollectionT< BYTE > &c)
[設定] 暗号化
void Compression(bool boIsEnable)
[設定] 圧縮モード.
CByteVector Decode(const IConstCollectionT< BYTE > &c)
[設定] 復号化
取得要素(空き)無し例外
Definition: TnbException.h:107
圧縮展開処理クラス
bool Decode(ICollectionT< BYTE > &_out, const IConstCollectionT< BYTE > &in)
[処理] 展開
bool Encode(ICollectionT< BYTE > &_out, const IConstCollectionT< BYTE > &in, bool boIsPlain=false)
[処理] 圧縮
bool IsEmpty(void) const
[確認] 空チェック
Definition: TnbStr.h:528
size_t GetLength(void) const
[取得] 文字列長
Definition: TnbStr.h:518
void Invalid(void)
[操作] 無効状態にする
Definition: TnbVector.h:604
#define EXCLUSIVE2(CLS1, CLS2)
簡易排他ツイン制御マクロ.
Definition: TnbSync.h:820
TNB Library
Definition: TnbDoxyTitle.txt:2
virtual bool Set(INDEX index, const TYP &t)=0
[設定] 要素の設定.
virtual bool SetSize(size_t size)
[操作] サイズ指定
virtual const TYP & At(INDEX index) const =0
[取得] 要素の参照取得.
virtual size_t GetSize(void) const =0
[取得] 要素数取得.