Symmetric-Key Cryptographyは、データのエンコードとデコードに同じキーが使用される暗号化システムです。鍵の安全な配布は、この方法の欠点の1つですが、セキュリティに欠けているものは、時間の複雑さを増やします。

暗号化アルゴリズムは一般に知られており、「暗黙のセキュリティ」に依存していないと常に仮定する必要があります。最も一般的な対称アルゴリズムは、DES、トリプルDES、AES、ブローフィッシュ、RC2、RC4(ARCFOUR)、RC5、RC6です。

1.対称鍵暗号の使用事例

以下では、対称キー暗号化を使用してプリセットされたディレクトリを暗号化または復号化するアプリケーションのコードを確認できます。コンストラクタは、パスワード、キーの長さ、および暗号に使用されるアルゴリズムで初期化されます。各アルゴリズムのキーの長さの詳細は、https://docs.oracle.com/javase/7/docs/technotes/guides/security/SunProviders.html[暗号アルゴリズムの制限のインポート]を参照してください。

この例では、スピードとセキュリティの間にシルバーライニングと見なされるため、AESを使用しています。

SymmetricKeyExample.java

package com.techfou.symmetric;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JOptionPane;

public class SymmetricKeyExample {
    private SecretKeySpec secretKey;
    private Cipher cipher;

    public SymmetricKeyExample(String secret, int length, String algorithm)
            throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException {
        byte[]key = new byte[length];
        key = fixSecret(secret, length);
        this.secretKey = new SecretKeySpec(key, algorithm);
        this.cipher = Cipher.getInstance(algorithm);
    }

    private byte[]fixSecret(String s, int length) throws UnsupportedEncodingException {
        if (s.length() < length) {
            int missingLength = length - s.length();
            for (int i = 0; i < missingLength; i++) {
                s += " ";
            }
        }
        return s.substring(0, length).getBytes("UTF-8");
    }

    public void encryptFile(File f)
            throws InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {
        System.out.println("Encrypting file: " + f.getName());
        this.cipher.init(Cipher.ENCRYPT__MODE, this.secretKey);
        this.writeToFile(f);
    }

    public void decryptFile(File f)
            throws InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {
        System.out.println("Decrypting file: " + f.getName());
        this.cipher.init(Cipher.DECRYPT__MODE, this.secretKey);
        this.writeToFile(f);
    }

    public void writeToFile(File f) throws IOException, IllegalBlockSizeException, BadPaddingException {
        FileInputStream in = new FileInputStream(f);
        byte[]input = new byte[(int) f.length()];
        in.read(input);

        FileOutputStream out = new FileOutputStream(f);
        byte[]output = this.cipher.doFinal(input);
        out.write(output);

        out.flush();
        out.close();
        in.close();
    }

    public static void main(String[]args) {
        File dir = new File("src/cryptodir");
        File[]filelist = dir.listFiles();

        SymmetricKeyExample ske;
        try {
            ske = new SymmetricKeyExample("!@#$MySecr3tPassw0rd", 16, "AES");

            int choice = -2;
            while (choice != -1) {
                String[]options = { "Encrypt All", "Decrypt All", "Exit" };
                choice = JOptionPane.showOptionDialog(null, "Select an option", "Options", 0,
                        JOptionPane.QUESTION__MESSAGE, null, options, options[0]);

                switch (choice) {
                case 0:
                    Arrays.asList(filelist).forEach(file -> {
                        try {
                            ske.encryptFile(file);
                        } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException
                                | IOException e) {
                            System.err.println("Couldn't encrypt " + file.getName() + ": " + e.getMessage());
                        }
                    });
                    System.out.println("Files encrypted successfully");
                    break;
                case 1:
                    Arrays.asList(filelist).forEach(file -> {
                        try {
                            ske.decryptFile(file);
                        } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException
                                | IOException e) {
                            System.err.println("Couldn't decrypt " + file.getName() + ": " + e.getMessage());
                        }
                    });
                    System.out.println("Files decrypted successfully");
                    break;
                default:
                    choice = -1;
                    break;
                }
            }
        } catch (UnsupportedEncodingException ex) {
            System.err.println("Couldn't create key: " + ex.getMessage());
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            System.err.println(e.getMessage());
        }
    }
}

出力


java-symmetrickey-output-1

「すべて暗号化」ボタンがクリックされたとき。

Encrypting file: Hello world.docx
Encrypting file: Hello world.pdf
Encrypting file: Smiley.png
Encrypting file: Text.txt
Files encrypted successfully

「すべて復号化」ボタンがクリックされたとき。

Decrypting file: Hello world.docx
Decrypting file: Hello world.pdf
Decrypting file: Smiley.png
Decrypting file: Text.txt
Files decrypted successfully

元のテキストファイル。

src/Text.txt

This is a text file.

テキストファイルは暗号化されています。

src/Text.txt

‡(?ê?z@ou7ÿ—pø"é3.Õ0Ò;jVi¶‚

このコードを実行する前に、この例の目的のためだけに、ファイルを含む別のディレクトリを作成してください。あなたがしていることを正確に知らなければ、以下のソースコードをダウンロードしてください。

2.プロジェクトディレクトリ


java-symmetrickey-example

ソースコードをダウンロードする

ダウンロードする – リンク://wp-content/uploads/2016/11/SymmetricCryptographyExample.zip[SymmetricCryptographyExample.zip(133 KB)]

参考文献


  1. https://en.wikipedia.org/wiki/Security


    through

    obscurity[Security

あいまいさを通して]。

http://www.cs.wustl.edu/~jain/cse567-06/ftp/encryption


perf/#2

5[Performance

データ暗号化アルゴリズムの分析:2.5比較アルゴリズム]。

https://docs.oracle.com/javase/7/docs/technotes/guides/security/SunProviders.html

[Import

アルゴリズム]


aes


cryptography


encryption


guest post

リンク://タグ/java-security/[javaセキュリティ]リンク://タグ/java-crypto/[java.crypto]

セキュリティ

リンク://タグ/対称キー/[対称キー]