代码之家  ›  专栏  ›  技术社区  ›  MarcForn

加密/解密字符串Kotlin

  •  6
  • MarcForn  · 技术社区  · 6 年前

    我在Kotlin中创建了这两个扩展来加密/解密字符串:

    fun String.encrypt(seed : String): String {
        val keyGenerator = KeyGenerator.getInstance("AES")
        val secureRandom = SecureRandom.getInstance("SHA1PRNG")
        secureRandom.setSeed(seed.toByteArray())
    
        keyGenerator.init(128, secureRandom)
        val skey = keyGenerator.generateKey()
        val rawKey : ByteArray = skey.encoded
    
        val skeySpec = SecretKeySpec(rawKey, "AES")
        val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec)
        val byteArray = cipher.doFinal(this.toByteArray())
    
        return byteArray.toString()
    }
    
    fun String.decrypt(seed : String): String {
        val keyGenerator = KeyGenerator.getInstance("AES")
        val secureRandom = SecureRandom.getInstance("SHA1PRNG")
        secureRandom.setSeed(seed.toByteArray())
    
        keyGenerator.init(128, secureRandom)
        val skey = keyGenerator.generateKey()
        val rawKey : ByteArray = skey.encoded
    
        val skeySpec = SecretKeySpec(rawKey, "AES")
        val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
        cipher.init(Cipher.DECRYPT_MODE, skeySpec)
        val byteArray = cipher.doFinal(this.toByteArray())
    
        return byteArray.toString()
    }
    

    出于某种原因,我遇到以下异常:

    javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
    

    我做错了什么?

    6 回复  |  直到 6 年前
        1
  •  16
  •   Kasım Özdemir    4 年前

    使用base64密钥、salt和iv(初始化向量)进行AES加密/解密。

    object AESEncyption {
    
    const val secretKey = "tK5UTui+DPh8lIlBxya5XVsmeDCoUl6vHhdIESMB6sQ="
    const val salt = "QWlGNHNhMTJTQWZ2bGhpV3U=" // base64 decode => AiF4sa12SAfvlhiWu
    const val iv = "bVQzNFNhRkQ1Njc4UUFaWA==" // base64 decode => mT34SaFD5678QAZX
    
    fun encrypt(strToEncrypt: String) :  String?
    {
        try
        {
            val ivParameterSpec = IvParameterSpec(Base64.decode(iv, Base64.DEFAULT))
    
            val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
            val spec =  PBEKeySpec(secretKey.toCharArray(), Base64.decode(salt, Base64.DEFAULT), 10000, 256)
            val tmp = factory.generateSecret(spec)
            val secretKey =  SecretKeySpec(tmp.encoded, "AES")
    
            val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec)
            return Base64.encodeToString(cipher.doFinal(strToEncrypt.toByteArray(Charsets.UTF_8)), Base64.DEFAULT)
        }
        catch (e: Exception)
        {
            println("Error while encrypting: $e")
        }
        return null
    }
    
    fun decrypt(strToDecrypt : String) : String? {
        try
        {
    
            val ivParameterSpec =  IvParameterSpec(Base64.decode(iv, Base64.DEFAULT))
    
            val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
            val spec =  PBEKeySpec(secretKey.toCharArray(), Base64.decode(salt, Base64.DEFAULT), 10000, 256)
            val tmp = factory.generateSecret(spec);
            val secretKey =  SecretKeySpec(tmp.encoded, "AES")
    
            val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
            return  String(cipher.doFinal(Base64.decode(strToDecrypt, Base64.DEFAULT)))
        }
        catch (e : Exception) {
            println("Error while decrypting: $e");
        }
        return null
      }
    }
    

    iOS swift

        2
  •  9
  •   MarcForn    6 年前

    按照Maarten Bodews指南,我将问题修复为:

    fun String.encrypt(password: String): String {
        val secretKeySpec = SecretKeySpec(password.toByteArray(), "AES")
        val iv = ByteArray(16)
        val charArray = password.toCharArray()
        for (i in 0 until charArray.size){
            iv[i] = charArray[i].toByte()
        }
        val ivParameterSpec = IvParameterSpec(iv)
    
        val cipher = Cipher.getInstance("AES/GCM/NoPadding")
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec)
    
        val encryptedValue = cipher.doFinal(this.toByteArray())
        return Base64.encodeToString(encryptedValue, Base64.DEFAULT)
    }
    
    fun String.decrypt(password: String): String {
        val secretKeySpec = SecretKeySpec(password.toByteArray(), "AES")
        val iv = ByteArray(16)
        val charArray = password.toCharArray()
        for (i in 0 until charArray.size){
            iv[i] = charArray[i].toByte()
        }
        val ivParameterSpec = IvParameterSpec(iv)
    
        val cipher = Cipher.getInstance("AES/GCM/NoPadding")
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec)
    
        val decryptedByteValue = cipher.doFinal(Base64.decode(this, Base64.DEFAULT))
        return String(decryptedByteValue)
    }
    
        3
  •  7
  •   Maarten Bodewes    6 年前

    要对密文进行编码,请使用基数64或十六进制。Java API包含Base64类,因此您最好使用它。

    byte[]#toString 不做你期望它做的事;它只返回 字节数组引用 ,而不是字节数组的内容。


    除此之外:

    • 不要使用SecureRandom派生密钥,尝试查找PBKDF2;
    • 明确使用操作模式,例如 "AES/GCM/NoPadding" ;
    • 如果您决定使用CBC(通常不安全),请使用唯一IV和随机IV;
    • 不使用 toByteArray 无需为消息显式选择字符编码。
        4
  •  3
  •   Bolt UIX    4 年前

    在Android中实现AES加密和解密的最简单方法是在项目中复制此类。

    加密字符串

    请先在项目中复制Asutils类,然后可以这样使用它。

    String encrypted = "";
    String sourceStr = "This is any source string";
    try {
        encrypted = AESUtils.encrypt(sourceStr);
        Log.d("TEST", "encrypted:" + encrypted);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    解密字符串

    String encrypted = "ANY_ENCRYPTED_STRING_HERE";
    String decrypted = "";
    try {
        decrypted = AESUtils.decrypt(encrypted);
        Log.d("TEST", "decrypted:" + decrypted);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    七叶树类

    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    
    public class AESUtils 
    {
    
        private static final byte[] keyValue =
                new byte[]{'c', 'o', 'd', 'i', 'n', 'g', 'a', 'f', 'f', 'a', 'i', 'r', 's', 'c', 'o', 'm'};
    
    
        public static String encrypt(String cleartext)
                throws Exception {
            byte[] rawKey = getRawKey();
            byte[] result = encrypt(rawKey, cleartext.getBytes());
            return toHex(result);
        }
    
        public static String decrypt(String encrypted)
                throws Exception {
    
            byte[] enc = toByte(encrypted);
            byte[] result = decrypt(enc);
            return new String(result);
        }
    
        private static byte[] getRawKey() throws Exception {
            SecretKey key = new SecretKeySpec(keyValue, "AES");
            byte[] raw = key.getEncoded();
            return raw;
        }
    
        private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
            SecretKey skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(clear);
            return encrypted;
        }
    
        private static byte[] decrypt(byte[] encrypted)
                throws Exception {
            SecretKey skeySpec = new SecretKeySpec(keyValue, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] decrypted = cipher.doFinal(encrypted);
            return decrypted;
        }
    
        public static byte[] toByte(String hexString) {
            int len = hexString.length() / 2;
            byte[] result = new byte[len];
            for (int i = 0; i < len; i++)
                result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
                        16).byteValue();
            return result;
        }
    
        public static String toHex(byte[] buf) {
            if (buf == null)
                return "";
            StringBuffer result = new StringBuffer(2 * buf.length);
            for (int i = 0; i < buf.length; i++) {
                appendHex(result, buf[i]);
            }
            return result.toString();
        }
    
        private final static String HEX = "0123456789ABCDEF";
    
        private static void appendHex(StringBuffer sb, byte b) {
            sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
        }
    }
    
        5
  •  2
  •   H.Fa8    3 年前

    您可以像这样在Kotlin中加密和解密

    首先,您需要:

    val SECRET_KEY = "secretKey"
    val SECRET_IV = "secretIV"
    

    在那之后

    加密:

    private fun String.encryptCBC(): String {
        val iv = IvParameterSpec(SECRET_IV.toByteArray())
        val keySpec = SecretKeySpec(SECRET_KEY.toByteArray(), "AES")
        val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv)
        val crypted = cipher.doFinal(this.toByteArray())
        val encodedByte = Base64.encode(crypted, Base64.DEFAULT)
        return String(encodedByte)
    }
    

    并解密:

    private fun String.decryptCBC(): String {
        val decodedByte: ByteArray = Base64.decode(this, Base64.DEFAULT)
        val iv = IvParameterSpec(SECRET_IV.toByteArray())
        val keySpec = SecretKeySpec(SECRET_KEY.toByteArray(), "AES")
        val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
        cipher.init(Cipher.DECRYPT_MODE, keySpec, iv)
        val output = cipher.doFinal(decodedByte)
        return String(output)
    }
    

    之后,您可以这样使用它:

    val strEncrypt = edt.text.toString().encryptCBC()
    val strDecrypted = strEncrypt?.decryptCBC()
    
        6
  •  0
  •   Irving Kennedy    4 年前

    根据MarcForn指南,我将其简化如下:

    const val encryptionKey = "ENCRYPTION_KEY"
    
    fun String.cipherEncrypt(encryptionKey: String): String? {
        try {
            val secretKeySpec = SecretKeySpec(encryptionKey.toByteArray(), "AES")
            val iv = encryptionKey.toByteArray()
            val ivParameterSpec = IvParameterSpec(iv)
    
            val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec)
    
            val encryptedValue = cipher.doFinal(this.toByteArray())
            return Base64.encodeToString(encryptedValue, Base64.DEFAULT)
        } catch (e: Exception) {
            e.message?.let{ Log.e("encryptor", it) }
        }
        return null
    }
    
    fun String.cipherDecrypt(encryptionKey: String): String? {
        try {
            val secretKeySpec = SecretKeySpec(encryptionKey.toByteArray(), "AES")
            val iv = encryptionKey.toByteArray()
            val ivParameterSpec = IvParameterSpec(iv)
    
            val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec)
    
            val decodedValue = Base64.decode(this, Base64.DEFAULT)
            val decryptedValue = cipher.doFinal(decodedValue)
            return String(decryptedValue)
        } catch (e: Exception) {
            e.message?.let{ Log.e("decryptor", it) }
        }
        return null
    }