使用可变密码在SQL中创建非对称密钥(来自存储过程)

时间:2015-05-06 04:20:05

标签: sql sql-server stored-procedures sql-server-2014

create procedure SP_INS_PUBLIC_NHANVIEN  
    @manv varchar(20),
    @hoten nvarchar(100),
    @email varchar(20),
    @luongcb varbinary ,
    @tendn nvarchar(100),
    @mk varchar

as 
    create asymmetric key mahoaluongi
        with algorithm = RSA_512
        encryption by password = @mk
    insert into nhanvien values (@manv,@hoten,@email,ENCRYPTBYASYMKEY(mahoaluongi,@luongcb),@tendn,HASHBYTES('SHA1',@mk),@manv)
    drop asymmetric key mahoaluongi
go

我想每次都使用新密码创建一个新的非对称密钥,从存储过程的参数传递。 我收到错误

  

@mk附近的语法不正确。期待STRING或TEXT_LEX。

但是使用castconvert无法将varchar转换为STRING。

我正在使用SQL Server 2014

3 个答案:

答案 0 :(得分:0)

我建议使用Exec Command作为以下查询:

create procedure SP_INS_PUBLIC_NHANVIEN  
    @manv varchar(20),
    @hoten nvarchar(100),
    @email varchar(20),
    @luongcb varbinary ,
    @tendn nvarchar(100),
    @mk varchar

as 
    DECLARE @C NVARCHAR(MAX) = 
    'create asymmetric key mahoaluongi
        with algorithm = RSA_512
        encryption by password = '''+@mk+''''
    EXEC(@C)
    insert into nhanvien values (@manv,@hoten,@email,ENCRYPTBYASYMKEY(ASYMKEY_ID('mahoaluongi'),@luongcb),@tendn,HASHBYTES('SHA1',@mk),@manv)
    Exec('drop asymmetric key mahoaluongi')

答案 1 :(得分:0)

ALTER PROCEDURE SP_INS_ENCRYPTED_KEY @name VARCHAR(20)
    ,@age INT
    ,@city VARCHAR(20)
    ,@mk VARCHAR(20)
AS
CREATE asymmetric KEY mahoaluongi
    WITH algorithm = RSA_512 ENCRYPTION BY password = N'@mk'

INSERT INTO Emp@236186
VALUES (
    @name
    ,@age
    ,ENCRYPTBYASYMKEY(AsymKey_ID('mahoaluongi'), @city)
    )

DROP asymmetric KEY mahoaluongi
GO

答案 2 :(得分:0)

试试这个:

texts[i].length