Вы находитесь на странице: 1из 4

Crypto.java byte[] inputBytes = inputString.getBytes(); //Untuk akses ke bit package crypto; byte[] outputBytes = cipher.doFinal(inputBytes); encryptedValue = new BASE64Encoder().

encode(outputBytes); import java.io.IOException; return encryptedValue; import java.security.InvalidKeyException; } import javax.crypto.BadPaddingException; public static String decrypt(String encryptString, String commonKey) import javax.crypto.Cipher; throws InvalidKeyException, IllegalBlockSizeException, import javax.crypto.IllegalBlockSizeException; BadPaddingException, IOException { import javax.crypto.SecretKey; String decryptedValue = ""; import javax.crypto.SecretKeyFactory; encryptString = encryptString.replace(' ', '+'); import javax.crypto.spec.DESKeySpec; SecretKey key = getSecretKey(commonKey); import sun.misc.BASE64Decoder; cipher.init(Cipher.DECRYPT_MODE, key); import sun.misc.BASE64Encoder; byte[] recoveredBytes = cipher.doFinal(new BASE64Decoder().decodeBuffer(encryptString)); //Untuk akses ke bit /** decryptedValue = new String(recoveredBytes);

* @author SOEDJANA return decryptedValue; */ }

public class kriptografi { private static SecretKey getSecretKey(String secretPassword) { private static final String CRYPTOGRAPHY_ALGO_DES = "DES"; SecretKey key = null; private static Cipher cipher = null; try { private static DESKeySpec keySpec = null; cipher = Cipher.getInstance(CRYPTOGRAPHY_ALGO_DES); private static SecretKeyFactory keyFactory = null; keySpec = new DESKeySpec(secretPassword.getBytes("UTF8")); keyFactory = SecretKeyFactory.getInstance(CRYPTOGRAPHY_ALGO_DES);commonKey) public static String encrypt(String inputString, String key = keyFactory.generateSecret(keySpec); throws InvalidKeyException, IllegalBlockSizeException, } catch (Exception e) { BadPaddingException { String e.printStackTrace(); encryptedValue = ""; System.out.println("Error in generating SecretKey key = getSecretKey(commonKey); the secret Key"); } cipher.init(Cipher.ENCRYPT_MODE, key); return key;} }

package crypto;

/** * @author SOEDJANA * */ public class testing { public static final String DES_ENCRYPTION_KEY = "testString"; /** * @param args */ public static void main(String[] args) { try{ String input = "egy sujana"; System.out.println("Input : "+input); String encrypted = kriptografi.encrypt(input, DES_ENCRYPTION_KEY); System.out.println("Hasil enkripsi : "+encrypted); String decrypted = kriptografi.decrypt(encrypted, DES_ENCRYPTION_KEY); Testing.java System.out.println("Hasil dekripsi : "+decrypted); }catch(Exception e){ } } }

run: Hasil egy sujana Input :running program Hasil enkripsi : Pyg8TnBNcnbyjXiV0yUqZA== Hasil dekripsi : egy sujana

Вам также может понравиться