** 文字列を暗号化する [#lf8e7d29] ** 文字列を暗号化する(Java) [#lf8e7d29] #setlinebreak(on); #mycode{{ 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); } } }} #html(<!--); import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class TestCrypt { /** * 文字列を暗号化する<BR> * @param 暗号化キー(DES:8バイト、AES:16バイト) * @param 暗号化する文字列 * @param 暗号化アルゴリズム(DES, AES など) * @return 暗号化後の文字列 * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalStateException * @throws IllegalBlockSizeException * @throws BadPaddingException */ public static String encrypt(String key, String plainText, String CryptMode) throws NoSuchAlgorithmException , NoSuchPaddingException , InvalidKeyException , IllegalStateException , IllegalBlockSizeException , BadPaddingException{ byte[] byteText = plainText.getBytes(); Cipher cipher = Cipher.getInstance(CryptMode); SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), CryptMode); cipher.init(Cipher.ENCRYPT_MODE, sksSpec); byte[] buff = cipher.doFinal(byteText); String encryptText = new BASE64Encoder().encode(buff); return encryptText; } /** * 暗号化された文字列を復号化する<BR> * @param 暗号化キー(DES:8バイト、AES:16バイト) * @param 暗号化文字列 * @param 暗号化アルゴリズム(DES, AES など) * @return 復号化文字列 * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalStateException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws IOException */ public static String decrypt(String key, String encryptText, String CryptMode) throws NoSuchAlgorithmException , NoSuchPaddingException , InvalidKeyException , IllegalStateException , IllegalBlockSizeException , BadPaddingException , IOException{ Cipher cipher = Cipher.getInstance(CryptMode); SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), CryptMode); cipher.init(Cipher.DECRYPT_MODE, sksSpec); byte[] buff = cipher.doFinal(new BASE64Decoder().decodeBuffer(encryptText)); String decryptText = new String(buff); return decryptText; } } #html(-->);