0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

単純な共通鍵をC言語で書いた

Last updated at Posted at 2020-08-07

単純な共通鍵をC言語で書いた

クソしょうもないですが、共通鍵の説明を概念図を使って説明するよりはマシかなと思いました。

^演算子は、排他的論理和です。
多分どの環境でも動くはずです。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void DecodeWithCommonKey(char *data, char key){
    int i;
    int len;
    len = strlen(data);
    for(i=0; i<len; i++){
        *(data+i) = *(data+i)^key;
    }
}
void EncodeWithCommonKey(char *data, char key){
    int i;    
    int len;
    len = strlen(data);
    for(i=0; i<len; i++){
        *(data+i) = *(data+i)^key;
    }
}
int main(){
    char key = 0x10;
    char data[]="Niwaka";

    //暗号化
    DecodeWithCommonKey(data, key);
    printf("暗号文:%s\n", data);

    //復号化
    EncodeWithCommonKey(data, key);
    printf("平文:%s\n", data);
    
    return 0;
}

keyでNiwakaを暗号化し、また復号化するという単純なプログラムです。
Niwaka文字列の1つ1つの文字に対して、keyとの排他的論理和を実行しています。
(文字コードはasciiコードのはずです。)

受信者と送信者で同じkeyを持っておかないといけないので、共通鍵を使用する場合は鍵配送問題が生じます。
受信者と送信者で共通鍵を共有しておかないといけないので、安全な方法で共通鍵を送信しなければいけません。共通鍵を安全に送信できる方法があるのでしたら、その方法でデータを送信したらいいだけですよね。(共通鍵はここが問題点)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?