• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

ruby>>
// To use those functions below
// You need to import CommonCrypto.h in your bridging header.
// #import <CommonCrypto/CommonCrypto.h>

func
DataCryptedByAES( operation: CCOperation, p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
var wKeyLength = 0
if key.length < kCCKeySizeAES128 { assert( false ) }
else if key.length < kCCKeySizeAES192 { wKeyLength = kCCKeySizeAES128 }
else if key.length < kCCKeySizeAES256 { wKeyLength = kCCKeySizeAES192 }
else { wKeyLength = kCCKeySizeAES256 }

if iv != nil { assert( iv!.length == kCCBlockSizeAES128 ) }

var	wLength = UInt( ( ( p.length + kCCBlockSizeAES128 - 1 ) / kCCBlockSizeAES128 ) * kCCBlockSizeAES128 )
let	v = NSMutableData( length: Int( wLength ) )!
let	s: CCCryptorStatus = CCCrypt(
	operation
,	CCAlgorithm( kCCAlgorithmAES )
,	options
,	key.bytes
,	UInt( wKeyLength )
,	iv != nil ? iv!.bytes : nil
,	p.bytes
,	UInt( p.length )
,	v.mutableBytes
,	wLength
,	&wLength
)
v.length = Int( wLength )
return ( s, v )

}

func
DataEncryptedByAES( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
return DataCryptedByAES( CCOperation( kCCEncrypt ), p, key, options, iv )
}

func
DataDecryptedByAES( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
return DataCryptedByAES( CCOperation( kCCDecrypt ), p, key, options, iv )
}

func
DataCryptedByBlowfish( operation: CCOperation, p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
if iv != nil { assert( iv!.length == kCCBlockSizeBlowfish ) }

var	wLength = UInt( ( ( p.length + kCCBlockSizeBlowfish - 1 ) / kCCBlockSizeBlowfish ) * kCCBlockSizeBlowfish )
let	v = NSMutableData( length: Int( wLength ) )!
let	s: CCCryptorStatus = CCCrypt(
	operation
,	CCAlgorithm( kCCAlgorithmBlowfish )
,	options
,	key.bytes
,	UInt( key.length )
,	iv != nil ? iv!.bytes : nil
,	p.bytes
,	UInt( p.length )
,	v.mutableBytes
,	wLength
,	&wLength
)
v.length = Int( wLength )
return ( s, v )

}

func
DataEncryptedByBlowfish( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
return DataCryptedByBlowfish( CCOperation( kCCEncrypt ), p, key, options, iv )
}

func
DataDecryptedByBlowfish( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
return DataCryptedByBlowfish( CCOperation( kCCDecrypt ), p, key, options, iv )
}

//USAGE

func
UTF8Data( p: String ) -> NSData? {
return p.dataUsingEncoding( NSUTF8StringEncoding )
}

func
Usage() {
let wKey = UTF8Data( "teststring" )!
let wIV = UTF8Data( "12345678" )!
var ( s, data ) = DataEncryptedByBlowfish( UTF8Data( "testmessage" )!, wKey, CCOptions( kCCOptionPKCS7Padding ), wIV )
assert( Int( s ) == kCCSuccess )
( s, data ) = DataDecryptedByBlowfish( data, wKey, CCOptions( kCCOptionPKCS7Padding ), wIV )
assert( Int( s ) == kCCSuccess )
}

<<--

posted by Saturn Saturn on Thu 26 Mar 2015 at 08:46 with 0 comments
Contents rssrss
Value Transformer
AES & Blowfish in swift (CBC)
OreOre.pem の作り方
JSON Chat Server Skelton in GoLang TLS Ver.
JSON Chat Server Skelton in GoLang
UITextView と、UITextField でキーボードが出た時の Notification の順番が違う
Making window which covers iPhone's Status Bar
UIImagePickerController の編集領域
UIView の tag property
UILocationManager
Comments
https://h-c-m.jp/ https://h-c-m.jp/: ご担当者様 突然のご連絡失礼いたします。WEB担当の中島と申します。 かねてより貴社のサイトを拝... 05/07 12:31
https://h-c-m.jp/ https://h-c-m.jp/: ご担当者様 突然のご連絡失礼いたします。WEB担当の中島と申します。 かねてより貴社のサイトを拝... 05/07 12:00
https://h-c-m.jp/ https://h-c-m.jp/: ご担当者様 突然のご連絡失礼いたします。WEB担当の中島と申します。 かねてより貴社のサイトを拝... 05/07 11:11
https://isshin-kikaku.com/resources/ https://isshin-kikaku.com/resources/: 突然のご連絡失礼いたします。 建築リペアガイド(https://isshin-kikaku.co... 04/25 00:02
https://www.nltc.co.jp/ https://www.nltc.co.jp/: ご担当者様 突然のご連絡失礼いたします。西の丘ローンテニスクラブ中川 と申します。 かねてより貴... 04/24 03:40
https://www.nltc.co.jp/ https://www.nltc.co.jp/: ご担当者様 突然のご連絡失礼いたします。西の丘ローンテニスクラブ中川 と申します。 かねてより貴... 04/23 10:58
https://www.nltc.co.jp/ https://www.nltc.co.jp/: ご担当者様 突然のご連絡失礼いたします。西の丘ローンテニスクラブ中川 と申します。 かねてより貴... 04/23 05:02
https://isshin-kikaku.com/resources/ https://isshin-kikaku.com/resources/: 突然のご連絡失礼いたします。 建築リペアガイド(https://isshin-kikaku.co... 04/21 06:07