Development
보안|개인정보 보호 및 암호화
어느가을빛
2010. 7. 26. 15:38
1. 단방향 암호화: SHA-512 알고리즘 사용
- 대상: 비밀번호
2. 양방향 암호화: AES256 알고리즘 사용
|
JAVA AES256 알고리즘
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.NoSuchAlgorithmException;
public class AES256 {
public static SecretKeySpec getKeySpec() throws IOException, NoSuchAlgorithmException {
byte[] bytes = new byte[32];
SecretKey key = null;
SecretKeySpec spec = null;
/*
File f = new File("aes_key");
if (f.exists()) {
new FileInputStream(f).read(bytes);
} else {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(256);
key = kgen.generateKey();
bytes1 = key.getEncoded();
new FileOutputStream(f).write(bytes1);
}
*/
String keyStr="암호화된 대칭키";
bytes = Base64Utils.base64Decode(keyStr);
spec = new SecretKeySpec(bytes,"AES");
return spec;
}
public String encrypt(String text) throws Exception {
SecretKeySpec spec = getKeySpec();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, spec);
BASE64Encoder enc = new BASE64Encoder();
return enc.encode(cipher.doFinal(text.getBytes())).toString();
}
public String decrypt(String text) throws Exception {
SecretKeySpec spec = getKeySpec();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, spec);
BASE64Decoder dec = new BASE64Decoder();
return new String(cipher.doFinal(dec.decodeBuffer(text)));
}
public static void main(String[] args) throws Exception {
String mode = "encrypt";
String text = "!mpluse0218";
String encText = "";
String decText = "";
AES256 aes = new AES256();
encText=aes.encrypt(text);
System.out.println("5ClhQFro0UAI+LrZkilamw==");
System.out.println("enc::"+encText);
decText = aes.decrypt(encText);
System.out.println("dec::"+decText);
}
}
import sun.misc.BASE64Decoder;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.NoSuchAlgorithmException;
public class AES256 {
public static SecretKeySpec getKeySpec() throws IOException, NoSuchAlgorithmException {
byte[] bytes = new byte[32];
SecretKey key = null;
SecretKeySpec spec = null;
/*
File f = new File("aes_key");
if (f.exists()) {
new FileInputStream(f).read(bytes);
} else {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(256);
key = kgen.generateKey();
bytes1 = key.getEncoded();
new FileOutputStream(f).write(bytes1);
}
*/
String keyStr="암호화된 대칭키";
bytes = Base64Utils.base64Decode(keyStr);
spec = new SecretKeySpec(bytes,"AES");
return spec;
}
public String encrypt(String text) throws Exception {
SecretKeySpec spec = getKeySpec();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, spec);
BASE64Encoder enc = new BASE64Encoder();
return enc.encode(cipher.doFinal(text.getBytes())).toString();
}
public String decrypt(String text) throws Exception {
SecretKeySpec spec = getKeySpec();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, spec);
BASE64Decoder dec = new BASE64Decoder();
return new String(cipher.doFinal(dec.decodeBuffer(text)));
}
public static void main(String[] args) throws Exception {
String mode = "encrypt";
String text = "!mpluse0218";
String encText = "";
String decText = "";
AES256 aes = new AES256();
encText=aes.encrypt(text);
System.out.println("5ClhQFro0UAI+LrZkilamw==");
System.out.println("enc::"+encText);
decText = aes.decrypt(encText);
System.out.println("dec::"+decText);
}
}
SHA512 알고리즘
http://www.docjar.com/html/api/gnu/java/security/hash/Sha512.java.html
http://blog.fethilale.com/sha512-hashing-on-java/
http://blog.fethilale.com/sha512-hashing-on-java/
JAVA MessageDigest Class 활용한 방법
http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html (JavaTM 2 Platform Std. Ed. v1.4.2)
This MessageDigest class provides applications the functionality of a message digest algorithm, such as MD5 or SHA. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.
If you want to hash password with SHA256, you can change the line:
to
For others: