• 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
Services from s21g
YOMU Web小説リーダー
Web小説を音声で楽しむ読み上げリーダー
補助探
公開されている補助金・助成金情報を集約し、条件に合う制度を探しやすくするサービスです。
jotter.me
個人開発者のためのホスティング一体型ノートサービス
ハンドミラー
iPhone向けの手鏡アプリ
ツイプロ(twpro)
Twitterプロフィールの高速検索エンジン