Java AES Salesforce

————————————–[下記開発例-S]—————————————-
Sample code Download
ApexCryptSample
JavaCryptSample

Java Decode Apex Encode String:new String(CryptUtil128.decrypt(Base64.decode(apex encode String));

————————————–[上記開発例-E]—————————————-

Ref Link
——————————————————————————————–
————————————–[上記参照例]—————————————-
package com.ilhwa.crypto;

import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidParameterSpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

public class Crypto {

public static void main(String[] args) {

// 平文のパスワード

String plainText = “sBivNq2p”;

// 32byte(256bit)のキー

byte[] key = “0xnF7TvvRrkUk7FvfdT6mjF83RRoJoYD”.getBytes();

try {
System.out.println(“Plain Text : ” + plainText);
System.out.println(“Plain Text Base64 : ” + Base64.encode(plainText.getBytes()));
System.out.println(“Key Base64 : ” + Base64.encode(key));

AlgorithmParameters ap = AlgorithmParameters.getInstance(ALGORITHM_NAME);

// 16byteの初期化ベクトル

ap.init(new IvParameterSpec(“oG1s7oSD2hTSb4Z6”.getBytes()));

// 暗号化

byte[] encrypted = encrypt(plainText, key, ap);

System.out.println(“Encrypted Text Base64 : ” + Base64.encode(encrypted));

// 復号

byte[] decrypted = decrypt(encrypted, key, ap);

System.out.println(“Decrypted Text : ” + new String(decrypted));

}
catch (Exception e) {
e.printStackTrace();
}
}

public static byte[] encrypt(String plainText, byte[] key, AlgorithmParameters ap)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException,
InvalidParameterSpecException, InvalidAlgorithmParameterException {

if (plainText == null || plainText.length() == 0 || key == null || key.length == 0) {

return new byte[0];
}

SecretKeySpec sks = new SecretKeySpec(key, ALGORITHM_NAME);

Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, sks, ap);

// ここで出力される iv を Salesforce 側で指定する

byte[] iv = cipher.getIV();
System.out.println(“IV Base64 : ” + Base64.encode(iv));

byte[] encrypted = cipher.doFinal(plainText.getBytes());

return encrypted;
}

public static byte[] decrypt(byte[] encrypted, byte[] key, AlgorithmParameters ap)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException,
InvalidParameterSpecException, InvalidAlgorithmParameterException {

if (encrypted == null || encrypted.length == 0 || key == null || key.length == 0) {

return new byte[0];
}

SecretKeySpec sks = new SecretKeySpec(key, ALGORITHM_NAME);

Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, sks, ap);

return cipher.doFinal(encrypted);
}

private static final String ALGORITHM_NAME = “AES”;
private static final String ALGORITHM = “AES/CBC/PKCS5Padding”;
}
————————————–[上記参照例]—————————————-

コメントを残す

メールアドレスが公開されることはありません。