Javaメモ > 共通鍵方式による暗号化/復号化(Java)

共通鍵方式による暗号化/復号化(Java)

【関連】

共通鍵方式による暗号化/復号化

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class MyCrypt {

	public static final String cryptType = "AES";
	//public static final String cryptType = "Blowfish";

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String key = "a1b2c3d4";
		String text = "N12345678";
		String encryptedText = encrypt(key, text, "UTF-8");
		System.out.println(encryptedText);

		String decryptedText = decrypt(key, encryptedText, "UTF-8");
		System.out.println(decryptedText);
	}

	/**
	 * バイト配列を暗号化する。<br />
	 * @param secretkey 暗号化キー
	 * @param data バイトデータ
	 * @return 暗号化されたバイトデータ
	 */
	public static byte[] encrypt(String secretkey, byte[] data) {

		SecretKeySpec key;
		byte[] bytes = new byte[128 / 8];
		byte[] keys = null;
		try {
			keys = secretkey.getBytes("UTF-8");
			for (int i = 0; i < secretkey.length(); i++) {
				if (i >= bytes.length)
					break;
				bytes[i] = keys[i];
			}
			key = new SecretKeySpec(bytes, cryptType);
			byte[] de = null;
			Cipher cipher = Cipher.getInstance(cryptType);
			cipher.init(Cipher.ENCRYPT_MODE, key);
			de = cipher.doFinal(data);
			byte[] encData = Base64.encodeBase64(de);
			return encData;
		} catch (Exception e1) {
			e1.printStackTrace();
		} 
		return null;
	} 

	/**
	 * バイト配列を復号化する。<br />
	 * @param secretkey 暗号化キー
	 * @param data バイトデータ
	 * @return 復号化されたバイトデータ
	 */
	public static byte[] decrypt(String secretkey, byte[] data) {

		try {
			byte[] bytes = new byte[128 / 8];
			byte[] keys = secretkey.getBytes("UTF-8");
			for (int i = 0; i < secretkey.length(); i++) {
				if (i >= bytes.length)
					break;
				bytes[i] = keys[i];
			}
			javax.crypto.spec.SecretKeySpec sksSpec = new SecretKeySpec(bytes, cryptType);
			javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(cryptType);
			cipher.init(javax.crypto.Cipher.DECRYPT_MODE, sksSpec);
			byte[] decrypted = cipher.doFinal(Base64.decodeBase64(data));
			return decrypted;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	} 

	/**
	 * 文字列を暗号化する。<br />
	 * @param secretkey 暗号化キー
	 * @param text 文字列
	 * @return 復号化された文字列
	 * @throws UnsupportedEncodingException
	 */
	public static String encrypt(String secretkey, String text, String encode) throws UnsupportedEncodingException {

		byte[] encBytes = encrypt(secretkey, text.getBytes(encode));
		String encrypted = new String(encBytes, encode);
		return encrypted;
	} 

	/**
	 * 文字列を復号化する。<br />
	 * @param secretkey 暗号化キー
	 * @param encryptedText 暗号化文字列
	 * @return 復号化された文字列
	 * @throws UnsupportedEncodingException
	 * @throws UnsupportedEncodingException
	 */
	public static String decrypt(String secretkey, String encryptedText, String encode)
			throws UnsupportedEncodingException {

		byte[] decBytes = decrypt(secretkey, encryptedText.getBytes(encode));
		return new String(decBytes, encode);
	} 
}

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2015-10-04 (日) 05:52:33 (3127d)