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!)
jww
  • 97,681
  • 90
  • 411
  • 885
rams
  • 652
  • 1
  • 9
  • 24

1 Answers1

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"
}
Subramanian P
  • 4,365
  • 2
  • 21
  • 25
  • in java for iv value byte[] ivBytes = new byte[16]; IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); what is equivalent for iv in swift. – rams Jul 11 '17 at 09:48
  • 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` – Subramanian P Jul 11 '17 at 10:07
  • 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"); – rams Jul 11 '17 at 10:28
  • are you using AES encryption? – Subramanian P Jul 11 '17 at 10:34
  • yes we are using AES encryption.... try AES(key:Array , iv: Array, blockMode: .CBC, padding: PKCS7()).encrypt(usernameutf8data!) – rams Jul 11 '17 at 10:38
  • give some input in java and get the result and past it here. I will help you get the same in ios – Subramanian P Jul 11 '17 at 10:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148946/discussion-between-rams-and-subramanian). – rams Jul 12 '17 at 00:08
  • @Subramanian, rams - Am looking for something similar. Do AES encryption in Swift and decrypt it in Java. Do you have any working example of the same? Thanks a bunch. – csharpnewbie Sep 28 '17 at 13:17