1
 public static String encryptStringToBase64(String messageString) { 
        byte[] messageBytes = messageString.getBytes("UTF-8"); 
        byte[] encrypted = convert(1, messageBytes); 
        return Base64.encodeBytes(encrypted); 
    } 

private static byte[] convert(int mode, byte[] messageBytes) { 

    MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); 
    sha256.update("abcdefgh".getBytes("UTF-8")); 
    byte[] keyBytes = sha256.digest(); 
    byte[] hash = Arrays.copyOfRange(keyBytes, 0, 16); 

    SecretKeySpec keySpec = new SecretKeySpec(hash, "AES"); 
    byte[] ivBytes = new byte[16]; 
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); 
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
    cipher.init(mode, keySpec, ivSpec); 
    return cipher.doFinal(messageBytes); 
}

Above is the logic used in java for encryption tried below encryption techniques https://gist.github.com/m1entus/f70d4d1465b90d9ee024 https://github.com/krzyzanowskim/CryptoSwift but I am unable to produce same encrypted string in both java & iOS. Is there any way that I can reproduce the same data in iOS.

Swift 3.0 code

import CryptoSwift

let ram = "aaaa"
let pas = "abbbb"

let usernameutf8data = ram.data(using: String.Encoding.utf8)
let passwordutf8data = pas.data(using: String.Encoding.utf8)

let copyRight = "abcdefgh"
let copyRightUtf8 = copyRight.data(using: .utf8)

let hash =  copyRightUtf8?.sha256()
let key: Array<UInt8> = Array(hash!.bytes[0..<16])

let iv: Array<UInt8> = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]//AES.randomIV(AES.blockSize)

let encrypted = try AES(key:key , iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt(usernameutf8data!)

let encryptedpas = try AES(key:key , iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt(passwordutf8data!)
1
  • Show your Swift code and specify what doesn't work Commented Jul 11, 2017 at 9:22

1 Answer 1

3

Base64EncodedString:

 func encryptStringToBaseSixtyFour(value : String) -> String {
       let data = value.data(using: .utf8)
       return data?.base64EncodedString()
 }

AES Encrypted String

func aesEncrypt(value: String, key: String, iv: String) throws -> String {
    let data = value.data(using: .utf8)!
    let encrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt([UInt8](data))
    let encryptedData = Data(encrypted)
    return encryptedData.base64EncodedString()
}

AES Decrypted Value

func aesDecrypt(encryptedString: String,key: String, iv: String) throws -> String {
    let data = Data(base64Encoded: encryptedString)!
    let decrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).decrypt([UInt8](data))
    let decryptedData = Data(decrypted)
    return String(bytes: decryptedData.bytes, encoding: .utf8) ?? "Could not decrypt"
}
Sign up to request clarification or add additional context in comments.

8 Comments

in java for iv value byte[] ivBytes = new byte[16]; IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); what is equivalent for iv in swift.
let ivByte = [UInt8](repeatElement(0, count: 16)) let ivParameterSpec = IvParameterSpec(ivByte) for explanation https://stackoverflow.com/questions/36459726/create-a-16-byte-array-of-zeros-in-swift
how about this MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); sha256.update("abcdefgh".getBytes("UTF-8")); byte[] keyBytes = sha256.digest(); byte[] hash = Arrays.copyOfRange(keyBytes, 0, 16); SecretKeySpec keySpec = new SecretKeySpec(hash, "AES");
are you using AES encryption?
yes we are using AES encryption.... try AES(key:Array<UInt8> , iv: Array<UInt8>, blockMode: .CBC, padding: PKCS7()).encrypt(usernameutf8data!)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.