<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>復号化 &#8211; blog.fukata.org</title>
	<atom:link href="/archives/tag/%E5%BE%A9%E5%8F%B7%E5%8C%96/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>旅するプログラマ</description>
	<lastBuildDate>Fri, 11 Aug 2017 22:59:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.6</generator>
	<item>
		<title>JavaによるDESの暗号化・復号化ユーティリティ</title>
		<link>/archives/1447/</link>
					<comments>/archives/1447/#respond</comments>
		
		<dc:creator><![CDATA[fukata]]></dc:creator>
		<pubDate>Sat, 01 Aug 2009 01:53:53 +0000</pubDate>
				<category><![CDATA[開発]]></category>
		<category><![CDATA[DES]]></category>
		<category><![CDATA[復号化]]></category>
		<category><![CDATA[暗号化]]></category>
		<guid isPermaLink="false">http://fukata.org/?p=1447</guid>

					<description><![CDATA[今日は、暗号化・復号化に関するユーティリティを残しておきたいと思います。作業ログ的なもので。 package org.fukata.jc_common.utils; import java.io.IO ... <a href="/archives/1447/"> 続きを読む</a>]]></description>
										<content:encoded><![CDATA[<p>今日は、暗号化・復号化に関するユーティリティを残しておきたいと思います。作業ログ的なもので。</p>
<pre lang="JAVA" line="1">
package org.fukata.jc_common.utils;

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 暗号化に関するユーティリティ
 * @author tatsuya
 *
 */
public class CryptUtils {

	private static BASE64Encoder base64Encoder = new BASE64Encoder();
	private static BASE64Decoder base64Decoder = new BASE64Decoder();
	
	
	private CryptUtils() {
	}
	
	/**
	 * 暗号化する。
	 * @param decrypt 暗号元の文字列
	 * @return
	 * @throws NoSuchAlgorithmException 
	 * @throws NoSuchPaddingException 
	 * @throws InvalidKeyException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 * @throws InvalidKeySpecException 
	 */
	public static String encrypt(String decrypt) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
		// 暗号化
        Cipher c = Cipher.getInstance("DES");
        c.init(Cipher.ENCRYPT_MODE, generateSecretKey());
        byte input[] = decrypt.getBytes();
        byte encrypted[] = c.doFinal(input); 
        
		return base64Encoder.encode(encrypted);
	}
	
	/**
	 * 復号化する。
	 * @param encrypt 暗号化された文字列
	 * @return
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 * @throws NoSuchAlgorithmException 
	 * @throws NoSuchPaddingException 
	 * @throws InvalidAlgorithmParameterException 
	 * @throws InvalidKeyException 
	 * @throws InvalidKeySpecException 
	 * @throws IOException 
	 */
	public static String decrypt(String encrypt) throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException, IOException {
		
		Cipher c = Cipher.getInstance("DES");
        c.init(Cipher.DECRYPT_MODE, generateSecretKey());
        byte output[] = c.doFinal(base64Decoder.decodeBuffer(encrypt));

        return new String(output);
	}

	/**
	 * 鍵を生成する。
	 * @return
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	private static SecretKey generateSecretKey() throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
        DESKeySpec dk = new DESKeySpec(Config.ENCRYPT_KEY.getBytes());
        SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
        SecretKey sk = kf.generateSecret(dk);
        return sk;
	}
}
</pre>
]]></content:encoded>
					
					<wfw:commentRss>/archives/1447/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
