ElGamal 暗号プログラム
機能概要
Java言語で記述された ElGamal 暗号プログラムのサンプルです.
ElGamal 暗号の暗号化と復号を行います.また,共通鍵暗号 (AES) と組合わせてハイブリッド暗号機能を実現します.
利用上の注意点は,以下です.
プログラム
クラス
- ElGamal クラス (ElGamal.java)
ElGamal 暗号およびハイブリッド暗号のアルゴリズムを実装するクラスであり,以下のメソッドを持つ.
- ElGamal()
コンストラクタである.
- int getBlockSize()
暗号のブロック長を返す.ブロック長は,パラメータ p のバイト長であり,ElGamal 暗号 1 回で暗号化できる平文の長さになる.
- int getCipherBlockSize()
暗号化データの長さを返す.
- void initEncrypt(Key key, SecureRandom rng)
暗号化のための初期化を行う.公開鍵 key と乱数ジェネレータ rng を与える.
- void initDecrypt(Key key)
復号のための初期化を行う.秘密鍵 key を与える.
- void initDecrypt(MPInt p, MPInt g)
復号のための初期化を行う.パラメータ p,g を与える.
- void encrypt(byte[] in, int inOffset, int inLen, byte[] out, int outOffset)
in の inOffset から始まる長さ inLen のデータを暗号化して,out の outOffset から始まる領域に格納する. out の領域は,事前に確保されている必要がある.
- int decrypt(byte[] in, int inOffset, byte[] out, int outOffset)
in の inOffset から始まるデータを復号して,out の outOffset から始まる領域に格納する. out の領域は,事前に確保されている必要がある.復号データの長さ (バイト長) が返る.
- byte[] encrypt_hybrid(byte[] src, int len)
領域 src からの len バイトをハイブリッド暗号化し,暗号文を返す.
- byte[] decrypt_hybrid(byte[] src, int len)
領域 src からの len バイトをハイブリッド暗号で復号し,復号結果を返す.
- byte[] encrypt(byte[] src, int len)
領域 src からの len バイトを暗号化し,暗号文を返す. 入力データは,(ブロック長 - 2) の単位に分割され暗号化される.各ブロックの先頭バイトには,パディング 0x80 が付加される.
- byte[] decrypt(byte[] src, int len)
領域 src からの len バイトを復号し,復号結果を返す.
- ElGamalPrivateKey クラス (ElGamalPrivateKey.java)
ElGamal 暗号の秘密鍵を保持するクラスであり,以下のメソッドを持つ.
- ElGamalPublicKey(MPInt p, MPInt g, MPInt publicKey, MPInt secretKey)
コンストラクタである.モジュラス p,生成元 g,公開鍵 publicKey,および秘密鍵 secretKey を与える.
- String getAlgorithm()
アルゴリズム名 ("ElGamal") を返す.
- MPInt getGenerator()
システムパラメータの生成元 g を返す.
- MPInt getModulus()
システムパラメータの素数(モジュラス) p を返す.
- MPInt getPrivate()
秘密鍵 x を返す.
- MPInt getPublic()
公開鍵 y を返す.
- String toString()
鍵データ (p, g, y, x) の文字表現を返す.
- ElGamalPublicKey クラス (ElGamalPublicKey.java)
ElGamal 暗号の公開鍵を保持するクラスであり,以下のメソッドを持つ.
- ElGamalPublicKey(MPInt p, MPInt g, MPInt publicKey)
コンストラクタである.モジュラス p,生成元 g,および公開鍵 publicKey を与える.
- String getAlgorithm()
アルゴリズム名 ("ElGamal") を返す.
- MPInt getGenerator()
システムパラメータの生成元 g を返す.
- MPInt getModulus()
システムパラメータの素数(モジュラス) p を返す.
- MPInt getPublic()
公開鍵 y を返す.
- String toString()
鍵データ (p, g, y) の文字表現を返す.