반응형
자바스크립트로 비밀번호 암호화하기
다음은 CryptoJS 라이브러리를 사용하여 자바스크립트에서 비밀번호를 암호화하는 방법입니다.
// 먼저 CryptoJS 라이브러리를 포함시켜야 합니다.function encryptPassword(password) { var secretKey = "your-secret-key"; return CryptoJS.AES.encrypt(password, secretKey).toString(); } var password = "MySuperSecretPassword"; var encryptedPassword = encryptPassword(password); console.log("Encrypted Password: " + encryptedPassword);
자바로 비밀번호 암호화하기
다음은 Java에서 'javax.crypto' 패키지를 사용하여 비밀번호를 암호화하는 방법입니다.
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class PasswordEncryption {
public static String encryptPassword(String password, String key) throws Exception {
byte[] keyBytes = key.getBytes();
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(password.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static void main(String[] args) {
try {
String secretKey = "your-secret-key";
String originalPassword = "MySuperSecretPassword";
String encryptedPassword = encryptPassword(originalPassword, secretKey);
System.out.println("Encrypted Password: " + encryptedPassword);
} catch (Exception e) {
e.printStackTrace();
}
}
}
반응형