Java using AES 256 and 128 Symmetric-key encryption

前端 未结 5 1562
余生分开走
余生分开走 2020-12-07 19:29

I am new in cipher technology. I found this code to do Symmetric Encryption.

byte[] key = //... secret sequence of bytes
byte[] dataToSend = ...
Cipher c = C         


        
5条回答
  •  星月不相逢
    2020-12-07 20:18

    public class CipherUtils
    {
        private static byte[] key = {
                0x74, 0x68, 0x69, 0x73, 0x49, 0x73, 0x41, 0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b, 0x65, 0x79
        };//"thisIsASecretKey";
    
        public static String encrypt(String strToEncrypt)
        {
            try
            {
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
                cipher.init(Cipher.ENCRYPT_MODE, secretKey);
                final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
                return encryptedString;
            }
            catch (Exception e)
            {
               e.printStackTrace();
            }
            return null;
        }
    }
    

提交回复
热议问题