したらばTOP ■掲示板に戻る■ 全部 1-100 最新50 | |

#サイバーセキュリティ

10匿名:2016/03/02(水) 04:13:12 ID:R2b9cAyE0
サイバー警察- [ツイカツ警察] - したらば掲示板2ちゃんねる閉鎖議論
法務部 掲示板名称変更Googleクローラー検知あり。

/*
* AES1.java
* Copyright (C) 2015 kaoru <kaoru@localhost>
*/
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.spec.KeySpec;
import java.security.SecureRandom;
import javax.crypto.spec.IvParameterSpec;
import java.util.Base64;
public class AES1
{
enum MODE {
ENCRYPT,
DECRYPT
};
public static SecretKey ENCRYPT_KEY;
public static int KEY_SIZE = 128;
public static byte[] ENCRYPT_IV;
public static String ALGORITHM = "AES_128/CBC/NOPADDING";
public static String CIPHER = "AES";
public static String char_code = "UTF-8";

public static String encrypt (String plaintext) throws Exception {
byte[] cipher_byte = crypt(plaintext.getBytes(), MODE.ENCRYPT);
String b64 = Base64.getEncoder().encodeToString(cipher_byte);
return b64;
}
public static String decrypt (String text64) throws Exception {
byte[] cipher_byte = Base64.getDecoder().decode(text64.getBytes() );
byte[] plain_byte = crypt(cipher_byte, MODE.DECRYPT);
String plaintext = new String(plain_byte, char_code);
return plaintext;
}
public static byte[] crypt(byte[] input, MODE mode) throws Exception {
byte[] output;
try {
byte[] key = ENCRYPT_KEY.getEncoded();
byte[] iv = ENCRYPT_IV;

SecretKeySpec skey = new SecretKeySpec(key, CIPHER);

IvParameterSpec ivp = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance(ALGORITHM);
int m = (mode == MODE.ENCRYPT)
? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
cipher.init(m, skey, ivp);
output = cipher.doFinal(input);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Can not crypt");
}
return output;
}
public static void makeKey () throws Exception {
try {
KeyGenerator keygen = KeyGenerator.getInstance(CIPHER);
SecureRandom random = new SecureRandom();
keygen.init(KEY_SIZE, random);
ENCRYPT_KEY = keygen.generateKey();
} catch (Exception e) {
throw new Exception("Can not make key", e);
}
}
public static void makeIv () throws Exception {
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
ENCRYPT_IV = new byte[16];
random.nextBytes(ENCRYPT_IV);
}
public static void main(String[] args) {
String text = "HogehogeHogehoge";
try {
makeKey();
makeIv();
String cipher_text = encrypt(text);
String plain_text = decrypt(cipher_text);
System.out.println(cipher_text);
System.out.println(plain_text);
} catch (Exception e) {
e.printStackTrace();
}
}
}


新着レスの表示


名前: E-mail(省略可)

※書き込む際の注意事項はこちら

※画像アップローダーはこちら

(画像を表示できるのは「画像リンクのサムネイル表示」がオンの掲示板に限ります)

掲示板管理者へ連絡 無料レンタル掲示板