I’m working on implementing encryption in MS SQL 2008R2. Loading data from a CVS file, I have that data in a temp table, and I now want to move some of that data into a encrypted credit card table. However, I keep getting the following error. Code for insert follows, table defs below.
The client ID is below 10 – the number causing the overflow is the very first line of data, CC number.
Error:
Msg 248, Level 16, State 1, Line 4
The conversion of the varchar value ‘5105105105105100’ overflowed an int column.
The statement has been terminated.
Insert statement:
OPEN SYMMETRIC KEY CreditCardKey
DECRYPTION BY CERTIFICATE CreditCardCert;
insert into CreditCard
Select
HASHBYTES('SHA1', t.CreditCard),
EncryptByKey(key_guid('CreditCardKey'), t.CreditCard),
'',
RIGHT(4, t.CreditCard),
'1',
t.Expiration,
convert(int, t.ClientID)
From TempTbl t
TempTbl:
ClientID int.First,Last,CC,Expiration— allvarchar(50).
All data appears to have imported correctly.
CreditCard: (destination table)
Hash varbinary(200)
Encrypted varbinary(400)
plaintext char(16)
lastfour int
servicecode int
expirationdate Date
ClientID int
Syntax issue was causing the overflow:
My use of the RIGHT function reversed the character and int offset, overflowing the integer expression of the function.
Correct use: