简体   繁体   中英

migrating encrypted data to Sql server from MYSQL server

We have aes-256 encryption for some data in one of the tables and we are migrating this to sql server. The problem is that we cannot decrypt the data in sql server due to incompatibility. Is there any way we can encrypt data in MYSQL in a way which is compatible with sql server aswell. Any advise ?

if you know the secretkey then you can decrypt the data see following code for encryption and decryption of AES-256 . the code is written in JAVA check this link AES-256 Password Based Encryption/Decryption in Java

 import java.security.AlgorithmParameters; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; import javax.xml.bind.DatatypeConverter; public class EncryptionDecryption { private static String salt; private static int iterations = 65536 ; private static int keySize = 256; private static byte[] ivBytes; private static SecretKey secretKey; public static void main(String []args) throws Exception { salt = getSalt(); char[] message = "PasswordToEncrypt".toCharArray(); System.out.println("Message: " + String.valueOf(message)); System.out.println("Encrypted: " + encrypt(message)); System.out.println("Decrypted: " + decrypt(encrypt(message).toCharArray())); } public static String encrypt(char[] plaintext) throws Exception { byte[] saltBytes = salt.getBytes(); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(plaintext, saltBytes, iterations, keySize); secretKey = skf.generateSecret(spec); SecretKeySpec secretSpec = new SecretKeySpec(secretKey.getEncoded(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretSpec); AlgorithmParameters params = cipher.getParameters(); ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV(); byte[] encryptedTextBytes = cipher.doFinal(String.valueOf(plaintext).getBytes("UTF-8")); return DatatypeConverter.printBase64Binary(encryptedTextBytes); } public static String decrypt(char[] encryptedText) throws Exception { System.out.println(encryptedText); byte[] encryptedTextBytes = DatatypeConverter.parseBase64Binary(new String(encryptedText)); SecretKeySpec secretSpec = new SecretKeySpec(secretKey.getEncoded(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretSpec, new IvParameterSpec(ivBytes)); byte[] decryptedTextBytes = null; try { decryptedTextBytes = cipher.doFinal(encryptedTextBytes); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return new String(decryptedTextBytes); } public static String getSalt() throws Exception { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); byte[] salt = new byte[20]; sr.nextBytes(salt); return new String(salt); } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM